Small event handling module
Posted: Mon Jul 13, 2015 5:22 am
This module provides a simple way to register multiple handlers for events.
The module has no knowledge of Love's API, so the first step (in main.lua) is this:
This "hijacks" all standard Love events, as well as the love.load, love.update and love.draw callbacks.
Now instead of defining (for example) love.update, you can do something like this:
The benefit here is that you can register as many handlers as you like for this "update" event; all of them will fire.
If you need to unregister an event handler at some point, save the return value from Event.on and call :remove() on it later.
You can also dispatch your own events if you like. These are not Love events, they are produced and consumed within this module.
That's pretty much it, do whatever you want with it. Any input on API or implementation is welcome.
https://gist.github.com/airstruck/9c07256b06ef5925c540
The module has no knowledge of Love's API, so the first step (in main.lua) is this:
Code: Select all
local Event = require 'event'
Event.injectDispatchers(love.handlers)
Event.injectDispatchers(love, { 'load', 'update', 'draw' })
Now instead of defining (for example) love.update, you can do something like this:
Code: Select all
Event.on('update', function (dt)
myGame:updateStuff(dt)
end)
If you need to unregister an event handler at some point, save the return value from Event.on and call :remove() on it later.
Code: Select all
local keyHandler = Event.on('keypressed', function (b, s, r)
myGame:mashButtons(b, s, r)
end)
-- later...
keyHandler:remove()
Code: Select all
Event.dispatch('pebkac', keyboard, chair)
https://gist.github.com/airstruck/9c07256b06ef5925c540