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:
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.
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
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:
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]
...
...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.