Skip to content

Commit

Permalink
Fix timer limit bugs. Fixes: thegrb93#1562
Browse files Browse the repository at this point in the history
  • Loading branch information
thegrb93 committed Nov 29, 2023
1 parent 053474d commit 2d224dc
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lua/starfall/libs_sh/timer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
local checkluatype = SF.CheckLuaType
local timer = timer

local max_timers = CreateConVar("sf_maxtimers", "200", { FCVAR_ARCHIVE, FCVAR_REPLICATED }, "The max number of timers that can be created")
local timer_count = SF.LimitObject("timer", "timer", 200, "The number of concurrent starfall timers")


--- Deals with time and timers.
Expand All @@ -15,11 +15,11 @@ SF.RegisterLibrary("timer")
return function(instance)

local timers = {}
local timer_count = 0

instance:AddHook("deinitialize", function()
for name, _ in pairs(timers) do
timer.Remove(name)
timer_count:free(instance.player, 1)
end
end)

Expand Down Expand Up @@ -67,23 +67,24 @@ local function mangle_simpletimer_name()
end

local function createTimer(name, delay, reps, func, simple)
if timer_count > max_timers:GetInt() then SF.Throw("Max timers exceeded!", 2) end
timer_count = timer_count + 1

local timername
if simple then
timername = mangle_simpletimer_name()
else
timername = mangle_timer_name(name)
end

if not timers[timername] then
timer_count:use(instance.player, 1)
end

local timerdata = {reps = reps, func = func}
local function timerCallback()
if timerdata.reps ~= 0 then
timerdata.reps = timerdata.reps - 1
if timerdata.reps<=0 then
timer_count = timer_count - 1
timers[timername] = nil
timer_count:free(instance.player, 1)
end
end
instance:runFunction(timerdata.func)
Expand Down Expand Up @@ -124,7 +125,7 @@ function timer_library.remove(name)

local timername = mangle_timer_name(name)
if timers[timername] then
timer_count = timer_count - 1
timer_count:free(instance.player, 1)
timers[timername] = nil
timer.Remove(timername)
end
Expand Down Expand Up @@ -225,7 +226,7 @@ end
--- Returns number of available timers
-- @return number Number of available timers
function timer_library.getTimersLeft()
return max_timers:GetInt() - timer_count
return timer_count:check(instance.player)
end

end

0 comments on commit 2d224dc

Please sign in to comment.