Page 1 of 1

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

Posted: Thu May 17, 2018 8:24 am
by mr_happy
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

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

Posted: Thu May 17, 2018 8:29 am
by Nixola
Can you post the whole code?

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

Posted: Thu May 17, 2018 9:49 am
by mr_happy
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().

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

Posted: Thu May 17, 2018 10:02 am
by mr_happy
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:

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

Posted: Thu May 17, 2018 10:07 am
by grump
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.

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

Posted: Thu May 17, 2018 10:44 am
by mr_happy
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]
    ...
   

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

Posted: Thu May 17, 2018 11:16 am
by grump
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.

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

Posted: Thu May 17, 2018 12:33 pm
by pgimeno
In most cases it will terminate immediately after you return.

I really prefer to see things finish gracefully.

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

Posted: Thu May 17, 2018 4:02 pm
by bartbes
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