-
Notifications
You must be signed in to change notification settings - Fork 810
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. `Events` global is replaced with `require('devilutionx.events')`. 2. Table is simplified and documented. 3. `On` removed from the event names as it was a bit redundant. 4. Functions are camelCase for consistency (e.g. `add` instead of `Add`). Example script: ```lua local events = require("devilutionx.events") local render = require("devilutionx.render") local message = require("devilutionx.message") local function greet() message("Hello from " .. _VERSION) print("Hello from ", _VERSION) end events.GameStart.add(greet) local function drawGreet() render.string("Hello from " .. _VERSION, 10, 40) end events.GameDrawComplete.add(drawGreet) ```
- Loading branch information
Showing
7 changed files
with
81 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
local function CreateEvent() | ||
local functions = {} | ||
return { | ||
---Adds an event handler. | ||
--- | ||
---The handler called every time an event is triggered. | ||
---@param func function | ||
add = function(func) | ||
table.insert(functions, func) | ||
end, | ||
|
||
---Removes the event handler. | ||
---@param func function | ||
remove = function(func) | ||
for i, f in ipairs(functions) do | ||
if f == func then | ||
table.remove(functions, i) | ||
break | ||
end | ||
end | ||
end, | ||
|
||
---Triggers an event. | ||
--- | ||
---The arguments are forwarded to handlers. | ||
---@param ... any | ||
trigger = function(...) | ||
if arg ~= nil then | ||
for _, func in ipairs(functions) do | ||
func(table.unpack(arg)) | ||
end | ||
else | ||
for _, func in ipairs(functions) do | ||
func() | ||
end | ||
end | ||
end, | ||
__sig_trigger = "(...)", | ||
} | ||
end | ||
|
||
local events = { | ||
---Called early on game boot. | ||
GameBoot = CreateEvent(), | ||
__doc_GameBoot = "Called early on game boot.", | ||
|
||
---Called every time a new game is started. | ||
GameStart = CreateEvent(), | ||
__doc_GameStart = "Called every time a new game is started.", | ||
|
||
---Called every frame at the end. | ||
GameDrawComplete = CreateEvent(), | ||
__doc_GameDrawComplete = "Called every frame at the end.", | ||
} | ||
|
||
---Registers a custom event type with the given name. | ||
---@param name string | ||
function events.registerCustom(name) | ||
events[name] = CreateEvent() | ||
end | ||
|
||
events.__sig_registerCustom = "(name: string)" | ||
events.__doc_registerCustom = "Register a custom event type." | ||
|
||
return events |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters