Version independent quitting

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.
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Version independent quitting

Post 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.
User avatar
a_greed
Prole
Posts: 19
Joined: Mon Feb 20, 2012 7:08 pm

Re: Version independent quitting

Post 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
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Version independent quitting

Post 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'.
When I write def I mean function.
User avatar
slime
Solid Snayke
Posts: 3162
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Version independent quitting

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Version independent quitting

Post 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.
When I write def I mean function.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: Version independent quitting

Post 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?
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: Version independent quitting

Post 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.
User avatar
richapple
Citizen
Posts: 65
Joined: Sun Dec 25, 2011 10:25 am

Re: Version independent quitting

Post by richapple »

Even easier:

Code: Select all

if key == "escape" then
	return love.event.push("quit") or love.event.push("q")
end
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Version independent quitting

Post 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'
;)
Help us help you: attach a .love.
User avatar
slime
Solid Snayke
Posts: 3162
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Version independent quitting

Post 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!
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], zingo and 3 guests