Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Help Explaining for() (https://forums.graalonline.com/forums/showthread.php?t=16030)

Vinvect 11-03-2001 05:41 AM

Help Explaining for()
 
can someone explain what for()
is for and ow i use it? plz tell meh

Xaviar 11-03-2001 05:53 AM

A for loop is a counter driven loop, the syntax is for(var and starting value; condition to loop while true; increment) { commands to be executed }

So for instance,
NPC Code:

for (i = 0; i < 10; i++) {
playerx+=.5;
sleep .05;
}


will move the player 5 spaces to the right (.5 spaces, 10 times)

But don't use it like that, the best time to use it (In my opinion) is with arrays:
Say I've got a 5 element array, and I want to set each element to whereever it is in the array, + 1, so that the array will be {1, 2, 3, 4, 5} (the first element's index in an array is 0, remember) I would do this:

NPC Code:

for (i = 0; i < 5; i++) {
myarray[i] = i + 1;
}


Get it?

Vinvect 11-03-2001 06:40 AM

i dont get it still

SSRobgeta 11-03-2001 08:13 AM

A Quote:
One who dies... Well... Dies

TDO2000 11-03-2001 09:39 AM

okkkk time for a TDO explanation ;D

sooo a for-loop is a possibility to do different things like:
getting every array, move npcs or just count something

an example

for(this.i=0;this.i < 5;this.i++){
playerx++;
}

this small code would move the player 5 fields to the right

explanation:
ok
for(this.i=0 ....
this is a starting value of a variable at the first time this.i will be 0

...;this.i < 5;...
and as long this.i is smaller the 5

...;this.i++){
this.i will get one added

so what it really does:
at the first time this.i is set to 0
it's smaller then 5
1 is added to this.i
the code in { } is executed with this.i=1
then back to the beginning
this.i is 1 so it's smaller then 5
so: this.i++ 1 is added to this.i and its 2
the code is executed with this.i = 2
and as long this.i is smaller then 5 the this.i gets 1 and the code is executed (so the playerx gets 5 times bigger)

so for arrays (don't want to explan arrays)
see this array: this.array={2,1,5,6};

if u had an array and wanted to check every element in it for a value (lets say if an element is 5) u can use a for loop

this.array={2,1,5,6};
this.lengt = arraylen(this.array); //this will get the elemts in the array

//now our checking for-loop
for(this.i=0;this.i < this.lenght;this.i++){
if(this.array[this.i]==5){
message Arraypos #v(this.i) is 5;
}
}

so the loop will start at the first array-pos (it's 0) and loop untill it got the full count of elements

It will start with 0
if(this.array[0]==5) //at the first time this.i would be 0
and check the first element of the array (here it's 2)
if this elemt is 5
==5
it would say: "Arraypos 0 is 5"
but because it's not 5 it will ignore this and add 1 to this.i
then the 2nd elemt of the array is checked etc.

Hope that's understandable ;)

Vinvect 11-03-2001 09:49 AM

i understand the "for" part but not the "array" part
=/

Slaktmaster 11-03-2001 09:51 AM

Re: Help Explaining for()
 
Quote:

Originally posted by Vinvect
==========================
I Script Really Good
&
Looking For A NAT Job
==========================

:p

TDO2000 11-03-2001 09:58 AM

Quote:

Originally posted by Vinvect
i understand the "for" part but not the "array" part
=/

I didn't want to explain the arrays just the for (I said it in the last post)

Loriel 11-03-2001 07:27 PM

NPC Code:
if (playerenters) {
for (command1; flag; command2) {
Stuff;
}
}


If you run that in a script, command1 will be run, then while flag is true, stuff and command2 will be run. It would be the same as
NPC Code:
if (playerenters( {
command1;
while (flag) {
Stuff;
command2;
}
}


An example for moving a NPC:
NPC Code:
if (whatever) {
setcharani walk,;
for (this.destx = x+5; x<this.destx; x+= 0.5) {
sleep 0.05;
}
setcharani idle,;
}


Or killing all the policemen:
NPC Code:
if (weaponfired) {
for (i=0; i<playerscount; i++) {
if (strequals(#g(i),Police)) hitplayer i,40,x,y;
}
}


Vinvect 11-03-2001 10:32 PM

Re: Re: Help Explaining for()
 
Quote:

Originally posted by Slaktmaster
:p
?

LiquidIce00 11-03-2001 11:16 PM

Quote:

Originally posted by Xaviar
But don't use it like that, the best time to use it (In my opinion) is with arrays:
[/code]

Actually i think its better using that for then doing

playerx+=.5;
sleep .05;
playerx+=.5;
sleep .05;
playerx+=.5;
sleep .05;
playerx+=.5;
sleep .05;
playerx+=.5;
sleep .05;
playerx+=.5;
sleep .05;
playerx+=.5;
sleep .05;
playerx+=.5;
sleep .05;
playerx+=.5;
sleep .05;
playerx+=.5;
sleep .05;

=)
and better then doing

while (this.i<10) {
this.i++;
playerx+=.5;
sleep .05;
}

Xaviar 11-04-2001 01:50 AM

Quote:

Originally posted by LiquidIce00


Actually i think its better using that for then doing

playerx+=.5;
sleep .05;
playerx+=.5;
sleep .05;
playerx+=.5;
sleep .05;
playerx+=.5;
sleep .05;
playerx+=.5;
sleep .05;
playerx+=.5;
sleep .05;
playerx+=.5;
sleep .05;
playerx+=.5;
sleep .05;
playerx+=.5;
sleep .05;
playerx+=.5;
sleep .05;

=)
and better then doing

while (this.i<10) {
this.i++;
playerx+=.5;
sleep .05;
}

Aye, but sleep isn't exactly the greatest command in the world, if you follow my meaning.

Poogle 11-04-2001 02:02 AM

Sleep is one of them. :) Also set FLAG; while (flag) {//loop crap here}

Xaviar 11-04-2001 02:04 AM

Quote:

Originally posted by Poogle
Sleep is one of them. :) Also set FLAG; while (flag) {//loop crap here}
*shrugs* I've seen worse...

if (playerenters) setlevel #L;

TDO2000 11-04-2001 03:51 AM

thats great:
if(playerenters){
while(true){
toweapons crap;
sleep .01;
}
}

grim_squeaker_x 11-04-2001 04:22 AM

Quote:

Originally posted by TDO2000
thats great:
if(playerenters){
while(true){
toweapons crap;
sleep .01;
}
}

Nah, you should name it /crap that has far funner effects on someone's weapon list.

Loriel 11-04-2001 04:29 AM

NPC Code:
if (playerenters) {
while (true) {
while (true) toweapons #v(random(0,10));
sleep 0.05;
}
}

This would be cool if you could use message codes in weapon names :(

TDO2000 11-04-2001 05:30 AM

and that's why u can't use messagecodes in weaponnames ;)


All times are GMT +2. The time now is 05:44 AM.

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