Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Stopping a For Loop

rubiq Campbell
Registered User
Join date: 4 Apr 2006
Posts: 22
09-29-2006 16:07
I am working on a game and I would like to stop a loop and reset the script of an object if I send a message to stop. For example:

for(i = 0; i < 10; i++)
{
Do something
}

The problem is I cannot stop the loop until it finish looping. Is it possible to send a message to quickly stop the loop and give the results. I would to reset everything anytime instead of waiting for the loop to finish. Is it possible? Lets say the loop loops 100000 times and would like to stop it lets say at 20000 or any other or just before it finishes looping.
DoteDote Edison
Thinks Too Much
Join date: 6 Jun 2004
Posts: 790
09-29-2006 16:32
One way I can think of is to include a check to some sort of variable or data external to the script itself. For instance, add a second script to the prim... Have that second script modify the prim's Description field when time to stop the loop. And of course, within the loop, compare the description field once per loop.
Aakanaar LaSalle
Registered User
Join date: 1 Sep 2006
Posts: 132
09-29-2006 22:16
Yea.. if you try to put in a listen or something in the original script, it won't get t that listen till after the loop is complete. any time you tell it to stop, it will queue that statement and get to it later.

Using a second script and adding something to the loop to check a description or other unused value may be the way to go, as stated in the post above. Then use a jump to exit the loop.
Osgeld Barmy
Registered User
Join date: 22 Mar 2005
Posts: 3,336
09-29-2006 23:35
CODE

integer x;

integer test = FALSE;

default
{
touch_start(integer total_number)
{
while (x < 10)
{
if (! test) // if x == FALSE
{
//llDoStuff();
llOwnerSay("true " + (string)x);
++x;

if (x == 2)
{
++test; //x = TRUE
}
}

else
{
x = 10;
llOwnerSay("false");
return;
}
}
}
}


works the same on a for loop ...

oh my bad i missed the message part, yea your pretty much SOL sry
Zeera Xi
Real Join Date: Mid '05
Join date: 21 Sep 2006
Posts: 54
09-30-2006 14:03
May I please lead you to this extremely rarely used but extremely yet useful flow controller?

http://www.lslwiki.com/lslwiki/wakka.php?wakka=jump

And anyone that says jumps are bad, think about this: Would you rather have the simulator process the rest of the code for an empty If statement? It is a waste of resources, so please go and die if you think that jumping out of loops is a stupid idea, because its better than leaving the loop continue processing empty statements.
Jillian Callahan
Rotary-winged Neko Girl
Join date: 24 Jun 2004
Posts: 3,766
09-30-2006 14:29
Maybe I missed something, but as LSL is an event based language with a state engine, why not something like this:
CODE
integer counter;

default
{
state_entry()
{
counter = 0;
}

touch_start(integer n)
{
if (llDetectedKey(0) == llGetOwner())
{
state countdown;
}
}
}

state countdown
{
state_entry()
{
counter = 0;
llListen(0, "", llGetOwner(), "");
llSetTimerEvent(0.25);
}

listen(integer chan, string name, key id, string msg)
{
if(llToLower(msg) == "stop")
{
llSetTimerEvent(0.0);
llSay(0, "stopped on " + (string)counter);
state default;
}
}

timer()
{
counter++;
if(counter > 9000)
{
llSetTimerEvent(0.0);
llSay(0, "Timed out");
state default;
}
}
}
_____________________
Ralph Doctorow
Registered User
Join date: 16 Oct 2005
Posts: 560
09-30-2006 14:33
Another scheme depending on how your code is organized would be for another listener script to just reset your looping script if the message arrives. This sounds pretty drastic I know, but it might be what you want.