diff --git a/DOCS/man/en/lua.rst b/DOCS/man/en/lua.rst index 02e7ca8ea9..54b05d9ea5 100644 --- a/DOCS/man/en/lua.rst +++ b/DOCS/man/en/lua.rst @@ -100,6 +100,10 @@ The ``mp`` module is preloaded, although it can be loaded manually with associated, the ``error`` field is set to a string describing the error, on success it's not set. + If multiple functions are registered for the same event, they are run in + registration order, which the first registered function running before all + the other ones. + Returns true if such an event exists, false otherwise. See `Events`_ and `List of events`_ for details. diff --git a/player/lua/defaults.lua b/player/lua/defaults.lua index 1ab1c4264a..8efa8d6315 100644 --- a/player/lua/defaults.lua +++ b/player/lua/defaults.lua @@ -135,7 +135,12 @@ mp.keep_running = true local event_handlers = {} function mp.register_event(name, cb) - event_handlers[name] = cb + local list = event_handlers[name] + if not list then + list = {} + event_handlers[name] = list + end + list[#list + 1] = cb return mp.request_event(name, true) end @@ -180,9 +185,11 @@ _G.mp_event_loop = function() mp.suspend() more_events = (e.event ~= "none") if more_events then - local handler = event_handlers[e.event] - if handler then - handler(e) + local handlers = event_handlers[e.event] + if handlers then + for _, handler in ipairs(handlers) do + handler(e) + end end end end