Files
Basalt/Basalt/objects/Timer.lua
Robert Jelic c9a74dfbd4 Small Fixes
- added Label RichText System - experimental
- moved eventhandlers to object
- Fixed still listening to incomming events when removing a object
- Fixed not passing args when creating a event via xml, args are now always stored inside a table called event
2023-05-07 15:17:27 +02:00

69 lines
1.9 KiB
Lua

return function(name, basalt)
local base = basalt.getObject("Object")(name, basalt)
local objectType = "Timer"
local timer = 0
local savedRepeats = 0
local repeats = 0
local timerObj
local timerIsActive = false
local object = {
getType = function(self)
return objectType
end,
setTime = function(self, _timer, _repeats)
timer = _timer or 0
savedRepeats = _repeats or 1
return self
end,
start = function(self)
if(timerIsActive)then
os.cancelTimer(timerObj)
end
repeats = savedRepeats
timerObj = os.startTimer(timer)
timerIsActive = true
self:listenEvent("other_event")
return self
end,
isActive = function(self)
return timerIsActive
end,
cancel = function(self)
if (timerObj ~= nil) then
os.cancelTimer(timerObj)
end
timerIsActive = false
self:removeEvent("other_event")
return self
end,
onCall = function(self, func)
self:registerEvent("timed_event", func)
return self
end,
eventHandler = function(self, event, ...)
base.eventHandler(self, event, ...)
if event == "timer" and tObj == timerObj and timerIsActive then
self:sendEvent("timed_event")
if (repeats >= 1) then
repeats = repeats - 1
if (repeats >= 1) then
timerObj = os.startTimer(timer)
end
elseif (repeats == -1) then
timerObj = os.startTimer(timer)
end
end
end,
}
object.__index = object
return setmetatable(object, base)
end