fix(timer): redefine local function as method

The previous commit added a private property, but they
cannot be set outside of class methods. Added an error
when starting a running timer, too.
This commit is contained in:
Linden
2025-01-26 05:20:23 +11:00
parent 8af94a7194
commit 769fa35f38

View File

@@ -35,12 +35,8 @@ function timer:constructor(time, onEnd, async)
self:start(async) self:start(async)
end end
function timer:start(async) ---@protected
if self.private.startTime > 0 then return end function timer:run()
self.private.startTime = GetGameTimer()
local function tick()
while self:isPaused() or self:getTimeLeft('ms') > 0 do while self:isPaused() or self:getTimeLeft('ms') > 0 do
Wait(0) Wait(0)
end end
@@ -52,10 +48,15 @@ function timer:start(async)
self.private.triggerOnEnd = true self.private.triggerOnEnd = true
end end
if not async then return tick() end function timer:start(async)
if self.private.startTime > 0 then error('Cannot start a timer that is already running') end
self.private.startTime = GetGameTimer()
if not async then return self:run() end
Citizen.CreateThreadNow(function() Citizen.CreateThreadNow(function()
tick() self:run()
end) end)
end end