Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   for loops and setarray (https://forums.graalonline.com/forums/showthread.php?t=32462)

Xbob42 06-28-2002 10:26 AM

for loops and setarray
 
I'm probably not the first to ask about these. I'm trying to learn about for loops and setarray (arrays or something).

Will anyone be nice and willing to post an extremely small script that does something and add an explanation (comments) alongside them so I can read it and hopefull get a better understanding of them.

I've tried to search through the old threads, but I didn't find anything really useful for what I was looking for. :(

I'll be very greatful and give many thanks to whoever could help me to understand them.

Python523 06-28-2002 10:32 AM

for(i=0;i<10;i++){
playerrupees+=i;
sleep 1;
}
that adds 1 ruppes every second for 10 second
setarray array,2;
array[0]=1
array[1]=2
you can save numberical info in arrays like that

mikepg 06-28-2002 11:04 AM

also
 
you can also set values of arrays like this:

array = {0,1,2,4,5,-15,..n};

setarray arrayname,10;

this would give you (in the debugger)
arrayname = 0/{0,0,0,0,0,0,0,0,0,0}
(the 0/ part does nothing, as far as i know)

example of an array in a for statement:

NPC Code:

setarray count,10;
for (i=1; i<=arraylen(count); i++;) {
count[i-1]=i;
}



and you'd get back
count = 0/{1,2,3,4,5,6,7,8,9,10}

count[x] is the "slot" of the array you are accessing..so
after we ran the forstatement above,
count[3] would be 4, because the array begins with count[0] (not count[1])

And...that's pretty much the basics...I don't know if that helped, that's the way I looked at it.

adam 06-29-2002 12:14 AM

NPC Code:

// NPC made by Rogue Shadow -TT-*C* (TCN) // my name
if (created){ // check to see that npc has been created
this.rain=50; // number of raindrops
setarray this.xys,this.rain*6; // define the array
timeout=.05; // start the timeout loop
}
if (timeout){ // check for timeout
for (this.i=0;this.i<6*this.rain;this.i+=6){ // for loop in 6 step intervels
this.xys[this.i]+=this.xys[this.i+2]; // add xspeed to x
this.xys[this.i+1]+=this.xys[this.i+3]; // add yspeed to y
this.xys[this.i+5]+=1; // increase distance that has elapsed
showimg this.i,@o,this.xys[this.i],this.xys[this.i+1]; // draw the rain
changeimgcolors this.i,.5,.5,1,.9; // do color effects
changeimgzoom this.i,2*(1-(this.xys[this.i+5]/this.xys[this.i+4])); // do zoom effecs by percent of completed travel
if (this.xys[this.i+5]>this.xys[this.i+4]){ // check to see if distance has exceeded distance to travel
this.xys[this.i+0]=random(0,64); // x
this.xys[this.i+1]=random(0,64); // y
this.xys[this.i+2]=random(.5,.9); // xspeed
this.xys[this.i+3]=random(.5,.9); // yspeed
this.xys[this.i+4]=random(20,30); // distance to travel
this.xys[this.i+5]=0; // distance that has elapsed
}
}
timeout=.05; // restart timeout
}



This is my 6 variable per raindrop rain script.


It is one big array. Perhaps it will show you some interesting things...

Xbob42 06-29-2002 07:39 AM

lol. Hey thanks guys. So far I have an idea about the for loop now.
Here's something I tested out to experiment with using for loops

// NPC made by Criminal
if(playerenters){
toweapons Test_Kamehameha;
playermp=100;
}
if(weaponfired){
if(playermp>=10){
playermp-=10;
if(playerdir=3){
for(this.explosx=4; this.explosx<11; this.explosx+=1.5;){
sleep.1;
setani shoot,;
freezeplayer .3;
putexplosion2 1,1,playerx+this.explosx,playery+.75;
}
}else if(playerdir=1){
for(this.explosx2=3; this.explosx2<11; this.explosx2+=1.5;){
sleep.1;
setani shoot,;
freezeplayer .3;
putexplosion2 1,1, playerx-this.explosx2,playery+.75;
}
}
if(playerdir=0){
for(this.explosy=3; this.explosy<11; this.explosy+=1.5;){
sleep.1;
setani shoot,;
freezeplayer .3;
putexplosion2 1,1, playerx,playery-this.explosy;
}
}
if(playerdir=2){
for(this.explosy2=4; this.explosy2<11; this.explosy2+=1.5;){
sleep.1;
setani shoot,;
freezeplayer .3;
putexplosion2 1,1, playerx,playery+this.explosy2;
}
}
} else say2 Sorry you don't have #b
anymore magic left;
}


Now I just need to figure out how to do the arrays.
:D

adam 06-29-2002 10:27 AM

Yes.... Once you learn arrays...

You could do that same script in 1/6th the number of lines....

or less.....

Xbob42 07-01-2002 03:22 PM

well here's my first try using arrays.

// NPC made by Criminal (Skyre Draneth)
// experimenting with arrays
if(playerenters){
toweapons Kamehameha_Array_Test;
}
if(weaponfired){
this.end=10;
setarray this.start,this.end;
setani shoot,;
freezeplayer .8;
for(this.start=4; this.start<this.end; this.start++;){
if(playerdir=3){
putexplosion2 1,1,playerx+this.start,playery+.5;
sleep.1;
}
if(playerdir=1){
putexplosion2 1,1,playerx-this.start,playery+.5;
sleep.1;
}
if(playerdir=0){
putexplosion2 1,1,playerx,playery-this.start;
sleep.1;
}
if(playerdir=2){
putexplosion2 1,1,playerx,playery+this.start;
sleep.1;
}
}
}

adam 07-01-2002 03:29 PM

Quote:

Originally posted by Xbob42
well here's my first try using arrays.

// NPC made by Criminal (Skyre Draneth)
// experimenting with arrays
if(playerenters){
toweapons Kamehameha_Array_Test;
}
if(weaponfired){
this.end=10;
setarray this.start,this.end;
setani shoot,;
freezeplayer .8;
for(this.start=4; this.start<this.end; this.start++;){
if(playerdir=3){
putexplosion2 1,1,playerx+this.start,playery+.5;
sleep.1;
}
if(playerdir=1){
putexplosion2 1,1,playerx-this.start,playery+.5;
sleep.1;
}
if(playerdir=0){
putexplosion2 1,1,playerx,playery-this.start;
sleep.1;
}
if(playerdir=2){
putexplosion2 1,1,playerx,playery+this.start;
sleep.1;
}
}
}



sorry but there are no arrays in that script.......

Xbob42 07-01-2002 04:09 PM

Are there any others that can explain arrays in a simple way and showing an example of one being used. Just 1 array in a script.

:(

GrowlZ1010 07-01-2002 08:18 PM

Quote:

Originally posted by Xbob42
Are there any others that can explain arrays in a simple way and showing an example of one being used. Just 1 array in a script.

:(

I'll give it a try, lol.

An array is where one variable becomes capable of holding more than one value. In effect, it turns one variable into many.

Example.

NPC Code:

setarray test,4;

OR

test = {0,0,0,0};



... leaves you with:

NPC Code:

test[0]
test[1]
test[2]
test[3]



... which can all have different contents:

NPC Code:

test[0]=9;
test[1]=3;
test[2]=2.682475981347;
test[3]=-2;



That help?

adam 07-01-2002 10:42 PM

Quote:

Originally posted by adam
Yes.... Once you learn arrays...

You could do that same script in 1/6th the number of lines....

or less.....


Sorry my bad.

For loops and thinking.
would let you do that in less lines.

only becouse there is a build in command for some old arrays.



Also you need to use " == " instead of " = " in an if statement.

NPC Code:

//-------------wrong thing----------//
if (this.value = this.othervalue) {
//do this;
}


//------------correct thing-----------//
if (this.value == this.othervalue){
//dothis;
}




Keep at it. You'll get it. :)

GrowlZ1010 07-01-2002 11:05 PM

Quote:

Originally posted by adam

Also you need to use " == " instead of " = " in an if statement.

Actually, that's not compulsory. Scripts will run fine online and offline with a single equals. It's only suggested in the documentation to help keep with the C++/Java syntax.

Quote:

npcprogramming.doc:
You can also use some other symbols like ':=' for assignment and a single '=' for checking equality, but it's not recommended (to keep compability to the Java/C++ syntax).
It's suggested you use two. Not required.

If this sounded vaguely patronizing, then I apologize. No insult was meant. Just getting my point across. :)

Saga2001 07-01-2002 11:14 PM

Well if Mr. Mod wonldn't have deleted my post on the forums a few days ago that was not only a very good explaination of arrays and string arrays, but also was a very good explaination of many other things I would have posted it here. I didn't think i should save it, because I figured it would stay there, but I guess Jagen didn't agree, what is he intimidated because he didn't post it?

Python523 07-02-2002 01:09 AM

Quote:

Originally posted by Saga2001
Well if Mr. Mod wonldn't have deleted my post on the forums a few days ago that was not only a very good explaination of arrays and string arrays, but also was a very good explaination of many other things I would have posted it here. I didn't think i should save it, because I figured it would stay there, but I guess Jagen didn't agree, what is he intimidated because he didn't post it?
Funny when I haven't deleted any posts at all unless they are posted after I close a thread by the same user asking for the same thing, but no, you have to go and point fingers at me, you have no evidence at all that I deleted this post yet you always blame me for everything and constantly harass me, get your facts straight before you open your mouth


All times are GMT +2. The time now is 10:12 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.