@@ -5,7 +5,9 @@ local timeoutsRemoveTbl = {}
55local timeoutsTbl = {}
66local 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
3838function 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 })
4749end
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 )
95102end
0 commit comments