Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Looping (https://forums.graalonline.com/forums/showthread.php?t=18753)

ShockwaveISTHEBEST 12-12-2001 05:30 AM

Looping
 
How do you make something loop. For instance.

if (playerenters) {
hide;
sleep 2;
show;
sleep 2;
}

How would i loop it?
Thanks. =]

AlexH 12-12-2001 05:33 AM

use timeout

ShockwaveISTHEBEST 12-12-2001 05:38 AM

yes but could you help me out. I'm trying that, but i can't get it to work.

btedji 12-12-2001 05:47 AM

Re: Looping
 
Quote:

Originally posted by ShockwaveISTHEBEST
How do you make something loop. For instance.

if (playerenters) {
hide;
sleep 2;
show;
sleep 2;
}

How would i loop it?
Thanks. =]

NPC Code:

if (playerenters||timeout) {
hide;
sleep 2;
show;
sleep 2;
timeout=.05;
}



I will have timeouts in my script guide soon

ShockwaveISTHEBEST 12-12-2001 05:51 AM

thanks

T-Squad 12-12-2001 06:39 AM

Have you ever heard of timeout you complete *****. There is also while, which to loop that you could try sleep or break, but the falg you are using while with must be true for it to loop...

AlexH 12-12-2001 06:45 AM

timeout is easy
but while wouldnt work the way he done it
while (playerenters) just doesnt work

cell424 12-12-2001 06:20 PM

Very True. while isnt the best thing to use when using a script. You would have to set a flag and then use while (flagname) {script here; } and when you dont want it to loop any more unset the flagname.

Kumada 12-13-2001 01:15 AM

Re: Looping
 
Quote:

Originally posted by ShockwaveISTHEBEST
How do you make something loop. For instance.

if (playerenters) {
hide;
sleep 2;
show;
sleep 2;
}

How would i loop it?
Thanks. =]

BAHAHAHHAHHHHA
//#CLIENTSIDE
if (playerenters){
timeout=.05;
}

//#CLIENTSIDE
while (timeout){
hide;
sleep2;
show;
sleep .10;
}

Python523 12-13-2001 07:55 AM

Re: Re: Looping
 
Quote:

Originally posted by Kumada

BAHAHAHHAHHHHA
//#CLIENTSIDE
if (playerenters){
timeout=.05;
}

//#CLIENTSIDE
while (timeout){
hide;
sleep2;
show;
sleep .10;
}

The second //#CLIENTSIDE is unneccesary

btedji 12-13-2001 08:25 PM

timeout isnt a constant flag is set to true once only the moment it gets to 0 so a while loop with timeout wouldnt work

Xaviar 12-14-2001 12:08 AM

if (playerenters) timeout = .05;

if (timeout) {
if (visible) hide;
else
show;
timeout = 2;
}

mikepg 12-16-2001 12:42 AM

hmm
 
if possible, you want to stay away from sleep variables, because that stops everything in the NPC until the sleep is over. Try using something like this.sleep like this:

if (playerenters) {timeout = .1; show;
this.sleep=20;
this.on=1;
}

if (timeout) {
if (this.sleep>0) {this.sleep-=1;
}
if (this.sleep<=0) {this.sleep=20;
if (this.on=1) {this.on=0; hide;
}
if (this.on=0) {this.on=1; show;
}
}
timeout = .1;
}

That will make it so other flags can be unset/set while the sleep counter is going. If all you want to do in the npc is hide and show for some reason, use one of the previous scripts, other wise, if you have a lot of sleeping required, use this.vars so things are timed correctly.
also, since timeout is equal to 1/10th of a second, this.sleep=20 would wait 2 seconds, and then change. if the timeout was equal to .05 you might want to use 40 for this.sleep.

That about wraps it up.

Sennema 12-16-2001 12:52 AM

you could use:
NPC Code:

if (playerenters||timeout) {
hide;
sleep 2;
show;
timeout=2;
}



or, you could go for the less convenient option...

NPC Code:

