use game_events.add in on_event.lua (#8082)
use game_events.add in on_event.lua previously on_event.lua and game_events.add had separate priority lists, so that independent of the priority parameter (which both game_events.add and on_event.lua now support) on_event.lua events were always executed first. The set_undoable(true) call is there to match the previous behavior of on_event.lua where events implemented via on_event were undoable by default. The higher default priority of 0.5 is there to match the previous behavior of on_event.lua where events implemented via on_event.lua were always run before wml events.
This commit is contained in:
parent
0abed71f75
commit
955a8bc445
1 changed files with 26 additions and 46 deletions
|
@ -2,54 +2,34 @@
|
|||
-- so you have to call this function from a toplevel lua tag or from a preload event.
|
||||
-- It is also not possible to use this for first_time_only=yes events.
|
||||
|
||||
if rawget(_G, "core_on_event") then
|
||||
return rawget(_G, "core_on_event") -- prevent double execution
|
||||
end
|
||||
|
||||
local event_handlers = {}
|
||||
|
||||
local old_on_event = wesnoth.game_events.on_event or function(eventname) end
|
||||
wesnoth.game_events.on_event = function(eventname)
|
||||
old_on_event(eventname)
|
||||
local context = nil
|
||||
for _, entry in pairs(event_handlers[eventname] or {}) do
|
||||
if context == nil then
|
||||
context = wesnoth.current.event_context
|
||||
end
|
||||
entry.h(context)
|
||||
end
|
||||
end
|
||||
-- This api used to be the default way to add events from lua (with its own implementation in lua
|
||||
-- based on game_events.on_event). Now it just calls game_evets.add, because otherwise the
|
||||
-- priority parameter wouldn't work across the different implementations.
|
||||
-- Still kept for compatibility and because it has an easier to
|
||||
-- use interface. Meaning you can easily write in a lua file:
|
||||
--
|
||||
-- on_event("moveto", 10, function(ec)
|
||||
-- ...
|
||||
-- end)
|
||||
--
|
||||
-- which is imo more convenient than the interface wesnoth.game_events.add or wesnoth.game_events.add_repeating offers
|
||||
-- even though its at this point technically equivalent to the latter.
|
||||
|
||||
|
||||
---Register an event handler
|
||||
---@param eventname string The event to handle; can be a comma-separated list
|
||||
---@param priority? number Events execute in order of decreasing priority, and secondarily in order of adding
|
||||
---@param fcn fun(ctx:event_context)
|
||||
local function on_event(eventname, priority, fcn)
|
||||
if string.match(eventname, ",") then
|
||||
for _,elem in ipairs((eventname or ""):split()) do
|
||||
on_event(elem, priority, fcn)
|
||||
end
|
||||
return
|
||||
end
|
||||
local handler
|
||||
return function(eventname, priority, fcn)
|
||||
if type(priority) == "function" then
|
||||
handler = priority
|
||||
priority = 0
|
||||
else
|
||||
handler = fcn
|
||||
fcn = priority
|
||||
priority = 0.5
|
||||
end
|
||||
eventname = string.gsub(eventname, " ", "_")
|
||||
event_handlers[eventname] = event_handlers[eventname] or {}
|
||||
local eh = event_handlers[eventname]
|
||||
table.insert(eh, { h = handler, p = priority})
|
||||
-- prioritize last entry
|
||||
for i = #eh - 1, 1, -1 do
|
||||
if eh[i].p < eh[i + 1].p then
|
||||
eh[i], eh[i + 1] = eh[i + 1], eh[i]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
core_on_event = on_event
|
||||
return on_event
|
||||
wesnoth.game_events.add{
|
||||
name = eventname,
|
||||
priority = priority,
|
||||
first_time_only = false,
|
||||
action = function()
|
||||
context = wesnoth.current.event_context
|
||||
wesnoth.experimental.game_events.set_undoable(true)
|
||||
fcn(context)
|
||||
end
|
||||
}
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue