Jump to content

The timer loop, how it works


Jenny Hispidus
 Share

You are about to reply to a thread that has been inactive for 1131 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

When I use timer to have the script count down to an event, and I want to do other things as well in the intervening time, how would I accomplish that?

For instance, I have a script that counts down to 60 seconds, but i'd like the object to also listen on a channel for another command, that might cause it to use llSay, or bail out of the timed event. I dont know how the logic works.

Link to comment
Share on other sites

llSetTimerEvent(float time) is the function you use to set up a timer. Every 'time' seconds it will activate the timer EVENT.

To stop a timer you would just do llSetTimerEvent(0.0); or to change it ot another time you would just do the llSetTimerEvent() function again with a different time. The event will keep activating every 'time' seconds until its stopped though.

 

Basically, start it with llSetTimerEvent(60.0); to have it activate 60 seconds later, and to stop it you would use llSetTimerEvent(0.0);

If you want it to do something every second you would need a global variable integer set to 60, a timer for every second, and just do

llSay(0, "COUNTER AT: " + (string)(--varInt));

if(!varInt){ we're all done } 

and to reset it you would stop the timer AND set varInt back to 60.

Link to comment
Share on other sites

You can multiplex a timer as much as you like. This timer manages actions every second, every ten seconds, and every minute, stopping in 5 minutes or when you click it.

integer count;integer ON;default{	touch_start(integer total_number)	{		llSetTimerEvent(1.0* (ON = !ON)); //Turns timer ON and OFF	}	timer()	{		++count;		llSay(0,"The current count is " + (string)count);		if(count%60 == 0)		{			llSay(0,"This message is sent once a minute");		}		if (count%10 == 0)		{			llSay(0,"This message is sent every ten seconds.");		}		if(count == 300)		{			llSetTimerEvent(0.0); // Timer shut off in five minutes		}	}}

 

  • Like 1
Link to comment
Share on other sites

I appreciate the question but that's not what I meant.

What if I want something to happen while the timer is ticking that has nothing to do with the timer?

One example being .... I say "turn off!", but another example would be listening and repeating the string as a shout or something.

Link to comment
Share on other sites

If you use a timer, the script will do other things while the timer event isn't executed - in fact, if you want to do things between two executions of the 'loop', you have to use a timer. If you use loops like for, while .., nothing else will be done - no events are triggered while a loop loops.

so in the following example, if the owner says "stop" on channel 1, it would stop the counter

integer giCounter;default {	state_entry()  {		llSetTimerEvent(5);		llListen(1, "", llGetOwner(), "stop");	}		listen(integer channel, string name, key id, string message) {		llSetTimerEvent(0);	}		timer() {		llOwnerSay("I am at: " +(string)giCounter);	}	}

 Whereas in this example it wouldn't:

integer giRun;default {	state_entry()  {		llListen(1, "", llGetOwner(), "stop");		integer counter;		giRun = 1;		while(giRun) {			llOwnerSay("I am at: " + (string)counter);
++counter;
llSleep(5); } } listen(integer channel, string name, key id, string message) { giRun = 0; }}

 

 

Link to comment
Share on other sites

SL scripts are event-driven.  Your timer is an event.  timer()  gets executed only when a timeout happens.  The timeout is the event, as the timer(0 function is called when the timer expires.   listen(), link_message() and other events such as changed() , can occur asynchronously to other events.  Only one event can be executing at a time. 

 

Anything inside of default:

default() {

  <HERE>// code in <here> are events

}

 

is an event.  And example is timer()   Events operate when some kind of trigger associated with that event happens.

As a rsult of this Your timer will operate independently the other events.    The other posts indicate some possible ways to get the timer to 'bail out'.   The simplest possible  way is to llSetTimerEvent(0) in any other event to stop the timer event from happening

 

Link to comment
Share on other sites

  • 9 years later...

If it's a for loop or a while loop that has a counter (index) that is compared against and upper limit (indexMax), then inside the loop, set index to be equal to or greater than indexMax and at the next test in a for loop, the loop will terminate. If it's a do.. while or while loop that tests for a condition being true or false, set the variables to that at the next test the condition for stopping will be met.

If it's an infinite loop, pulling the plug is the way to go for volatile memory, otherwise it's quite a drastic process.

Link to comment
Share on other sites

On 3/21/2021 at 9:07 AM, childhoodbestfriend489 said:

How to stop this loop?

What loop?  In the context of this thread, script execution is not IN the timer event except in the instant after it is triggered by llSetTimerEvent.  That is, if you say llSetTimerEvent(60.0) somewhere in your script, absolutely nothing related to the commands in the timer event will happen until precisely 60 seconds later.  Up to that instant, your script can do all sorts of other things including shutting the timer off with llSetTimerEvent(0.0) or resetting it to a different period. The script is not in a loop in the strict sense that it would be if you used a flow control option like for or while or do while.  If you do not issue a new llSetTimerEvent command, it will keep triggering the timer event every 60 seconds -- which is sort of loopy, I'll admit -- but between triggerings execution can go almost anywhere you like.  

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1131 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...