mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-11 13:26:58 +00:00

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.
36 lines
1.2 KiB
Lua
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
|