The code shown below is what I described above.
Code: Select all
local callbacks = {
"update", "keypressed", "keyreleased", "textedited", "textinput",
"mousemoved", "mousepressed", "mousereleased", "wheelmoved",
"gamepadaxis", "gamepadpressed", "gamepadreleased", "joystickadded",
"joystickaxis", "joystickhat", "joystickpressed", "joystickreleased",
"joystickremoved", "touchmoved", "touchpressed", "touchreleased"
}
for _, v in ipairs(callbacks) do
local func = function(...)
local args = {...}
self.children:forEach(function(i, child)
if type(child[v]) == "function" then
child[v](unpack(args))
end
end)
end
self.class.__declaredMethods[v] = func
self.class.__instanceDict[v] = func
Because this works
Code: Select all
self.update = function(dt)
print "Hello World"
end
Code: Select all
function Game:update(dt)
print "Hello World"
end
- I'm also using kikito's middleclass for OOP.
- self.Children is a class similar to C#'s List.
- Although I can use love.handlers, I decided to list only input handlers.