Don't click here unless you want to be banned.

LSL Wiki : timer

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are ec2-184-73-7-143.compute-1.amazonaws.com
timer()

The timer event is raised at regular time intervals set by the llSetTimerEvent function (you only need to call this function once).

Note: an event will never run while another event is still executing. The timer event will get queued and be executed after the previous events are dequeued. In other words, llSetTimerEvent only specifies the amount of time that must pass before a timer event is put into the event queue, which is not necessarily the same as the amount of time before the next timer event actually runs.

The timer event interval can be changed randomly each time it's called by putting:
llSetTimerEvent(llFrand(#));
in the timer event itself, where "#" is the upper value. llFrand will return a random result between 0 and #.

default {
    state_entry() {
        llSetTimerEvent(1.0); // generate a timer event every 1 second
    }
    
    timer() {
        llSetColor(<llFrand(1.0),llFrand(1.0),llFrand(1.0)>,ALL_SIDES); // set a random color
    }
}

Specifics:
When a timer event is specified, it does not immediately fire; rather, it waits the allotted amount of time before triggering the first timer event. So, if llSetTimerEvent(3600); it must wait one hour (60 minutes = 60 seconds x 60 = 3600 seconds) for it to trigger the first event.

To trigger the event once before waiting the time set, create a function with what it is to do, call it, call llSetTimerEvent, and then call the function in the timer event:

neTimed()
{
    llSetColor(<llFrand(1.0),llFrand(1.0),llFrand(1.0)>,ALL_SIDES); // set a random color
}

default {
    state_entry() {
        neTimed();
        llSetTimerEvent(1.0); // generate a timer event every 1 second
    }
    
    timer() {
        neTimed();
    }
}

The way the timer event works is: wait, do event, wait, do event, wait, ...
The way the above script works is: do event, wait, do event, wait, do event, ...

For the timer to do something different depending on some condition, use an if statement that checks a global variable. The following example uses a touch event to set the global variable that the timer event checks:

integer on = 1; // set "on" flag to 1 (on)
float time = 1; // timer interval

default
{
    state_entry()
    {
        llSetTimerEvent(time); // generate a timer event every 1 second
    }
    
    timer()
    {
        llOwnerSay("on");
    }

    touch(integer touches)
    {
        if (on == 1)
        {
            on = 0;
            llSetTimerEvent(0);
            llOwnerSay("off");
        }
        else
        {
            on = 1;
            llSetTimerEvent(time);
        }
    }
}
Note: the second if statement is only necessary in a touch event if there is also an if-else in the timer event; it won't work as else otherwise, oddly. However, simply remove the timer's if-else and code as above and this won't be a problem.

Events | Time
Comments [Hide comments/form]
What do you mean by "an event will never run while another is still executing"? Isn't all code in a script part of an event handler?
-- GunzourYellowknife (2004-06-26 19:59:44)
It means that the timer event won't interrupt the running event - it'll simply queue a timer event for execution after the current one is done. If there is no event being handled currently, the timer event will get executed right away.
-- EzharFairlight (2004-06-26 20:13:33)
Is it possible to call 2 timers that call each one a specific function ?
For exemple, a first function that would be called each 10 seconds to change the shape of the object, and a second one that would change it's color each 3 seconds.
-- ShineScript (2007-01-18 07:09:49)
You can only have one timer event active in a script. You could trigger every 3 seconds and also have a global var that is incremented on each timer event so after 9 seconds (3 x 3sec) the other things was done.
-- 82-32-72-155.cable.ubr02.bath.blueyonder.co.uk (2007-04-23 01:38:47)
Is there a way to stop a timer event?
-- cpe-75-80-136-147.san.res.rr.com (2007-11-01 06:48:05)
You stop a timer event by calling it every zero seconds:
llTimerEvent(0);
-- 184.sub-75-199-245.myvzw.com (2007-12-17 10:31:50)
oops.... brain faster than fingers

llSetTimerEvent(0);
-- 184.sub-75-199-245.myvzw.com (2007-12-17 10:33:20)
Attach a comment to this page: