-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathon-tick-n.lua
98 lines (88 loc) · 2.8 KB
/
on-tick-n.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
if ... ~= "__flib__.on-tick-n" then
return require("__flib__.on-tick-n")
end
--- Schedule tasks to be executed later.
--- ```lua
--- local flib_on_tick_n = require("__flib__.on-tick-n")
--- ```
--- @class flib_on_tick_n
local on_tick_n = {}
--- Initialize the module's script data table.
---
--- Must be called at the **beginning** of `on_init`. Can also be used to delete all current tasks.
function on_tick_n.init()
if not storage.__flib then
storage.__flib = {}
end
--- @type table<number, flib.OnTickNTasks>
storage.__flib.on_tick_n = {}
end
--- Retrieve the tasks for the given tick, if any.
---
--- Must be called **during** `on_tick`.
--- @param tick number
--- @return flib.OnTickNTasks?
function on_tick_n.retrieve(tick)
-- Failsafe for rare cases where on_tick can fire before on_init
if not storage.__flib or not storage.__flib.on_tick_n then
return
end
local actions = storage.__flib.on_tick_n[tick]
if actions then
storage.__flib.on_tick_n[tick] = nil
return actions
end
end
--- Add a task to execute on the given tick.
--- @param tick number
--- @param task any The data representing this task. This can be anything except for a `function`.
--- @return flib.OnTickNTaskID ident An identifier for the task. Save this if you might remove the task before execution.
function on_tick_n.add(tick, task)
local list = storage.__flib.on_tick_n
local tick_list = list[tick]
if tick_list then
local index = #tick_list + 1
tick_list[index] = task
return { index = index, tick = tick }
else
list[tick] = { task }
return { index = 1, tick = tick }
end
end
--- Remove a scheduled task.
--- @param ident flib.OnTickNTaskID The identifier object for the task, as returned from `on-tick-n.add`.
function on_tick_n.remove(ident)
local tick_list = storage.__flib.on_tick_n[ident.tick]
if not tick_list or not tick_list[ident.index] then
return false
end
tick_list[ident.index] = nil
return true
end
--- A unique identifier for a previously added task, used in `on-tick-n.remove`.
--- @class flib.OnTickNTaskID
--- @field tick number The tick this task is scheduled for.
--- @field index number The tasks' index in the tick's `Tasks` table.
--- A table of tasks.
---
--- Each task can be anything that is not a function, as specified in `on-tick-n.add`.
---
--- **This is not an array, there may be gaps. Always use `pairs` to iterate this table.**
---
--- # Example
---
--- ```lua
--- event.on_tick(function(e)
--- for _, task in pairs(on_tick_n.retrieve(e.tick) or {}) do
--- if task == "say_hi" then
--- game.print("Hello there!")
--- elseif task == "order_66" then
--- for _, player in pairs(game.players) do
--- player.die()
--- end
--- end
--- end
--- end)
--- ```
--- @alias flib.OnTickNTasks table<number, any>
return on_tick_n