wesnoth/data/lua/on_event.lua
gfgtdf 955a8bc445
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.
2023-12-15 21:28:45 +01:00

36 lines
1.2 KiB
Lua

-- registers an event handler. note that, like all lua variables this is not persitent in savefiles,
-- 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.
-- 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.
return function(eventname, priority, fcn)
if type(priority) == "function" then
fcn = priority
priority = 0.5
end
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