Jump to content
Sign in to follow this  
Jenny Hispidus

The timer loop, how it works

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.

Share this post


Link to post
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.

Share this post


Link to post
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		}	}}

 

Share this post


Link to post
Share on other sites

You just have something in the Listen event change the time, it will stop. So, just use llSetTimerEVent(0.0) after you listen event set of checks, word verification or whatever.

if(Stop = TRUE){llSetTimerEvent(0.0);} //switch off timer.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

you do those as normal... that's the beauty of timer loops, they don't stay in the event between triggering... which lets other code run normally.

Share this post


Link to post
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; }}

 

 

Share this post


Link to post
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

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...