This is a timer library for Love2D.
Allows you to create, control and keep all created timers in one place. Can also be used anywhere by altering timerManager.lua
- Timer manager for Love2D
git clone https://github.com/msun-dev/lua_love2d_timerManagerRemove all unneeded files afterwards.
Or use something like downgit to download a single folder.
In timerManager.lua change line #47:
47: local s = love.math.random(0, 0xf) -- This line should be changedFor example, using lua math library you can do this:
+4: local seed = os.time() -- Add it before all initializations
47: local s = math.random(0, 0xf) -- Change to your preferred methodTimers related documentation. How timers and timerManager work, methods, how to initialize, etc. Everything that is somewhat related to timers.
Main object that executes user-defined callback after some amount of time. Used by timerManager.lua.
uuid- Default value:"". Value generated when initialized by timer manager.time_left- Default value:0. Shows the time left untilcallbackexecution.stopped- Default value:true. Becomestruewhentime_leftreaches 0. Timer doesn't update whenstoppedequalstrue.
duration- Default value:1. Duration of the timer in seconds.autostart- Default value:true. Iftrue, the timer will start automatically on initialization.repeating- Default value:false. Timer will automatically start again aftertime_leftreaches 0.autoremove- Default value:false. Timer manager will automatically remove this timer if this field is set totrueandtime_leftreaches 0.callback- Default value:false. The timer automatically executes this function whentime_leftreaches 0.
-
.new(settings)- Description: Creates new timer object.
- Parameters:
settings- Settings table for timer. See timers user fields for possible options. - Returns:
table
-
:get_timeleft()- Description: Returns value of
time_leftfield. - Parameters:
none - Returns:
number
- Description: Returns value of
-
:get_duration()- Description: Returns value of
durationfield. - Parameters:
none - Returns:
number
- Description: Returns value of
-
:get_uuid- Description: Returns value of
uuidfield. - Parameters:
none - Returns:
string
- Description: Returns value of
-
:set_duration(value)- Description: Sets the duration of a timer.
- Parameters:
value- duration in seconds. - Returns:
nothing
-
:is_stopped()- Description: Returns value of
stoppedfield. - Parameters:
none - Returns:
boolean
- Description: Returns value of
-
:start()- Description: Starts the timer.
- Parameters:
none - Returns:
nothing
-
:stop()- Description: Stops the timer.
- Parameters:
none - Returns:
nothing
-
:execute()- Description: Executes user-definend callback.
- Parameters:
none - Returns:
nothing
-
:update(delta)- Description: Update timer by delta between frames. Used by timer manager.
- Parameters:
delta- time between frames. - Returns:
nothing
-
:__index()- Description: Returns string representation of the timer.
- Parameters:
none - Returns:
string
-
:__add(value)- Description: Add time to
time_left - Parameters:
number- seconds to be added totime_left - Returns:
nothing
- Description: Add time to
-
:__sub(value)- Description: Remove time from
time_left - Parameters:
number- seconds to subtract fromtime_left - Returns:
nothing
- Description: Remove time from
With timer manager (assuming you've already initiated timerManager in variable TimerManager):
require("timer")
Timer = TimerManager:create_timer({ -- TimerManager returns uuid of the timer
duration = 1, -- So you can get a timer using this value
autostart = true, -- in TimerManager:get_timer(uuid)
repeating = true,
autoremove = true,
callback = function() print("One second has passed!") end
})Without timer manager (need manually update delta in every timer):
Timer_1sec = Timer.create_timer({
duration = 1,
autostart = true,
repeating = true,
autoremove = true,
callback = function() print("One second has passed!") end
})
-- Pseudo main loop
function loop(delta) -- delta is a time between frames
Timer_1sec:update(delta)
endManages timers. Allows you to keep all timers in one place and allows you to control and update all created timers at once. Timers are created with unique UUID so you can easily access them by using :get_timer(uuid).
timers- Default value:{}. Table of timers. The manager keeps them all there.
name- Default value:TimerManager. Name of timer manager. Every manager should have a name, right?
-
.new()- Description: Creates new timer manager object.
- Parameters:
settings- Settings table for timer manager. See timer manager user fields for possible options. - Returns:
table
-
:get_timer(uuid)- Description: Get timer using uuid of created timer.
- Parameters:
uuid- UUID string of a timer. - Returns:
table
-
:get_timers()- Description: Get
timerstable. - Parameters:
none - Returns:
table
- Description: Get
-
:get_timer_by_num()- Description: Get timer using index number.
- Parameters:
index- index of a timer. - Returns:
table
-
:get_timer_count()- Description: Get timer count in
timerstable. - Parameters:
none - Returns:
number
- Description: Get timer count in
-
:update(delta)- Description: Updates all timers in
timertable bydeltavalue. - Parameters:
delta- delta between frames. - Returns:
nothing
- Description: Updates all timers in
-
:create_timer(settings)- Description: Creates timer with specified settings.
- Parameters:
settings- Settings table for timer. See timer settings - Returns:
nothing
-
:remove_timer(uuid)- Description: Remove timer specified by UUID.
- Parameters:
uuid- UUID string of a timer to remove. - Returns:
nothing
-
:gen_uuid()- Description: Generates UUID for the timer using template variable. UUID used to lookup specific timers in the table. Used in
:create_timer(). - Parameters:
none - Returns:
string- UUID using template intemplatevaraible.
- Description: Generates UUID for the timer using template variable. UUID used to lookup specific timers in the table. Used in
:__tostring()- Description: Returns string representation of the timer manager.
- Parameters:
none - Returns:
string- Timer manager representation.
require("timerManager")
Timer_manager = TimerManager.new({name="TimerManager_basic"})Pull requests are opened. Issues too. Any help is appreciated and will be rewarded with virtual cookies (like this one: 🍪). Down below is a TO-DO list og things that left to do. For every completed TO-DO task you will get 3 (🍪🍪🍪) cookies.
- - Refractoring:
- - general: fix code style
- - general: fix methods names
For some reasons lua creates timers in random order:
If you know why that happens, please, let me know!

