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

LSL Wiki : timer

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are crawl809.us.archive.org
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
There are 7 comments on this page. [Display comments/form]