Module timer
Timer management for Lua.
No dependency.
Usage:
TODO
Timer:onStart (func) |
Add a function to the start callback: will execute func(self, lag) when the function execution start. |
Timer:onUpdate (func) |
Add a function to the update callback: will execute func(self, lag) each frame the main function is run. |
Timer:onEnd (func) |
Add a function to the end callback: will execute func(self, lag) when the function execution end. |
Timer:chain (func) |
Creates another timer which will be replace the current one when it ends. |
-
new ()
-
Creates and return a new timer registry.
A timer registry provides an easy way to handle your timers; it will keep track of them,
updating and removing them as needed. If you use the scene system, a scene-specific
timer registry is available at ubiquitousse.scene.current.timer.
Returns:
TimerRegistry
-
run ([func])
-
Create a new timer that will run a function.
The function will receive as first parameter the timer object.
As a second parameter, the function will receive the delta time (dt).
As a third parameter, the function will receive the lag time (difference between the time when the function was run and when it should have been run).
As a fourth parameter, the function will receive as first parameter the wait(time) function, which will pause the function execution for time seconds.
You will need to call the :update(dt) method on the timer object every frame to make it do something, or create the timer from a timer registry if you
don’t want to handle your timers manually.
Parameters:
- func
function
the function to schedule
(optional)
Returns:
Timer
the new timer
-
tween (duration, tbl, to[, method="linear"])
-
Create a timer that will tween some numeric values.
You will need to call the :update(dt) method on the timer object every frame to make it do something, or create the timer from a timer registry if you
don’t want to handle your timers manually.
Parameters:
- duration
number
tween duration (seconds)
- tbl
table
the table containing the values to tween
- to
table
the new values
- method
string/function
tweening method (string name or the actual function(time, start, change, duration))
(default "linear")
Returns:
TweenTimer
the object. A duration is already defined, and the :chain methods takes the same arguments as tween (and creates a tween).
Timer methods.
-
Timer:after ([time])
-
Wait time seconds before running the function.
Specify no time to remove condition.
Parameters:
- time
number
duration to wait
(optional)
Returns:
Timer
the same timer, for chaining method calls
-
Timer:every ([time])
-
Run the function every time millisecond.
Specify no time to remove condition.
Parameters:
- time
number
duration between execution
(optional)
Returns:
Timer
the same timer, for chaining method calls
-
Timer:times ([count])
-
The function will not execute more than count times.
Specify no time to remove condition.
Parameters:
- count
number
number of times
(optional)
Returns:
Timer
the same timer, for chaining method calls
-
Timer:during ([time])
-
The timer will be active for a time duration.
Specify no time to remove condition.
Parameters:
- time
number
duration of execution
(optional)
Returns:
Timer
the same timer, for chaining method calls
Function conditions
-
Timer:initWhen (func)
-
Starts the function execution when func() returns true. Checked before the “after” condition,
meaning the “after” countdown starts when func() returns true.
If multiple init functions are added, init will trigger only when all of them returns true.
Parameters:
- func
function
function that returns true if condition is verified
Returns:
Timer
the same timer, for chaining method calls
-
Timer:startWhen (func)
-
Starts the function execution when func() returns true. Checked after the “after” condition.
If multiple start functions are added, start will trigger only when all of them returns true.
Parameters:
- func
function
function that returns true if condition is verified
Returns:
Timer
the same timer, for chaining method calls
-
Timer:repeatWhile (func)
-
When the functions ends, the execution won’t stop and will repeat as long as func() returns true.
Will cancel timed repeat conditions if false but needs other timed repeat conditions to be true to create a new repeat.
If multiple repeat functions are added, a repeat will trigger only when all of them returns true.
Parameters:
- func
function
function that returns true if condition is verified
Returns:
Timer
the same timer, for chaining method calls
-
Timer:stopWhen (func)
-
Stops the function execution when func() returns true. Checked before all timed conditions.
If multiple stop functions are added, stop will trigger only when all of them returns true.
Parameters:
- func
function
function that returns true if condition is verified
Returns:
Timer
the same timer, for chaining method calls
Conditions override
-
Timer:start ()
-
Force the function to start its execution.
Returns:
Timer
the same timer, for chaining method calls
-
Timer:stop ()
-
Force the function to stop its execution.
Returns:
Timer
the same timer, for chaining method calls
-
Timer:abort ()
-
Force the function to stop immediately. Won’t trigger onEnd or other callbacks.
Returns:
Timer
the same timer, for chaining method calls
-
Timer:skip (time)
-
Skip some amount of time.
Parameters:
- time
number
duration to skip
Returns:
Timer
the same timer, for chaining method calls
Callbacks functions
-
Timer:onStart (func)
-
Add a function to the start callback: will execute func(self, lag) when the function execution start.
Parameters:
- func
function
function to call when timer start
Returns:
Timer
the same timer, for chaining method calls
-
Timer:onUpdate (func)
-
Add a function to the update callback: will execute func(self, lag) each frame the main function is run.
Parameters:
- func
function
function to call at each timer update
Returns:
Timer
the same timer, for chaining method calls
-
Timer:onEnd (func)
-
Add a function to the end callback: will execute func(self, lag) when the function execution end.
Parameters:
- func
function
function to call when timer ends
Returns:
Timer
the same timer, for chaining method calls
Chaining
-
Timer:chain (func)
-
Creates another timer which will be replace the current one when it ends.
Returns the new timer (and not the original one!).
Parameters:
- func
function
function called by the chained timer
Returns:
Timer
the new timer
Management
-
Timer:update (dt)
-
Update the timer.
Should be called at every game update.
Parameters:
- dt
number
the delta-time (time spent since last time the function was called) (seconds)
-
Timer:dead ()
-
Check if the timer is dead.
You shouldn’t need to worry about this if your timer belongs to a registry.
If you don’t use registries, you probably should purge dead timers to free up some memory (dead timers don’t do anything otherwise).
Returns:
bool
true if the timer can be discarded, false if it’s still active.
Registry methods.
-
TimerRegistry:update (dt)
-
Update all the timers in the registry.
Should be called at every game update; called by ubiquitousse.update.
Parameters:
- dt
number
the delta-time (time spent since last time the function was called) (seconds)
-
TimerRegistry:run (func)
-
Create a new timer and add it to the registry.
Same as timer_module.run, but add it to the registry.
Parameters:
- func
function
function called by the timer
Returns:
Timer
the new timer
-
TimerRegistry:tween (duration, tbl, to, method)
-
Create a new tween timer and add it to the registry.
Same as timer_module.tween, but add it to the registry.
Parameters:
Returns:
TweenTimer
the new timer
-
TimerRegistry:clear ()
-
Cancels all the running timers in this registry.
Tween timer: inherit all fields and methods from
Timer and change a few to make them easier to use in a tweening context.
-
r:chain (duration, tbl, to[, method="linear"])
-
Creates another tween which will be initialized when the current one ends.
If tbl and/or method are not specified, the values from the current tween will be used.
Returns the new tween timer.
Parameters:
- duration
number
tween duration (seconds)
- tbl
table
the table containing the values to tween
- to
table
the new values
- method
string/function
tweening method (string name or the actual function(time, start, change, duration))
(default "linear")
Returns:
TweenTimer
the new timer