3--! \brief Timers
module for FrameworkZ. Allows for the creation of timers for delaying code executions.
4--! \class FrameworkZ.Timers
6--FrameworkZ.Timers.__index = FrameworkZ.Timers
7FrameworkZ.Timers = FrameworkZ.Foundation:NewModule(FrameworkZ.Timers, "Timers")
8FrameworkZ.Timers.AdvancedTimers = {}
9FrameworkZ.Timers.SimpleTimers = {}
11local os_time = os.time
12local table_insert = table.insert
13local table_remove = table.remove
18--! \brief Creates a simple timer that executes a function after a delay.
19--! \param \integer delay The delay in seconds before the function is executed.
20--! \param \function func The function to execute after the delay.
21function FrameworkZ.Timers:Simple(delay, func)
22 assert(type(delay) == "number", "Delay of timer should be a number type")
23 assert(type(func) == "function", "Func of timer should be a function type (lol)")
25 table_insert(self.SimpleTimers, {
26 EndTime = os_time() + delay,
31function FrameworkZ.Timers:Create(name, delay, repetitions, func)
33 assert(type(name) == "string", "ID of timer should be a string type")
34 assert(type(delay) == "number", "Delay of timer should be a number type")
35 assert(type(repetitions) == "number", "Repetitions of timer should be a number type")
36 assert(type(func) == "function", "Func of timer should be a function type (lol)")
38 self.AdvancedTimers[name] = {
40 StartRepetitions = repetitions,
41 Repetitions = repetitions,
42 Infinity = repetitions == 0,
43 LastFuncTime = os_time(),
50local function timerUpdate()
52 local cur_time = os_time()
54 local advanced_timers = FrameworkZ.Timers.AdvancedTimers
56 for k, v in pairs(advanced_timers) do
60 if cur_time >= v.LastFuncTime + v.Delay then
64 v.LastFuncTime = cur_time
66 if not v.Infinity then
68 v.Repetitions = v.Repetitions - 1
70 if v.Repetitions <= 0 then
72 FrameworkZ.Timers.AdvancedTimers[k] = nil
84 local simple_timers = FrameworkZ.Timers.SimpleTimers
86 for i = #simple_timers, 1, -1 do
88 local t = simple_timers[i]
90 if t.EndTime <= cur_time then
94 table_remove(simple_timers, i)
101Events.OnTickEvenPaused.Add(timerUpdate)
103function FrameworkZ.Timers:Remove(name)
105 local t = self.AdvancedTimers[name]
107 if not t then return false end
109 self.AdvancedTimers[name] = nil
115function FrameworkZ.Timers:Exists(name)
117 return self.AdvancedTimers[name] and true or false
121function FrameworkZ.Timers:Start(name)
123 local t = self.AdvancedTimers[name]
125 if not t then return false end
127 t.Repetitions = t.StartRepetitions
128 t.LastFuncTime = os_time()
136function FrameworkZ.Timers:Pause(name)
138 local t = self.AdvancedTimers[name]
140 if not t then return false end
142 if t.Paused then return false end
145 t.PausedTime = os_time()
151function FrameworkZ.Timers:UnPause(name)
153 local t = self.AdvancedTimers[name]
155 if not t then return false end
157 if not t.Paused then return false end
164FrameworkZ.Timers.Resume = FrameworkZ.Timers.UnPause
166function FrameworkZ.Timers:Toggle(name)
168 local t = self.AdvancedTimers[name]
170 if not t then return false end
172 t.Paused = not t.Paused
178function FrameworkZ.Timers:TimeLeft(name)
180 local t = self.AdvancedTimers[name]
182 if not t then return end
186 return (t.Repetitions - 1) * t.Delay + (t.LastFuncTime + t.Delay - t.PausedTime)
190 return (t.Repetitions - 1) * t.Delay + (t.LastFuncTime + t.Delay - os_time())
196function FrameworkZ.Timers:NextTimeLeft(name)
198 local t = self.AdvancedTimers[name]
200 if not t then return end
204 return t.LastFuncTime + t.Delay - t.PausedTime
208 return t.LastFuncTime + t.Delay - os_time()
214function FrameworkZ.Timers:RepsLeft(name)
216 local t = self.AdvancedTimers[name]
218 return t and t.Repetitions
void local advanced_timers()
Timers module for FrameworkZ. Allows for the creation of timers for delaying code executions.
FrameworkZ Timers SimpleTimers
void Simple(delay, func)
Creates a simple timer that executes a function after a delay.
FrameworkZ Timers AdvancedTimers
void Create(name, delay, repetitions, func)