Skip to content

Commit 724da5b

Browse files
committed
fix(timers): NextTick not executing in some cases
1 parent f757faf commit 724da5b

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

plugin_files/bin/scripting/timers.lua

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,29 @@ local timeoutsRemoveTbl = {}
55
local timeoutsTbl = {}
66
local nextTickFunctions = {}
77

8-
AddEventHandler("OnGameTick", function(event, simulating, first, last)
9-
timerstblsize = #timeoutsTbl;
8+
AddEventHandler("OnGameTick", function(event, simulating, first, last)
9+
timerstblsize = #timeoutsTbl;
1010
timerst = GetTime()
1111

12-
for i=1,#nextTickFunctions do
13-
nextTickFunctions[i]()
12+
local nexttickCopy = nextTickFunctions
13+
nextTickFunctions = {}
14+
15+
for i = 1, #nexttickCopy do
16+
nexttickCopy[i]()
1417
end
1518

16-
for i=1,timerstblsize do
17-
if timeoutsTbl[i].call - timerst <= 0 then
18-
timeoutsTbl[i].cb();
19-
timeoutsRemoveTbl[#timeoutsRemoveTbl + 1] = i
20-
end
19+
for i = 1, timerstblsize do
20+
if timeoutsTbl[i].call - timerst <= 0 then
21+
timeoutsTbl[i].cb();
22+
timeoutsRemoveTbl[#timeoutsRemoveTbl + 1] = i
23+
end
2124
end
2225

23-
for i=#timeoutsRemoveTbl,1,-1 do
24-
table.remove(timeoutsTbl, timeoutsRemoveTbl[i])
25-
end
26+
for i = #timeoutsRemoveTbl, 1, -1 do
27+
table.remove(timeoutsTbl, timeoutsRemoveTbl[i])
28+
end
2629

2730
timeoutsRemoveTbl = {}
28-
nextTickFunctions = {}
2931

3032
return EventResult.Continue
3133
end)
@@ -54,8 +56,8 @@ function SetTimer(delay, callback)
5456

5557
timerIds = timerIds + 1
5658

57-
timersTable[timerIds] = {
58-
id = timerIds,
59+
timersTable[timerIds] = {
60+
id = timerIds,
5961
callback = callback,
6062
timeoutFunction = function()
6163
if timersTable[timerIds] then
@@ -87,4 +89,4 @@ function NextTick(callback)
8789
return print("The callback needs to be a function.")
8890
end
8991
table.insert(nextTickFunctions, callback)
90-
end
92+
end

0 commit comments

Comments
 (0)