llGetTime

From Second Life Wiki
Revision as of 07:22, 8 March 2024 by Tapple Gao (talk | contribs) (Added a table of floating point precision thresholds)
Jump to navigation Jump to search

Summary

Function: float llGetTime( );
0.0 Forced Delay
10.0 Energy

Returns a float that is script time in seconds with subsecond precision since the script started, was last reset, or call to either llResetTime or llGetAndResetTime.

Specification

Script time matches normal time, it is unaffected by time dilation. For example, if you call llResetTime on two objects in separate simulators at the same time, and later call llGetTime on both at the same time, their values will be equal regardless of differences in dilation.

Caveats

  • Script time is the amount of real-world time that the script has been in a running state. It is unaffected by time dilation, but it does not count time while the script is suspended, the user is offline (when in an attachment), the object is in inventory rather than rezzed, etc.
  • Script time resets when...
  • Due to (32 bit) floating point number limitations, the accuracy of this function is 1/32sec up to ~3 days, 1/16sec up to ~6 days, etc... doubling each time, e.g. it's only 1 second at ~194 days. Use llResetTime or llGetAndResetTime whenever practical to maintain the accuracy you require:
Floating Point Precision Thesholds
Precision Seconds Hours Days
2-6=0.016 218= 262144 72.82 3.03
1/45=0.022 1 simulator physics frame
2-5=0.031 219= 524288 145.63 6.06
2/45=0.044 1 simulator script frame
2-4=0.063 220= 1048576 291.27 12.13
2-3=0.125 221= 2097152 582.54 24.27
2-2=0.250 222= 4194304 1165.08 48.54
2-1=0.500 223= 8388608 2330.16 97.09
2 0=1.000 224=16777216 4660.33 194.18

Examples

default {
    state_entry()
    {
        llResetTime();
    }
    touch_start(integer num_touch)
    {
        float time = llGetTime(); //Instead getting, and then resetting the time, we could use llGetAndResetTime() to accomplish the same thing.
        llResetTime();
        llSay(0,(string)time + " seconds have elapsed since the last touch." );
    }
}
// Distinguish between a single click and a double click

float gHoldTime;

default
{
    touch_start(integer total_number)
    {
        float now = llGetTime();
        if (now - gHoldTime < 0.3)
        {
            llSay(PUBLIC_CHANNEL,"Double clicked");
            // Trigger one sequence of actions
        }
        else
        {
            llSetTimerEvent(0.32);
        }
        gHoldTime = now;
    }
    
    timer()
    {
        llSetTimerEvent(0.0);
        if (llGetTime()-gHoldTime > 0.3)
        {
            llSay(PUBLIC_CHANNEL,"Single clicked.");
            // Trigger a different sequence of actions
        }
    }
}
//  To do time-dependant loops of whatever:
//  for example move 2 meters within 5.0 seconds

float time = 5.0;
float i;
llResetTime();
do
{
    i = llGetTime()/time;
    // move2meters*i
}
while (llGetTime() < time);

See Also

Deep Notes

Signature

function float llGetTime();