Skip to content

Commit e6ef8a4

Browse files
committed
chore(timers): Optimization
1 parent 7262fb9 commit e6ef8a4

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

plugin_files/bin/scripting/timers.lua

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ local timeoutsRemoveTbl = {}
55
local timeoutsTbl = {}
66
local nextTickFunctions = {}
77

8-
AddEventHandler("OnGameTick", function(event, simulating, first, last)
8+
local gameTickEvent = nil
9+
10+
local gameTickCall = function()
911
timerstblsize = #timeoutsTbl;
1012
timerst = GetTime()
1113

@@ -31,9 +33,7 @@ AddEventHandler("OnGameTick", function(event, simulating, first, last)
3133
end
3234

3335
timeoutsRemoveTbl = {}
34-
35-
return EventResult.Continue
36-
end)
36+
end
3737

3838
function SetTimeout(delay, callback)
3939
if type(delay) ~= "number" then
@@ -43,6 +43,8 @@ function SetTimeout(delay, callback)
4343
return print("The callback needs to be a function.")
4444
end
4545

46+
if not gameTickEvent then gameTickEvent = AddEventHandler("OnGameTick", gameTickCall) end
47+
4648
table.insert(timeoutsTbl, { call = GetTime() + delay, cb = callback })
4749
end
4850

@@ -57,6 +59,8 @@ function SetTimer(delay, callback)
5759
return print("The callback needs to be a function.")
5860
end
5961

62+
if not gameTickEvent then gameTickEvent = AddEventHandler("OnGameTick", gameTickCall) end
63+
6064
timerIds = timerIds + 1
6165

6266
timersTable[timerIds] = {
@@ -91,5 +95,8 @@ function NextTick(callback)
9195
if type(callback) ~= "function" then
9296
return print("The callback needs to be a function.")
9397
end
98+
99+
if not gameTickEvent then gameTickEvent = AddEventHandler("OnGameTick", gameTickCall) end
100+
94101
table.insert(nextTickFunctions, callback)
95102
end

0 commit comments

Comments
 (0)