Resetting all of Löve's callbacks?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Resetting all of Löve's callbacks?

Post by Germanunkol »

Here's the scenario:
In some cases (for example, when the user is using the wrong Löve version), I want to overwrite all callbacks, display something else (an error message) and exit on any key. That's very simple to do, like so (pseudocode, untested, but you get the idea):

Code: Select all

function checkLoveVersion()
    local major, minor, revision, codename = love.getVersion( )
    if minor != 9 then
         love.draw = function()
               love.graphics.print( "Using wrong löve version! Need Version 0.9", 10, 10 )
          end
         love.keypressed = love.quit
     end
end
As long as I call this function after defining my normal draw and keypressed event, I should be fine.
However, I want to avoid that some other callback (for example, textinput) is still present, so I have to manually set all of those to nil. Is there a way to do this automatically? I want to make sure this works over all love versions and future versions as well, and I want to make sure I don't forget some callback.
I'm thinking something like (of course this won't work, because the callbacks aren't in the "callbacks" table):

Code: Select all

for i, c in pairs( love.callbacks ) do
    love.callbacks[i] = nil
end
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Resetting all of Löve's callbacks?

Post by s-ol »

Lol, I accidentally sent a PM.
For everyone else:

Code: Select all

for _,v in ipairs{"textinput","update","mousepressed",...} do
  love[v] = nil
end

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: Resetting all of Löve's callbacks?

Post by Germanunkol »

Hm, that's nice code. However it's not really what I want to do. The problem I was having is: for future versions of Löve there are probably going to be more callbacks which don't exist yet. With your code, I still have to manually set the names of the callbacks which I want to reset. And I'm "scared" I will forget to add future callbacks and then the game will crash when these callbacks happen but the engine has the wrong version.

What I'm looking for is more something like a function love.reset() which resets all current callbacks.
But I guess I'll have to go the "manual" way.
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Resetting all of Löve's callbacks?

Post by s-ol »

Germanunkol wrote:Hm, that's nice code. However it's not really what I want to do. The problem I was having is: for future versions of Löve there are probably going to be more callbacks which don't exist yet. With your code, I still have to manually set the names of the callbacks which I want to reset. And I'm "scared" I will forget to add future callbacks and then the game will crash when these callbacks happen but the engine has the wrong version.

What I'm looking for is more something like a function love.reset() which resets all current callbacks.
But I guess I'll have to go the "manual" way.
Most of the callbacks are in a table called love.handlers:

Code: Select all

love.handlers
{focus = function() --[[..skipped..]] end, gamepadaxis = function() --[[..skipped..]] end, gamepadpressed = function() --[[..skipped..]] end, gamepadreleased = function() --[[..skipped..]] end, joystickadded = function() --[[..skipped..]] end, joystickaxis = function() --[[..skipped..]] end, joystickhat = function() --[[..skipped..]] end, joystickpressed = function() --[[..skipped..]] end, joystickreleased
= function() --[[..skipped..]] end, joystickremoved = function() --[[..skipped..]] end, keypressed = function() --[[..skipped..]] end, keyreleased = function() --[[..skipped..]] end, mousefocus = function() --[[..skipped..]] end, mousemoved = function() --[[..skipped..]] end, mousepressed = function() --[[..skipped..]] end, mousereleased = function() --[[..skipped..]] end, quit = function() --[[..skipped..]]
end, resize = function() --[[..skipped..]] end, textedit = function() --[[..skipped..]] end, textinput = function() --[[..skipped..]] end, threaderror = function() --[[..skipped..]] end, visible = function() --[[..skipped..]] end} --[[table: 0x41a73f98]]
I think that are actually all except for love.update, love.draw and love.run (which isn't really a callback and can't be changed anyway)

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
Evine
Citizen
Posts: 72
Joined: Wed May 28, 2014 11:46 pm

Re: Resetting all of Löve's callbacks?

Post by Evine »

A simple assert seems to be the best solution here. Unless you want to continue running the game after the "error"

Code: Select all

function checkLoveVersion()
    local major, minor, revision = love.getVersion( )
    assert(major == 0 and minor == 9 and revision == 2 , "Wrong Love version" )
end
Take a look at the love.errhand function. To see how love handles errors by default. (There's really no magic to it. Just pure lua code)
https://www.love2d.org/wiki/love.errhand
Artal, A .PSD loader: https://github.com/EvineDev/Artal
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Resetting all of Löve's callbacks?

Post by bartbes »

S0lll0s wrote:love.run (which isn't really a callback and can't be changed anyway)
But.. it can be changed.
User avatar
slime
Solid Snayke
Posts: 3142
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Resetting all of Löve's callbacks?

Post by slime »

Germanunkol wrote:when the user is using the wrong Löve version, I want to overwrite all callbacks, display something else (an error message)
In that specific case, LÖVE will already display a notice if the version you set in love.conf isn't compatible with the running LÖVE version.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Resetting all of Löve's callbacks?

Post by s-ol »

bartbes wrote:
S0lll0s wrote:love.run (which isn't really a callback and can't be changed anyway)
But.. it can be changed.
after it has started?

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Resetting all of Löve's callbacks?

Post by bartbes »

Well, no, but that goes for every callback (they just end much more often).
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Resetting all of Löve's callbacks?

Post by s-ol »

bartbes wrote:Well, no, but that goes for every callback (they just end much more often).
Yeah. What I was saying was that there is no point in resetting love.run in case of some error; it won't ever be restarted.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Post Reply

Who is online

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