Difference between revisions of "LlSetTimerEvent"

From Second Life Wiki
Jump to navigation Jump to search
m (added warning to LSL Tip about using llSetTimerEvent in combination with llFrand)
m (Replaced old <LSL> block with <source lang="lsl2">)
(5 intermediate revisions by 4 users not shown)
Line 2: Line 2:
|func_id=107|func_sleep=0.0|func_energy=10.0
|func_id=107|func_sleep=0.0|func_energy=10.0
|func=llSetTimerEvent|p1_type=float|p1_name=sec|p1_desc=Any positive non-zero value to enable, zero (0.0) to disable.
|func=llSetTimerEvent|p1_type=float|p1_name=sec|p1_desc=Any positive non-zero value to enable, zero (0.0) to disable.
|func_desc=Cause the [[timer]] event to be triggered a maximum of once every '''sec''' seconds. Passing in 0.0 stops further timer events.
|func_desc=Cause the [[timer]] event to be triggered a maximum of once every {{LSLPT|sec}} seconds. Passing in 0.0 stops further timer events.
|func_footnote
|func_footnote
|return_text
|return_text
Line 11: Line 11:
** '''[[LSL Delay#Events|Default event delay]]''' - Only so many events can be triggered per second.
** '''[[LSL Delay#Events|Default event delay]]''' - Only so many events can be triggered per second.
** '''Event Execution''' - If the execution of an event takes too long.
** '''Event Execution''' - If the execution of an event takes too long.
*The timer persists across state changes, but gets removed when the script is reset
*The timer persists across state changes, but gets removed when the script is reset. So if you change to a state that has a timer() event, with the timer still running, it will fire in the new state.
*Setting a new timer replaces the old timer and resets the timer clock.
**If you repeatedly call this function at some interval less than {{LSLPT|sec}} the timer event will never fire.
*The [[timer]] event is not an interrupt, it will not pause the execution of the currently running event to execute the timer. The current event must finish executing before the timer executes.
|constants
|constants
|examples=
|examples=
{{LSL Tip|Timers inside Second Life are either running, when you set the time inside the [[llSetTimerEvent]] function call to a positive float, or they are not running, when you set the time inside the [[llSetTimerEvent]] function call to 0.0. Each state in your script only has one timer, and timers keep running even when switching states.


For the sake of readability I personally tend to write [[llSetTimerEvent]]( (float)[[FALSE]] ); when stopping a timer, because I read it as "stop timer". Other scripters will argue though, that 0.0 is just fine and I'm crazy.
<source lang="lsl2">
 
Please take care when using something like [[llSetTimerEvent]]([[llFrand]](5.0)) in your code. There's a slight chance of [[llFrand]] returning 0.0 and that would stop your timer. Instead write [[llSetTimerEvent]](1.0 + [[llFrand]](4.0)) to get a random somewhere in {{Interval|gte=1.0|lt=5.0|lth=5.0|center=return}}. }}
<lsl>
// default of counter is 0 upon script load
// default of counter is 0 upon script load
integer counter;
integer counter;
float  gap = 2.0;


default
default
Line 27: Line 27:
     state_entry()
     state_entry()
     {
     {
        // llResetTime();
         llSetTimerEvent(gap);
 
         llSetTimerEvent(2.0);
     }
     }


     touch_start(integer total_number)
     touch_start(integer total_number)
     {
     {
        // PUBLIC_CHANNEL has the integer value 0
         llSay(0, "The timer stops.");
         llSay(PUBLIC_CHANNEL, "The timer stops.");
 
         llSetTimerEvent(0.0);
         llSetTimerEvent(0.0);
         counter = 0;
         counter = 0;
Line 44: Line 40:
     {
     {
         ++counter;  
         ++counter;  
         llSay(PUBLIC_CHANNEL,
         llSay(0,
             (string)counter+" ticks have passed in " + (string)llGetTime()  
             (string)counter+" ticks have passed in " + (string)llGetTime()  
             + " script seconds.\nEstimated elapsed time: " + (string)(counter * gap));
             + " script seconds.\nEstimated elapsed time: " + (string)(counter * gap));
     }
     }
}
}
</lsl>
</source>
<source lang="lsl2">
//  random period in between (+15.0, +30.0]
//  which means the resulting float could be 30.0 but not 15.0
    llSetTimerEvent( 30.0 - llFrand(15.0) );
//  same results for:
//  llSetTimerEvent( 30.0 + llFrand(-15.0) );
</source>
<source lang="lsl2">
// The timer event will never fire.
default
{
    state_entry()
    {
        llSetTimerEvent(5.0);
        // Sensor every 2.5 seconds
        llSensorRepeat("", NULL_KEY, FALSE, 0.0, 0.0, 2.5);
    }
 
    no_sensor()
    {
        llSetTimerEvent(5.0);
    }
   
    timer()
    {
        llSay(0, "I will never happen.");
        llSay(0, "Well, unless llSensorRepeat() magically finds something,"+
                    "or maybe there's 2.5+ seconds of lag and the timer()"+
                    "event queues.");
    }
}
</source>
|helpers
|helpers
|also_functions=
|also_functions=

Revision as of 13:06, 22 January 2015

Summary

Function: llSetTimerEvent( float sec );

Cause the timer event to be triggered a maximum of once every sec seconds. Passing in 0.0 stops further timer events.

• float sec Any positive non-zero value to enable, zero (0.0) to disable.

Caveats

  • The time between timer events can be longer, this is caused by:
  • The timer persists across state changes, but gets removed when the script is reset. So if you change to a state that has a timer() event, with the timer still running, it will fire in the new state.
  • Setting a new timer replaces the old timer and resets the timer clock.
    • If you repeatedly call this function at some interval less than sec the timer event will never fire.
  • The timer event is not an interrupt, it will not pause the execution of the currently running event to execute the timer. The current event must finish executing before the timer executes.
All Issues ~ Search JIRA for related Bugs

Examples

// default of counter is 0 upon script load
integer counter;
float   gap = 2.0;

default
{
    state_entry()
    {
        llSetTimerEvent(gap);
    }

    touch_start(integer total_number)
    {
        llSay(0, "The timer stops.");
        llSetTimerEvent(0.0);
        counter = 0;
    }

    timer()
    {
        ++counter; 
        llSay(0,
            (string)counter+" ticks have passed in " + (string)llGetTime() 
            + " script seconds.\nEstimated elapsed time: " + (string)(counter * gap));
    }
}
//  random period in between (+15.0, +30.0]
//  which means the resulting float could be 30.0 but not 15.0
 
    llSetTimerEvent( 30.0 - llFrand(15.0) );
 
//  same results for:
//  llSetTimerEvent( 30.0 + llFrand(-15.0) );
// The timer event will never fire.
default
{
    state_entry()
    {
        llSetTimerEvent(5.0);
        // Sensor every 2.5 seconds
        llSensorRepeat("", NULL_KEY, FALSE, 0.0, 0.0, 2.5);
    }

    no_sensor()
    {
        llSetTimerEvent(5.0);
    }
    
    timer()
    {
        llSay(0, "I will never happen.");
        llSay(0, "Well, unless llSensorRepeat() magically finds something,"+
                    "or maybe there's 2.5+ seconds of lag and the timer()"+
                    "event queues.");
    }
}

Notes

See Also

Events

•  timer

Functions

•  llSensorRepeat
•  llGetRegionTimeDilation
•  llGetTime

Deep Notes

Search JIRA for related Issues

Signature

function void llSetTimerEvent( float sec );