Page 1 of 2

Version independent quitting

Posted: Sat Mar 31, 2012 2:24 pm
by T-Bone
Hi :neko: I'm trying to make my game quit nicely both on 0.7.2 and 0.8.0. Since the commands are different, love.event.push('q') and love.event.push('quit'), I tried using pcall like this

Code: Select all

if pcall(function() love.event.push('q') end) then
		
	else
		love.event.push('quit')
	end


However, when I run this on 0.8.0, the blue screen still appears and says "Unknown event 'q'". It is as if LÖVE error aren't caught by pcall properly. Is this perhaps a bug in 0.8.0 or am I doing something wrong?

I guess a workaround could be something like checking at runtime what version it is currently running on.

Re: Version independent quitting

Posted: Sat Mar 31, 2012 2:49 pm
by a_greed
Same problem. I solved it by os.exit(), but I don't think it's the best solution.
UPD: It's bad solution. It doesn't work for Dingoo for example

Re: Version independent quitting

Posted: Sat Mar 31, 2012 3:43 pm
by kikito
Someone pointed out that 0.8.x doesn't have love.event.push - it's love.eventquit .

Here's the bit I'm using now:

Code: Select all

  function quit()
     local f = love.event.quit or love.event.push
     f('q')
  end
...
  if key == 'escape' then quit() end
Notice that love.event.quit doesn't take any parameters. But it will not complain if you shove it a 'q'.

Re: Version independent quitting

Posted: Sat Mar 31, 2012 5:12 pm
by slime
0.8.0 *does* have love.event.push, however love.event.push("q") was changed to love.event.push("quit") and love.event.quit() was added as well.

Re: Version independent quitting

Posted: Sat Mar 31, 2012 10:17 pm
by kikito
Good to know. I don't use 0.8.x myself, so I have no way to test that. But the code above works on 0.7.x, and I got confirmation that it also works in 0.8.x.

Re: Version independent quitting

Posted: Sat Mar 31, 2012 10:50 pm
by Inny
Out of curiosity, would returning from love.run be an acceptable way to quit, or is love.audio.stop in a quit event a necessary shutdown action?

Re: Version independent quitting

Posted: Sun Apr 01, 2012 8:40 am
by T-Bone
kikito wrote:Someone pointed out that 0.8.x doesn't have love.event.push - it's love.eventquit .

Here's the bit I'm using now:

Code: Select all

  function quit()
     local f = love.event.quit or love.event.push
     f('q')
  end
...
  if key == 'escape' then quit() end
Notice that love.event.quit doesn't take any parameters. But it will not complain if you shove it a 'q'.
That works regardless of wether 0.8 has love.event.push (which it does), your code simply test wether or not "love.event.quit" exists, and uses that if possible. That's a smart solution.

love.event.quit is not listed in https://love2d.org/wiki/love.event. That should probably be changed.

Re: Version independent quitting

Posted: Sun Apr 01, 2012 1:13 pm
by richapple
Even easier:

Code: Select all

if key == "escape" then
	return love.event.push("quit") or love.event.push("q")
end

Re: Version independent quitting

Posted: Sun Apr 01, 2012 5:55 pm
by Robin
Interesting. It works because LÖVE 0.7.* ignores unknown events where 0.8.0 gives an error.

My favourite is:

Code: Select all

(love.event.quit or love.event.push) 'q'
;)

Re: Version independent quitting

Posted: Sun Apr 01, 2012 6:25 pm
by slime
I think love.event.quit is actually slightly different from love.event.push("quit"/"q"). The former will quit LÖVE right away, while the latter will wait until the next time events are polled, which could be nearly an entire frame. Usually it won't matter though.

Disregard, I mis-remembered!