mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-04 18:41:51 +00:00
add Schedule class & add time.schedule.world schedule group
This commit is contained in:
parent
cd2bc8fbf6
commit
b28aa71845
3 changed files with 73 additions and 0 deletions
37
res/modules/schedule.lua
Normal file
37
res/modules/schedule.lua
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
local Schedule = {
|
||||
__index = {
|
||||
set_interval = function(self, ms, callback)
|
||||
local id = self._next_interval
|
||||
self._intervals[id] = {
|
||||
last_called = 0.0,
|
||||
delay = ms / 1000.0,
|
||||
callback = callback,
|
||||
}
|
||||
self._next_interval = id + 1
|
||||
return id
|
||||
end,
|
||||
tick = function(self, dt)
|
||||
local timer = self._timer + dt
|
||||
for id, interval in pairs(self._intervals) do
|
||||
if timer - interval.last_called >= interval.delay then
|
||||
xpcall(interval.callback, function(s)
|
||||
debug.error(s..'\n'..debug.traceback())
|
||||
end)
|
||||
interval.last_called = timer
|
||||
end
|
||||
end
|
||||
self._timer = timer
|
||||
end,
|
||||
remove_interval = function (self, id)
|
||||
self._intervals[id] = nil
|
||||
end
|
||||
}
|
||||
}
|
||||
|
||||
return function ()
|
||||
return setmetatable({
|
||||
_next_interval = 1,
|
||||
_timer = 0.0,
|
||||
_intervals = {},
|
||||
}, Schedule)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue