Graal Forums

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

GoZelda 09-26-2003 10:45 PM

Explaination of for
 
Well, it seemed to me that some people are very unfamiliar with for.

This is what for actually is:
NPC Code:

i = 0;
while (i<50){
i++;
)



So, for works like this:

Quote:

First, there's the "statement". This is, i = 0;.
for (i=0; ----){}
then, comes a "test".
i<50;
for (i=0;i<50){}
and then an "operation"
for (i=0;i<50;i++){}
So, i becomes zero. As long as i is less then 50, 1 will be added to i, until it becomes 50 (or more).
Note: never use

NPC Code:

for (i=0;i<1;i--){}


if you don't want an uber-lagged level with an infinite for-loop.

So, once more:

Quote:

I becomes equilevant to zero. As long as i is less then 50, then there's added one to i.
Ofcourse, you can add much more to a loop.

NPC Code:

for (i=17;i>0;i--){}


for example. I hope you see all possibilities of i. Here's a nice example:

NPC Code:

if (timeout||playerenters){
this.savex=0;
for (i=1;i<64;i++){
showpoly 2,{this.savex,32,i,32};
this.savex=i;
sleep 0.05;
}
timeout = 0.05;
}


Python523 09-26-2003 11:43 PM

Re: Explaination of for
 
Quote:

Originally posted by GoZelda

NPC Code:

for (i=0;i<1;i--){}


if you don't want an uber-lagged level with an infinite for-loop.
[/CODE]

Not exactly, serverside it'll give you an error message sent to the NC if the loops excedes 10000 runs after it's done and stop the loop from going further, it wont lag clientside unless you do something to cause it to lag, for(i=0; i<1; i--) wont cause the level to lag on it's own without you doing anything

GoZelda 09-27-2003 12:09 PM

I'm not so familiar with //#CLIENTSIDE and serverside, does anybody know any good tutorial?

Kaimetsu 09-27-2003 12:13 PM

Your 'nice' example is quite the opposite. If you're attempting to teach people then you should indent, and you shouldn't encourage them to do icky things like putting sleeps in loops.

GoZelda 09-28-2003 07:51 PM

1 Attachment(s)
But Kaimetsu, if i were to create a NPC that would make the level slightly darker every 0.05 seconds, then wouldn't a for loop with sleep be the best solution?
Oh yeah, here's some strange stuff i did with for loops and polygons o.o

Dach 09-28-2003 09:00 PM

use a timeout instead

GoZelda 09-28-2003 10:26 PM

Yes, but what if i'd wanted to repeat my action? In my "nice" example above, it'd repeat without trouble. How would it work with timeout? You can save some trouble with a sleep for loop. And i don't really see what's wrong with sleep :/
NPC Code:

if (playerenters){
i=1;
this.savex=0;
timeout = 0.05;
}
if (timeout){
if (i<64){
i++;
showpoly 2,{this.savex,32,i,32};
this.savex=i;
} if (i==63){
i=1;
this.savex=0;
}
timeout = 0.05;
}


Kaimetsu 09-28-2003 11:02 PM

Quote:

Originally posted by GoZelda
But Kaimetsu, if i were to create a NPC that would make the level slightly darker every 0.05 seconds, then wouldn't a for loop with sleep be the best solution?
No.

Quote:

Originally posted by GoZelda
Yes, but what if i'd wanted to repeat my action? In my "nice" example above, it'd repeat without trouble. How would it work with timeout? You can save some trouble with a sleep for loop. And i don't really see what's wrong with sleep :/
Too many things to list. If you can't think of an easy way to do it with timeout, you shouldn't call yourself a scripter.

protagonist 09-29-2003 12:06 AM

Quote:

Originally posted by GoZelda
But Kaimetsu, if i were to create a NPC that would make the level slightly darker every 0.05 seconds, then wouldn't a for loop with sleep be the best solution?
Oh yeah, here's some strange stuff i did with for loops and polygons o.o

No way man, it'd be like a flash.

zs0 09-29-2003 12:10 AM

Stupid advanced scripting :/..

protagonist 09-29-2003 12:25 AM

Quote:

Originally posted by zs0
Stupid advanced scripting :/..
It's not very advanced.
It works like this:

for(a;b;c){ stuff (optional); }


a. The variable, and what you want it to start as (for example a = 0)
b. The condition that has to happen when you want c to happen.
c. The change to a variable that you want to happen (NOTE: Doesn't have to be the same variable as a, but be careful not to infinite for loop. In other words, where the optional place is you have to make it catch the variable at some point and stop the for loop. Otherwise you will crash your graal).

GoZelda 09-29-2003 02:38 PM

Quote:

Originally posted by Kaimetsu


No.



Too many things to list. If you can't think of an easy way to do it with timeout, you shouldn't call yourself a scripter.

Okay, the level-darkening stuff isn't hard with timeout.
But, Kai, can you please explain me what is wrong with sleep?

Kaimetsu 09-29-2003 03:52 PM

Quote:

Originally posted by GoZelda

Okay, the level-darkening stuff isn't hard with timeout.
But, Kai, can you please explain me what is wrong with sleep?

Just trust me on this one, dude.

I'll probably add a mention to KSI-GS at some point, you can read it then.

GoZelda 09-29-2003 04:09 PM

Quote:

Originally posted by Kaimetsu


Just trust me on this one, dude.

I don't doubt you're right, really.
If you were wrong, you would've been jumped and raped totally by now -.-


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

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