zorg wrote:Granted this won't work if the function is not defined in the state, but that's another bag of tricks to deal with
(currentState.keypressed and currentState.keypressed(...) is one solution, but one could or it with an empty function too)
cval wrote:
"Scancode" appeared in 0.10 and whereas it's not really mandatory to write your library functions exactly like that (and if you aim for compatibility with older versions - not necessary at all), people who use it later in their project may experience bugs that sometimes aren't easy to track. You know, because their code may expect those omitted arguments and process it somehow to change state of their program flow.
I use function generation from list of callbacknames, and this tricks too.
Any callback called with multiple arguments, passes through "...", like original callbacks.
Code: Select all
--This simplifies the addition of new callbacks with a totally any arguments.
local callbacks = {
'keypressed', 'keyreleased', 'textinput', 'textedited',
'mousepressed', 'mousereleased', 'mousefocus', 'mousemoved', 'wheelmoved',
'touchpressed', 'touchreleased', 'touchmoved',
'directorydropped', 'filedropped',
'quit', 'lowmemory',
'focus', 'visible',
'threaderror', 'errhand',
'update', 'draw',
'gamepadaxis', 'gamepadpressed', 'gamepadreleased',
'joystickadded', 'joystickaxis', 'joystickhat',
'joystickpressed', 'joystickreleased', 'joystickremoved'
}
--Self and additional love callback initialisation
--(load/update/draw/keypressed/keyreleased etc)
function states:init()
for _, k in ipairs(callbacks) do
--initialisation self methods like self:update(dt)
self[k] = function(self, ...)
if self.current[k] and type(self.current[k]) == 'function' then
--passed "self" and any number of arguments, with any names and etc.
self.current[k](self.current, ...)
end
end
--optional addition methods for love, if not defined
if not love[k] then
love[k] = function(...)
self[k](self, ...)
end
end
end
return self
end
function anystate:keypressed(key, scancode, isRepeat) {code} end - works fine too.
In love 0.10. Indeed, in another version, they will not be passed arguments "scancode" and "isrepeat".
Heh.
I can't say that "the designer no responsibility".
Rather, it is the possibility of using the library on all versions of love.
Yea, callbacknames forbidden for use:
Code: Select all
--Function returns new state like object
local function state(t)
return setmetatable({}, {
--table with methods
__index = t,
--forbidding reserved names
__newindex = function(self, key, value)
--checking if key is not named like any callback in list
if not isReserved(key) then
rawset(self, key, value)
else
error('Trying to modify reserved callback "'..key..'"', 2)
end
end
}
)
end
I'm sorry, I do not use github, and therefore the source code on pastebin.
Little comments included.
http://pastebin.com/9rgH4BAj