For my Ludum Dare 23 entry I needed some sort messaging system. I considered using kikito's beholder.lua, but it seemed (no offense) just a bit too bloated and complex for my purposes.
A basic implementation of signals and slots is easy enough to do yourself. This is what I came up with (formatting changed to emphasize the brevity):
Code: Select all
local reg = setmetatable({}, {__index = function(t,k) local u = {}; rawset(t, k, u); return u end})
return {
emit = function(s, ...) for f in pairs(reg[s]) do f(...) end end,
register = function(s, f) reg[s][f] = f; return f end,
remove = function(s, f) reg[s][f] = nil end,
clear = function(s) reg[s] = {} end,
}
Code: Select all
-- elements.lua
signals.register('sensor-on', function() self.value = true end)
signals.register('sensor-off', function() self.value = false end)
-- bot.lua
if self.level:sense(p) then
signals.emit('sensor-on')
else
signals.emit('sensor-off')
end
The above is still the basic core of hump.signal, but I added two nifty features:
- Support for registry instances (akin to hump.timer).
- Support for lua string patterns: You can emit, remove from and clear multiple signals that match a pattern.