A small immediate mode timer module that makes it easier to have an action execute with a certain interval or after a delay.
The step.lua file should be dropped into an existing project and required by it.
step = require "step"
You can then use step
to create a new timer.
You create a new timer with step.new(time)
or step.every(time)
. When updated, the timer will return true
with time
as the interval.
timer = step.every(3)
Update the timer with timer:update(dt)
or by calling your timer (timer(dt)
) and pass the delta time as argument.
if timer:update(dt) then
print("This will be printed every 3 seconds.")
end
If you create a new timer with step.after()
the timer will return true
every frame after the timer reaches its set time.
timer = step.after(3)
if timer:update(dt) then
print("After 3 seconds this will be printed every frame.")
end
If you create a new timer with step.once()
the timer will return true
on the frame the timer reaches its set time. After that it will return false and the timer needs to be manually reset.
timer = step.once(3)
if timer:update(dt) then
print("After 3 seconds this will be printed once.")
end
If you create a new timer with step.during()
the timer will return true
on every frame that the timer has not reached its set time. After that it will return false and the timer needs to be manually reset.
timer = step.during(3)
if timer:update(dt) then
print("This will be printed every frame until 3 seconds have passed")
end
You can always reset your timer with timer:reset()
or simply calling your timer without arguments.
timer:reset()
timer()
If you pass two numbers timer.new(lowest, highest)
it will have a random length (with decimals) between those numbers after each reset.
timer = step.new({3, 5})
You can use timer:set(t, [noreset])
to change the time for this timer. If noreset
is true
, the timer will not be reset.
timer = step.new(5)
timer:set(3)
You can use timer:finish()
to set the timer to the end, meaning that on the next update with step.every
it will return true
and reset the timer.
timer = step.new(4.7)
timer:finish()
if timer(dt) then
print("On the first frame this will be printed and then again after 4.7 seconds.")
end
This library is free software; you can redistribute it and/or modify it under the terms of the MIT license. See LICENSE for details.