function countWalls(tiles)
local walls = 0
for x = self.x - 1, self.x + 1 do
for y = self.y - 1, self.y + 1 do
if tiles[x][y] == nil then -- doesn't work, but needs to
walls = walls + 1
elseif tiles[x][y].blocked == true then
walls = walls + 1
end
end
end
return walls
end
line 28: attempt to index field '?' ( a nil value )
Now, I've actually been able to rectify this pretty easily by adding code to the function to ignore border tiles. That being said, I would really like to know of an elegant way to check whether or not a variable might exist without it throwing an error when it doesn't. I'll post up a sample of my game once I get this refactor done.
You've got the right idea. The problem is that something in the first dimension of your 2d array is nil. To prevent this from causing an error you need to change your code here:
Kadoba wrote:You've got the right idea. The problem is that something in the first dimension of your 2d array is nil. To prevent this from causing an error you need to change your code here: