I LÖVE callbacks

General discussion about LÖVE, Lua, game development, puns, and unicorns.
chimmihc
Prole
Posts: 13
Joined: Mon Oct 19, 2015 6:32 am

I LÖVE callbacks

Post by chimmihc »

So much better to wrap around a callback event system than some OO listener system.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: I LÖVE callbacks

Post by airstruck »

Image
chimmihc
Prole
Posts: 13
Joined: Mon Oct 19, 2015 6:32 am

Re: I LÖVE callbacks

Post by chimmihc »

airstruck wrote:Image
Take a look at the example here.

I would much rather have the love.keypressed,love.mousepressed, etc callbacks.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: I LÖVE callbacks

Post by airstruck »

It's a lot simpler than having a robust, fully-featured event system isn't it?

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?

Code: Select all

function love.mousepressed (...)
    mylibrary.handlemouse (...)
end

function love.keypressed (...)
    mylibrary.handlekeyboard (...)
end
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?

Personally, I'd happily trade the simplicity of this event system for something a bit more flexible.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: I LÖVE callbacks

Post by zorg »

chimmihc wrote:Take a look at the example here.
I would much rather have the love.keypressed,love.mousepressed, etc callbacks.
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.

Yeah it's pretty neat... except, behind the scenes, löve uses tons of Objects and OO in general as well... I mean, the reason you can pass tons of different stuff into love.graphics.draw (Like canvases or images (both being specializations of the texture type) or spritebatches) is because their supertype is Drawable, which the function accepts as its argument.

As for what you linked, if you ever write your own lib code, or even just gamestates (with HUMP's gamestates for example, you will find that it has a registerEvents function that hooks the currently active state's callbacks to be run instead of löve's own, then as a last step, calls those as well. (Note that this isn't mandatory though)), you may need to have some functionality like that.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
chimmihc
Prole
Posts: 13
Joined: Mon Oct 19, 2015 6:32 am

Re: I LÖVE callbacks

Post by chimmihc »

I am well aware of what happens behind the scenes. I am just saying for "scripting end" coding I prefer simple systems that are easy to wrap around and make custom systems rather than systems like Gideros and Corona SDK have.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: I LÖVE callbacks

Post by vrld »

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 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: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?
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.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
slime
Solid Snayke
Posts: 3161
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: I LÖVE callbacks

Post by slime »

vrld wrote:
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 of that as a feature, because it allows me to know what is happening and when.
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).

For example, if you were to make a game that has gamestates (most games do), and a library is used in a specific gamestate, you would want to make sure the library isn't grabbing and processing events while it isn't supposed to be active. It's very easy to get that wrong or have to do a lot of poking in library code if love.event.addListener were used by the library. It's much easier to do the right thing when it isn't used.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: I LÖVE callbacks

Post by Inny »

Maybe I'm not following this thread correctly, is this about the love event callbacks (love.update et al) or continuation passing style? I thought the love event callbacks being where library users make sure to call the library event handlers was pretty much the standard.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: I LÖVE callbacks

Post by airstruck »

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.
I think it is. Doesn't mean it's a great standard. Consider this code:

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
In my view that's annoying and borderline unacceptable. The point of library code is to encapsulate functionality. When you have to wire up a shit ton of events by hand like this, that's the opposite of encapsulating functionality. I'd much rather do something like this:

Code: Select all

gamestate.push(function()
    somelibrary.handleevents()
end)

gamestate.pop(function()
    somelibrary.stophandlingevents()
end)
Of course that's entirely possible if you hook the callbacks in your library code. What's not possible is doing the hooking in a non-destructive way. It would have been possible with addListener, of course.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests