Small LUA question
Posted: Thu Mar 08, 2012 2:31 am
This isn't specific to LOVE, but I'm having trouble finding an answer elsewhere so I figured I'd give it a shot here.
My question is how exactly should
Is there a handy and quick way to check whether or not a given variable exists?
I just keep getting
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.
Code: Select all
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
Code: Select all
if tiles[x][y] == nil then -- doesn't work, but needs to
I just keep getting
Code: Select all
line 28: attempt to index field '?' ( a nil value )