-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.lua
57 lines (49 loc) · 1.35 KB
/
control.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
--Loaded in the order they are given
local modules = {
require("modules.MasterList"),
require("modules.LeaderboardUpdater"),
require("modules.EntityHover"),
require("modules.GuiLeaderboardManager")
}
--Returns a event_handler that executers each event handler function passed in
local function create_multifunction_handler(functions)
return function(event)
for i=1, #functions do
local fn = functions[i]
fn(event)
end
end
end
--Registers all event handlers in the given modules
local function register_events(modules)
local event_map = {}
--build event map
for _, module in ipairs(modules) do
local hooks = module.hooks()
for event, fn in pairs(hooks) do
event_map[event] = event_map[event] or {}
table.insert(event_map[event], fn)
end
end
--register events
for event, fn_table in pairs(event_map) do
if #fn_table == 1 then
script.on_event(event, fn_table[1])
elseif #fn_table > 1 then
script.on_event(event, create_multifunction_handler(fn_table))
end
end
end
register_events(modules)
script.on_init(function()
for _, module in ipairs(modules) do
module.init()
end
end
)
script.on_load(function()
for _, module in ipairs(modules) do
module.load()
end
end
)