if(strequals(#L,levelname)){
set onlevel;
}
while(onlevel){
hide;
sleep 2;
show;
sleep 2;
}



But the second is pointless. Use the first.

kyle0654 12-16-2001 01:40 AM

oye...why must idiots insult people who can't script as well as them and haven't done anything to deserve harsh words?

anyway...


timeout loops
NPC Code:

if (playerenters||timeout) {
hide;
sleep 2;
show;
sleep 2;
timeout = .05;
}



Timeout will make the timeout counter count down to 0 if you set it to anything above 0, and when it gets to 0, it will trigger a flag called timeout on the npc. If you place a timeout at the end of an if (timeout) statement, then it will loop.


while loops
NPC Code:

if (playerenters) {
while (1==1) {
hide;
sleep 2;
show;
sleep 2;
}
}



While is just like if, except when it finds a } it will go back to the while () and check the statement again. If the conditions in the brackets are still true, it will do the loop again. Otherwise, it will jump back to the } and continue with the script.

Above, I have while (1==1), which will create an infinite loop.


for loops
NPC Code:

if (playerenters) {
for (i = 0; i>=0; i++) {
hide;
sleep 2;
show;
sleep 2;
}
}



For is very useful, and most loops you encounter (that aren't timeout loops) will use for. If you have read the commands.rtf, you should notice that the syntax for for is something like:

for (initial; condition; increment) {

Now, the loop above could be written like this, and work the same way:
NPC Code:

if (playerenters) {
i = 0;
while (i>=0) {
hide;
sleep 2;
show;
sleep 2;
i++;
}
}


And, if you look at the debugger while playing (F6), you'll notice that your for loops are changed into while loops when they're processed by the scripting engine. The reason you use for though is because it's much easier to write, and is harder to mess up with.

The best advantage with for loops is that you can use the variable that is looping in your scripting. So, you could loop through an entire array and add all the numbers like this:
NPC Code:

if (playerenters) {
sum = 0;
myarray = {0,13,16,64,34};

for (i = 0; i < arraylen(myarray); i++) {
sum += myarray[i];
}

message #v(sum);
}


Now, I'll assume you don't know arrays since you're asking about loops, but just remember that loops can be very useful with arrays when you get to them.

Xaviar 12-16-2001 03:35 AM

Quote:

Originally posted by Xaviar
if (playerenters) timeout = .05;

if (timeout) {
if (visible) hide;
else
show;
timeout = 2;
}


ShockwaveISTHEBEST 12-19-2001 04:34 AM

Damn Kyle your good.

Saga2001 12-19-2001 11:54 AM

ummmm...
 
actually when it hits 0 it becomes true until a timeout is set again meaning this:
NPC Code:

if (playerenters) timeout=.05;
while (timeout) {
actions...
sleep .05;
}


would work...

ShockwaveISTHEBEST 12-20-2001 11:52 PM

Oh I see thanks

Warcaptain 12-21-2001 11:44 AM

Quote:

Originally posted by cell424
Very True. while isnt the best thing to use when using a script. You would have to set a flag and then use while (flagname) {script here; } and when you dont want it to loop any more unset the flagname.
not really....
flag just means an event to call it.
NPC Code:

i=0;
while (i<10){
commands;
i++;
}



is same as
[code]
while (i=0;i<10;i++;){
commands;
}
[/codes]

its just a lengthier version :)
same as

NPC Code:

if (i<10){
this.var=this.var+1;
}else this.var=0+1;


same as
NPC Code:

this.var=this.var%10+1;


kyle0654 12-21-2001 12:48 PM

Not every flag triggers an NPC...in fact, many don't. If you callnpc it will though.

nyghtGT 12-21-2001 05:38 PM

ohh and if he did not know:
NPC Code:

NPC Code:

i=1;
while (i>=0) {
i++;
}

is the same as (does the same thing as):

for (i=0;i>=0;i++) {}




:D

btedji 12-21-2001 08:22 PM

Quote:

Originally posted by nyghtGT
ohh and if he did not know:
NPC Code:

NPC Code:

i=1;
while (i>=0) {
i++;
}

is the same as (does the same thing as):

for (i=0;i>=0;i++) {}




:D

Your not setting i to the same thing both times.

ShockwaveISTHEBEST 12-22-2001 01:34 AM

How the hell did you guys learn to script I suck and then your all here putting leet scripts all over forums. I feel so inferior


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

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