Why this error on love.event.quit() ?

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.
Post Reply
User avatar
mr_happy
Citizen
Posts: 78
Joined: Fri Mar 18, 2016 8:57 pm

Why this error on love.event.quit() ?

Post by mr_happy » Thu May 17, 2018 8:24 am

In function love.keypressed() the first line is:

Code: Select all

if key == 'escape' then love.event.quit() end
If I close the game window using the escape key I get this:

Code: Select all

Error: main.lua:149: attempt to index a nil value
stack traceback:
	[string "boot.lua"]:637: in function '__index'
	main.lua:149: in function <main.lua:126>
	[string "boot.lua"]:503: in function <[string "boot.lua"]:493>
Line 149, which is a little further down love.keypressed(), is:

Code: Select all

local c = town[tY][tX]
The program (only about 300 lines) works as expected without error and If I close the window using the mouse, the program exits normally. So what's going on with love.event.quit()? Should I be doing some sort of clean up or exiting elsewhere? I'm using Zerobrane Studio on Linux 64 bit if that's relevant.

TIA

User avatar
Nixola
Inner party member
Posts: 1904
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Why this error on love.event.quit() ?

Post by Nixola » Thu May 17, 2018 8:29 am

Can you post the whole code?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics

User avatar
mr_happy
Citizen
Posts: 78
Joined: Fri Mar 18, 2016 8:57 pm

Re: Why this error on love.event.quit() ?

Post by mr_happy » Thu May 17, 2018 9:49 am

Nixola wrote:
Thu May 17, 2018 8:29 am
Can you post the whole code?
I'd prefer not to at the moment as it contains the tiniest grain of an idea I'd like to keep to myself atm :D

I moved

Code: Select all

if key == 'escape' then love.event.quit() end
to the end of love.keypressed() but the result is exactly the same. I don't see how line 149 is being executed after the call to love.event.quit().

User avatar
mr_happy
Citizen
Posts: 78
Joined: Fri Mar 18, 2016 8:57 pm

Re: Why this error on love.event.quit() ?

Post by mr_happy » Thu May 17, 2018 10:02 am

Right, I've found what causes the error but not why it only happens when exiting with the keyboard.

Code: Select all

function love.keypressed(key)
 
  if key == 'escape' then love.event.quit() end
 
  -- HERE'S THE PROBLEM:
  local tX, tY = 0, 0
  
  if key == "left" then
    tX = player.x - 1
    tY = player.y
  end
  if key == "right" then
    tX = player.x + 1
    tY = player.y
  end   
  if key == "up" then
    tX = player.x
    tY = player.y - 1
  end    
  if key == "down" then
    tX = player.x
    tY = player.y + 1
  end   

  local c = town[tY][tX]
  ...  
I was initialising tX, tY to zero which, if used to index the table, obviously causes an error. However that indexing of town{} only happens when exiting using the keyboard (and why??). If I initialise them to '1', the problem disappears. If I don't initialise them I still get the error only when exiting via 'esc'. I don't understand :huh:

grump
Party member
Posts: 318
Joined: Sat Jul 22, 2017 7:43 pm

Re: Why this error on love.event.quit() ?

Post by grump » Thu May 17, 2018 10:07 am

mr_happy wrote:
Thu May 17, 2018 9:49 am
I don't see how line 149 is being executed after the call to love.event.quit().
love.event.quit() places a 'quit' event in the event queue. It does not terminate the program immediately. Use os.exit() for that.

User avatar
mr_happy
Citizen
Posts: 78
Joined: Fri Mar 18, 2016 8:57 pm

Re: Why this error on love.event.quit() ?

Post by mr_happy » Thu May 17, 2018 10:44 am

grump wrote:
Thu May 17, 2018 10:07 am
mr_happy wrote:
Thu May 17, 2018 9:49 am
I don't see how line 149 is being executed after the call to love.event.quit().
love.event.quit() places a 'quit' event in the event queue. It does not terminate the program immediately. Use os.exit() for that.
OK thanks. I thought it might be something to do with that but I thought it would most likely return immediately and proceed to pop off any other outstanding events rather than continue executing code in the current callback.
I suppose:

Code: Select all

if key == 'escape' then love.event.quit() return end
would do the same job as long as it appears before the table is indexed.

I notice that os.exit() also has a slightly different effect when exiting with keyboard :

Code: Select all

AL lib: (EE) alc_cleanup: 1 device not closed
...which doesn't happen when closing the window. Some SDL thing perhaps...?

EDIT: I also wrapped the table access as I just realised it wouldn't work as desired if I pressed spurious keys (oops).

Code: Select all

function love.keypressed(key)
 
   if key == 'escape' then os.exit() end

  local tX, tY
  if key == "left" then
    tX = player.x - 1
    tY = player.y
  elseif key == "right" then
    tX = player.x + 1
    tY = player.y
  elseif key == "up" then
    tX = player.x
    tY = player.y - 1
  elseif key == "down" then
    tX = player.x
    tY = player.y + 1
  end   

  if tX then
    local c = town[tY][tX]
    ...
   

grump
Party member
Posts: 318
Joined: Sat Jul 22, 2017 7:43 pm

Re: Why this error on love.event.quit() ?

Post by grump » Thu May 17, 2018 11:16 am

mr_happy wrote:
Thu May 17, 2018 10:44 am
I notice that os.exit() also has a slightly different effect when exiting with keyboard :

Code: Select all

AL lib: (EE) alc_cleanup: 1 device not closed
...which doesn't happen when closing the window. Some SDL thing perhaps...?
I get the same message in my projects. It's probably some cleanup task inside love that is not running when terminating with os.exit(). I don't know of any other way to quit immediately though. I don't think it has any ill side effects.

User avatar
pgimeno
Party member
Posts: 1060
Joined: Sun Oct 18, 2015 2:58 pm

Re: Why this error on love.event.quit() ?

Post by pgimeno » Thu May 17, 2018 12:33 pm

In most cases it will terminate immediately after you return.

I really prefer to see things finish gracefully.

User avatar
bartbes
Sex machine
Posts: 4902
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Why this error on love.event.quit() ?

Post by bartbes » Thu May 17, 2018 4:02 pm

In this case the simple solution would be to return directly after posting the event, so just use

Code: Select all

if key == 'escape' then return love.event.quit() end
Cowardly acts and the eating of birds must not be the deeds of a Hero of Storms.

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests