Small LUA question

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
zd
Prole
Posts: 5
Joined: Thu Mar 08, 2012 2:27 am

Small LUA question

Post by zd »

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.

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
My question is how exactly should

Code: Select all

if tiles[x][y] == nil then -- doesn't work, but needs to
Is there a handy and quick way to check whether or not a given variable exists?

I just keep getting

Code: Select all

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.
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Small LUA question

Post by veethree »

What's on line 28?

It'd help if you'd post the whole .love, That makes it easier for us to troubleshoot.
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: Small LUA question

Post by Kadoba »

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:

Code: Select all

if tiles[x][y] == nil then 
to this:

Code: Select all

if tiles[x] == nil or tiles[x][y] == nil then
Another trick is that nil is the only thing besides false that does not resolve to true in logical expressions, so you can do this as a shortcut:

Code: Select all

if not tiles[x] or not tiles[x][y] then
I created a grid class to make working with 2d arrays easier. Here it is if you're interested.
zd
Prole
Posts: 5
Joined: Thu Mar 08, 2012 2:27 am

Re: Small LUA question

Post by zd »

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:

Code: Select all

if tiles[x][y] == nil then 
to this:

Code: Select all

if tiles[x] == nil or tiles[x][y] == nil then
Another trick is that nil is the only thing besides false that does not resolve to true in logical expressions, so you can do this as a shortcut:

Code: Select all

if not tiles[x] or not tiles[x][y] then
I created a grid class to make working with 2d arrays easier. Here it is if you're interested.
Thanks man, this worked perfectly.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 2 guests