Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

HELP: How can I order to stop a loop statement?

Seagel Neville
Far East User
Join date: 2 Jan 2005
Posts: 1,476
03-25-2005 17:06
Hello there,

I wrote the following's script.
If you say, "start", the object moves up and down forever.
CODE

integer continue = TRUE;

default
{
state_entry()
{
llListen( 0, "", NULL_KEY, "" );
}

listen( integer channel, string name, key id, string message )
{
if ( message == "start" )
{
do
{
llSetPos(llGetPos() + (<0,0,2> * llGetRot()));
llSleep(2.0);
llSetPos(llGetPos() - (<0,0,2> * llGetRot()));
llSleep(2.0);
}
while(continue);
}
else
{
// no action
}
}
}

Well then, I became to think that I wanted to stop this object
by saying "stop". But it didn't listen to me again because the
loop statement was between 'do' and 'while'. I couldn't include
the listen event between do-while.
What should I do?
Jeffrey Gomez
Cubed™
Join date: 11 Jun 2004
Posts: 3,522
03-25-2005 17:18
Loops will do that. Try this.

CODE

integer continue = TRUE;

default
{
state_entry()
{
llListen( 0, "", NULL_KEY, "" );
}
timer()
{
llSetPos(llGetPos() + (<0,0,2> * llGetRot()));
llSleep(2.0);
llSetPos(llGetPos() - (<0,0,2> * llGetRot()));
llSleep(2.0);
if(continue) llSetTimerEvent(0.1);
}
listen( integer channel, string name, key id, string message )
{
if ( message == "start" )
{
continue == TRUE;
llSetTimerEvent(0.1);
}
else
{
continue = FALSE;
}
}
}

The thing with loops is they're very hard to break out of by "interrupting" them with an event. A timer, on the other hand, gives you a window between calls of the timer. Try it. :D
_____________________
---
Buster Peel
Spat the dummy.
Join date: 7 Feb 2005
Posts: 1,242
03-25-2005 18:16
few problems with that --

when you set a timer, the timer will keep firing. You don't need to set it again inside the timer loop. It will fire every X seconds until you stop it by calling llSetTimerEvent(0), or until you change to a different state.

also, if you are using a timer, why use the sleep also.

try this:

CODE

integer direction = 1;

default
{
state_entry()
{
llListen( 0, "", NULL_KEY, "" );
}
timer()
{
llSetPos(llGetPos() + (<0,0,2> * llGetRot() * direction));
direction *= -1;
}
listen( integer channel, string name, key id, string message )
{
if ( message == "start" )
{
llSetTimerEvent(2);
}
else if ( message = "stop" )
{
llSetTimerEvent(0);
}
}
}
Jeffrey Gomez
Cubed™
Join date: 11 Jun 2004
Posts: 3,522
03-26-2005 14:57
Good points. I actually did not know timers worked like that, which is funny because I use them so commonly. Typically, however, the second timer call "Refreshes" your interval, which is why I generally add an "llSetTimerEvent(?)" in given areas, anyway. Pretty useful for synchronizing stuff per call.

Anyway, Buster is correct.
_____________________
---
Buster Peel
Spat the dummy.
Join date: 7 Feb 2005
Posts: 1,242
03-26-2005 15:25
It's usually not hard to improve somebody else's code ;)

Just don't look at mine. :eek:
Seagel Neville
Far East User
Join date: 2 Jan 2005
Posts: 1,476
03-27-2005 03:02
Thanks, Jeffrey and Buster.

I'm not sure why Jeffrey's one can't stop yet, but it enlightend me so much. :)
Buster's one worked well, but it seems to be too hard for me to get.

Thanks. :)
Seagel Neville
Far East User
Join date: 2 Jan 2005
Posts: 1,476
03-27-2005 03:28
I changed the last part of Jeffrey's one and worked well. :)
CODE
if ( message == "start" )
{
continue == TRUE;
llSetTimerEvent(1);
}
else if ( message == "stop" )
{
continue = FALSE;
llSetTimerEvent(0);
}
Seagel Neville
Far East User
Join date: 2 Jan 2005
Posts: 1,476
03-27-2005 03:39
Sorry for a lot of post, but I've realized that "continue" is just my code's remain.

So I wrote the following.
CODE

default
{
state_entry()
{
llListen( 0, "", NULL_KEY, "" );
}
timer()
{
llSetPos(llGetPos() + (<0,0,2> * llGetRot()));
llSleep(2.0);
llSetPos(llGetPos() - (<0,0,2> * llGetRot()));
llSleep(2.0);
}
listen( integer channel, string name, key id, string message )
{
if ( message == "start" )
{
llSetTimerEvent(1);
}
else if ( message == "stop" )
{
llSetTimerEvent(0);
}
}
}

It worked. :)
Tiger Crossing
The Prim Maker
Join date: 18 Aug 2003
Posts: 1,560
03-28-2005 08:20
You have two 2-second sleeps in the timer, but are calling the timer once every second. You might want to set the timer to 4 seconds so it's not trying to trigger faster than it can run.
_____________________
~ Tiger Crossing
~ (Nonsanity)
Frans Charming
You only need one Frans
Join date: 28 Jan 2005
Posts: 1,847
03-28-2005 13:00
Just one remark.

llListen( 0, "", NULL_KEY, "" );
this will call, the listen function on everything that is being said in the chat. Wich isn't necesary and slows the server a bit.
it would be better to change it to.
llListen(1,"",llGetOwner(),"";)
Now the listen function will only be called when the owner speaks in channel 1.
Graham Mondrian
Registered User
Join date: 16 Mar 2005
Posts: 59
03-31-2005 14:07
From: Tiger Crossing
You have two 2-second sleeps in the timer, but are calling the timer once every second. You might want to set the timer to 4 seconds so it's not trying to trigger faster than it can run.


Depending on how accurate the server measures time im gonna be annoying and suggest 4.5 to give room to execute those maths statements :)