I LÖVE callbacks
Posted: Tue Jan 12, 2016 3:07 pm
So much better to wrap around a callback event system than some OO listener system.
Take a look at the example here.airstruck wrote:
Code: Select all
function love.mousepressed (...)
mylibrary.handlemouse (...)
end
function love.keypressed (...)
mylibrary.handlekeyboard (...)
end
Oh ok, i get it now, you're saying that you like how löve does it, with a game loop ([wiki]love.run[/wiki]) polling SDL events and calling callbacks ([wiki]love.event[/wiki]) inside that.chimmihc wrote:Take a look at the example here.
I would much rather have the love.keypressed,love.mousepressed, etc callbacks.
I think of that as a feature, because it allows me to know what is happening and when. No magic behavior or libraries doing stuff behind my back. I'm in control. This has two advantages: For one, it is easier to optimize code. But more importantly, it makes it easier to reason about the code and find bugs in it. Of course, this comes at the costs you mentioned, but in game programming (and other real-time stuff) the benefits outweigh the costs. In other areas (web programming, UI heavy stuff) the situation is different.airstruck wrote:But what will you do when you want to write some library code than needs to respond to events? Have your API require a bunch of boilerplate where the user writes stuff like this?
hump does it that way, but only upon request. When this is done in love.load(), the library can reasonably assume that all callbacks are defined. If the callback is overwritten later on, it's either very intentional (for example, to temporarily suspend library callbacks) or very unintentional. The latter is unfortunate, but not the libraries fault: the user requested to overwrite the callbacks in the first place.airstruck wrote:Or hook it yourself, cross your fingers and hope the user doesn't stomp on it by redefining that handler? But the user still needs to define handlers, don't they... so now won't you need to implement your own ad-hoc event system that they'll have to use instead?
In fact, love.event.addListener was a function in 0.10.0 for a short time before release, but ended up being removed for reasons like that (and some other reasons).vrld wrote:I think of that as a feature, because it allows me to know what is happening and when.airstruck wrote:But what will you do when you want to write some library code than needs to respond to events? Have your API require a bunch of boilerplate where the user writes stuff like this?
I think it is. Doesn't mean it's a great standard. Consider this code:Inny wrote: I thought the love event callbacks being where library users make sure to call the library event handlers was pretty much the standard.
Code: Select all
function love.draw (...)
somelibrary.handledraw(...)
end
function love.resize (...)
somelibrary.handleresize(...)
end
function love.mousepressed (...)
somelibrary.handlemousepressed(...)
end
function love.mousereleased (...)
somelibrary.handlemousereleased(...)
end
function love.mousemoved (...)
somelibrary.handlemousemoved(...)
end
function love.keypressed (...)
somelibrary.handlekeypressed(...)
end
function love.keyreleased (...)
somelibrary.handlekeyreleased(...)
end
function love.textinput (...)
somelibrary.handletextinput(...)
end
-- and so on
Code: Select all
gamestate.push(function()
somelibrary.handleevents()
end)
gamestate.pop(function()
somelibrary.stophandlingevents()
end)