I don't understand "Attempting to index a nil value"
Posted: Tue Jun 17, 2014 12:14 am
I am making a simple ASCII game and I am now trying to implement wall detection. I have looked at multiple online forums and wikis attempting to make sense of what they are explaining, but I am just not quite comprehending it. Perhaps I am not on the same thinking pattern? What I am attempting to do is pass the stage outline, a 2D array of 1s and 0s to my player function and then checking the players position against the 2D array to see if the next tile will be a 0 or 1. The program runs, however, when I go to move I get this error:
What I am actually attempting to do is below (Where I get "outline" from my stage.lua and pass it to my player.lua in the main.lua file)
stage.lua (EDIT: I added more of my stage.lua)
main.lua
player.lua
Below I have also attache the .love file. I feel like this should be right but am not quite sure what is messing up. Ive been messing up alot but I with yalls help and the wikis help ive been able to get a lot done. Thanks again for the help!
What I am actually attempting to do is below (Where I get "outline" from my stage.lua and pass it to my player.lua in the main.lua file)
stage.lua (EDIT: I added more of my stage.lua)
Code: Select all
stage = {}
function stage.load()
--Unit Declaration
stage.w = 20
stage.h = 20
stage.unit_w = 10
stage.unit_h = 16
stage.offset_x = 40
stage.offset_y = 40
--Stage Outline
stage.outline = {
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1},
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
}
end
function stage.get_outline()
return stage.outline
end
Code: Select all
player.get_outline(stage.get_outline())
Code: Select all
function player.get_outline( outline )
outline = outline --Outline should be a 2D array
end
function player.keypressed( key )
--Checks
if key == "up" then
if outline[player.y - 16 - 72][player.x] == 0 then --72 is the starting location of the player and 16 is the size of each tile (y-values)
player.y = player.y - 16 --58 is the starting location and 10 is the tile size (x-values)
end
end
if key == "down" then
if outline[player.y + 16 - 72][player.x] == 0 then
player.y = player.y + 16
end
end
if key == "left" then
if outline[player.y][player.x - 10 - 58] == 0 then
player.x = player.x - 10
end
end
if key == "right" then
if outline[player.y][player.x + 10 - 58] == 0 then
player.x = player.x + 10
end
end
end