Skip to content

Commit

Permalink
fix(timer): redefine local function as method
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
thelindat committed Jan 25, 2025
1 parent 8af94a7 commit 769fa35
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions imports/timer/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,28 @@ function timer:constructor(time, onEnd, async)
self:start(async)
end

function timer:start(async)
if self.private.startTime > 0 then return end
---@protected
function timer:run()
while self:isPaused() or self:getTimeLeft('ms') > 0 do
Wait(0)
end

self.private.startTime = GetGameTimer()
if self.private.triggerOnEnd then
self:onEnd()
end

local function tick()
while self:isPaused() or self:getTimeLeft('ms') > 0 do
Wait(0)
end
self.private.triggerOnEnd = true
end

if self.private.triggerOnEnd then
self:onEnd()
end
function timer:start(async)
if self.private.startTime > 0 then error('Cannot start a timer that is already running') end

self.private.triggerOnEnd = true
end
self.private.startTime = GetGameTimer()

if not async then return tick() end
if not async then return self:run() end

Citizen.CreateThreadNow(function()
tick()
self:run()
end)
end

Expand Down

0 comments on commit 769fa35

Please sign in to comment.