Solarus Forum

Community => Your scripts => Topic started by: Christopho on October 20, 2016, 03:18:55 pm

Title: multi_events.lua: Register multiple functions on an event
Post by: Christopho on October 20, 2016, 03:18:55 pm
I already talked about the multi_events.lua script in another topic, but let's share it in this forum because most scripts to be shared by the community will need it. Indeed, scripts that use it can be more self-contained, and therefore easier to share, which is the goal of this forum.

Scripts that I will share it on this forum, as well as the ones you can already find in the Zelda OLB Solarus Edition ( project, almost always use this feature.

So what is it?

This multi_events.lua script allows to register several functions to a single event, for example game:on_started().


You often have a lot of scripts: the dialog box, the HUD, the pause menu, the camera manager, etc., and all these components need to do some stuff in the same event, for example they all need to perform some initialization in game:on_started(). One way to do it is to define the game:on_started() event in your game_manager.lua script, and from there, call the initialization code of each component (the dialog box, the HUD, the pause menu, the camera manager, etc). You can guess the problem: whenever you create a new component that also needs to do something in game:on_started(), you need to modify your game_manager.lua script again. This is because game:on_started() can be defined only once (or it would overwrite the previous definition). And then when you want to share your nice new feature on this forum, you need to tell people how to modifiy they game_manager.lua script. Not as easy as just copying a Lua script file. This is only an exemple, the same is true for a lot of other events (not only game:on_started()).

As an example, our previous games all have this issue. Here is the game:on_started() event from Zelda ROTH SE:
Code: (lua) [Select]
function game:on_started()

  dialog_box = dialog_box_manager:create(game)
  hud = hud_manager:create(game)
  pause_menu = pause_manager:create(game)
  camera = camera_manager:create(game)

  -- Initialize the hero.
  local hero = game:get_hero()
  game:stop_rabbit()  -- In case the game was saved as a rabbit.

  -- Measure the time played.
As described above, we initialize all components. Components are usually defined in their own separate Lua scripts of course, but there is still this central initialization code needed. It is a bit delicate to share one of these features with you (like for example, the transformation of Link into a rabbit), because you would have not only to copy the rabbit.lua script, but also this initialization code. And actually even more, because in fact, rabbit.lua also needs to do stuff in game:on_game_over_started() and in hero:on_state_changed()… Very easy to miss things and to introduce bugs when you want the feature in your project.

So, it would be great if there was a way to define multiple functions to be called when a single event occurs, right? This is precisely what multi_events.lua script does. By using a different syntax, you can register several functions to the same event. Which means that in our examples above, you no longer need to modifiy existing scripts every time you import a script from some other project! Well, you still need to require() the script somewhere, but that's it.

Now in Zelda OLB SE, one of our new projects, the game_manager.lua script no longer defines game:on_started() at all. Instead, every component (dungeons, equipment, dialog box, HUD, pause menu, camera, rabbit manager, chronometer, etc.) registers its own stuff to be done in game:on_started(). The game manager remains unchanged when features are added or removed from the project. The whole rabbit code is contained in a single script now (and actually I will share it on this forum very soon).

So, this is extremely useful to make scripts as much independent and self-contained as possible. Basically, they become much easier to share.

How to use it?

- Copy the multi_events.lua script below to your project, into the scripts/ folder.
- Just require("scripts/multi_events.lua") at some point in your project (like from main.lua).
- In your scripts for all events, you now have the choice between two approaches: the traditional one or the multi-functions one, using object:register_event(event_name, callback).


Code: (lua) [Select]
-- The traditional way still works:
function my_sprite:on_animation_finished()
  -- blah blah

Code: (lua) [Select]
local map = ...


-- This approach allows to register several functions to the same event:
map:register_event("on_started", function()
        -- blah blah

map:register_event("on_started", function()
        -- other stuff

The code!

Script: scripts/multi_events.lua
License: GPL v3
Author: Christopho
Code: (lua) [Select]
-- 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

Title: Re: Register multiple functions on an event
Post by: llamazing on October 21, 2016, 08:20:13 am
On line 39, why do you create the empty function? Isn't that wasteful? Couldn't you just do the following instead? Lines 39 and 41 are modified.

Code: (lua) [Select]
local function register_event(object, event_name, callback)
  local previous_callbacks = object[event_name]
  object[event_name] = function(...)
    return previous_callbacks and previous_callbacks(...) or callback(...)
Title: Re: Register multiple functions on an event
Post by: Christopho on October 21, 2016, 08:55:38 am
Both approaches should be equivalent I think. It is a matter of taste. To me, the and+or version is a bit less readable. It makes one additional test. Also, when defining a recursive algorithm it is nice to show explicitly the an initial value, which is often an empty list, or in this particular case, an empty function. Like in functional programming.
But again, this is equivalent, there is no real problem with your suggestion.
Title: Re: multi_events.lua: Register multiple functions on an event
Post by: MetalZelda on January 27, 2017, 11:42:29 pm
Interesting, using the multiple event for game:save() makes the game un-save-able, calling game:save() returns nil
Title: Re: multi_events.lua: Register multiple functions on an event
Post by: Christopho on January 28, 2017, 01:58:57 am
What do you mean? game:save() is a normal function, not an event.
Title: Re: multi_events.lua: Register multiple functions on an event
Post by: MetalZelda on January 28, 2017, 03:39:35 am
Oh right I didn't take that in consideration