Skip to main content

Time and Scheduling

The tick event

The engine exports an event called Tick for the scripts to their logical processing in. This event gets called before the Render event and not as frequently. It gets passed the current tick counter as the argument. The event gets emitted at a constant frequency, which can be read from the global Engine.TICK_RATE.

-- Tick event emitted each tick.
Event<uint> Event.OnTick

-- Tick rate in Hz.
uint Engine.TICK_RATE

Scheduling actions

You can schedule a closure to be invoked after a certain amount of time by using the function Engine.Delay, or invoked at specified interval using Engine.Timer. If the callback returns true, timer will be erased.

-- Schedules a function to be ran before the next tick when most optimal.
-- - Aliases as 'queueMicrotask'.
void Engine.Defer(function<> cb)

-- Schedules a function to be ran after the specified delay, current time will be the only argument.
-- - Aliases as 'setTimeout' and 'setImmediate' with ms=0.
uint Engine.Delay(function<float time> cb, float milliseconds)

-- Schedules a function to be ran at the specified interval, current time will be the only argument.
-- If the callback returns true timer will be erased.
-- - Aliases as 'setInterval'.
uint Engine.Timer(function<float time> cb, float milliseconds)

-- Removes a previously scheduled closure from the queue, returns false if not found.
-- - Aliases as 'clearInterval/clearImmediate/clearTimeout'.
bool Engine.Cancel(uint key)


As an alternative to the standard Lua function os.clock the engine exposes the Engine.Time() function which reads the current timestamp as measured in milliseconds since the initialization of the environment in a fast manner and is accurate to nansecond scale. The engine namespace also exports the number of frames drawn since initialization, number of tick events emitted, and delta timings.

float      Engine.Time()          -- Returns the current timestamp
int Engine.GetTickCount() -- Returns the tick counter
int Engine.GetFrameCount() -- Returns the frame counter
float Engine.GetTickDelta() -- Returns the duration since the previous tick
float Engine.GetFrameDelta() -- Returns the duration since the previous frame
float Engine.GetFrameTime() -- Returns the timestamp for the beginning of the frame