This repository has been archived by the owner. It is now read-only.
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
113 lines (103 sloc) 2.51 KB
-- This script allows to register multiple functions as Solarus events.
-- Usage:
-- Just require() this script and then all Solarus types
-- will have a register_event() method that adds an event callback.
-- Example:
-- local multi_events = require("scripts/multi_events")
-- -- Register two callbacks for the game:on_started() event:
-- game:register_event("on_started", my_function)
-- game:register_event("on_started", another_function)
-- -- It even works on metatables!
-- game_meta:register_event("on_started", my_function)
-- game_meta:register_event("on_started", another_function)
-- The good old way of defining an event still works
-- (but you cannot mix both approaches on the same object):
-- function game:on_started()
-- -- Some code.
-- end
-- Limitations:
-- Menus are regular Lua tables and not a proper Solarus type.
-- They can also support multiple events, but to do so you first have
-- to enable the feature explicitly like this:
-- multi_events:enable(my_menu)
-- Note that sol.main does not have this constraint even if it is also
-- a regular Lua table.
local multi_events = {}
local function register_event(object, event_name, callback)
local previous_callbacks = object[event_name] or function() end
object[event_name] = function(...)
return previous_callbacks(...) or callback(...)
-- Adds the multi event register_event() feature to an object
-- (userdata, userdata metatable or table).
function multi_events:enable(object)
object.register_event = register_event
local types = {
-- Add the register_event function to all userdata types.
for _, type in ipairs(types) do
local meta = sol.main.get_metatable(type)
assert(meta ~= nil)
-- Also add it to sol.main (which is a regular table).
return multi_events