How to refer to a table inside another table?

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.
User avatar
szmol96
Citizen
Posts: 51
Joined: Mon Oct 07, 2013 4:24 pm
Location: Horvátkút, Hungary

How to refer to a table inside another table?

Post by szmol96 »

Hi. I have this table, where I store my map segments and their data:

Code: Select all

map = {
	a1 = {
		x = 0,
		y = 0,
		width = 640,
		height = 480,
		image = love.graphics.newImage("img/mapa1.png"),
		data = love.image.newImageData("img/mapa1.png")
	},
	a2 = {
		x = a1.x + a1.width, --this is line 11
		y = a1.y,
		width = 640,
		height = 480,
		image = love.graphics.newImage("img/mapa2.png"),
		data = love.image.newImageData("img/mapa2.png")
	},
	b1 = {
		x = a1.x,
		y = a1.y + a1.height,
		width = 640,
		height = 480,
		image = love.graphics.newImage("img/mapb1.png"),
		data = love.image.newImageData("img/mapb1.png")
	},
	b2 = {
		x = a1.x + a1.width,
		y = a1.y + a1.height,
		width = 640,
		height = 480,
		image = love.graphics.newImage("img/mapb2.png"),
		data = love.image.newImageData("img/mapb2.png")
	}
}
But i get this error: "map.lua:11:attempt to index global 'a1' (a nil value)".
If I put "map" there, like this:

Code: Select all

x = map.a1.x + a1.width,
I get another error, which says: "map.lua:11:attempt to index global 'map' (a nil value)"
So, how do I refer to 'a1'?
All your code are belong to us.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to refer to a table inside another table?

Post by Robin »

Well, you can't refer to a table when it doesn't exist yet, so the solution is this:

Code: Select all

map = {
	a1 = {
		x = 0,
		y = 0,
		width = 640,
		height = 480,
		image = love.graphics.newImage("img/mapa1.png"),
		data = love.image.newImageData("img/mapa1.png")
	},
	a2 = {
		width = 640,
		height = 480,
		image = love.graphics.newImage("img/mapa2.png"),
		data = love.image.newImageData("img/mapa2.png")
	},
	b1 = {
		width = 640,
		height = 480,
		image = love.graphics.newImage("img/mapb1.png"),
		data = love.image.newImageData("img/mapb1.png")
	},
	b2 = {
		width = 640,
		height = 480,
		image = love.graphics.newImage("img/mapb2.png"),
		data = love.image.newImageData("img/mapb2.png")
	}
}
map.a2.x = map.a1.x + map.a1.width
map.a2.y = map.a1.y
map.b1.x = map.a1.x
map.b1.y = map.a1.y + map.a1.height	
map.b2.x = map.a1.x + map.a1.width
map.b2.y = map.a1.y + map.a1.height
Help us help you: attach a .love.
User avatar
szmol96
Citizen
Posts: 51
Joined: Mon Oct 07, 2013 4:24 pm
Location: Horvátkút, Hungary

Re: How to refer to a table inside another table?

Post by szmol96 »

Oh, thank you Sir!

EDIT: Oh, and also I need some help with this function. The game says "attempt to index local 'image' (a function value)"

Code: Select all

function maskedHLine(x, y, length, image)
	for i = 1, length do
		local r, g, b, a = image:getPixel( x + i, y )
		if a > 0 then
			return true
		end
	end
	return false
end
It is in connection with this piece of code:

Code: Select all

if maskedHLine(player.x - 11, player.y + 16 + player.ySpeed * dt, 22, currMapSegment) == false then
		if player.ySpeed < player.maxFallSpeed then
			player.ySpeed = player.ySpeed + (1000 * dt)
		end
		player.onGround = false
	else
		player.ySpeed = 0
		player.onGround = true
	end
And I get currMapSegment from this function:

Code: Select all

function currMapSegment()
	local currMapSegment
	local i, segment
	for i, segment in ipairs(map) do
		if player.x >= segment.x and player.x <= segment.x + segment.width and player.y >= segment.y and player.y <= segment.y + segment.height then
			currMapSegment = segment
		end
	end
	return currMapSegment
end
How to solve this one?
All your code are belong to us.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to refer to a table inside another table?

Post by Robin »

Like the error says, currMapSegment is a function.

So:

Code: Select all

if not maskedHLine(player.x - 11, player.y + 16 + player.ySpeed * dt, 22, currMapSegment()) then
(Generally, you'd want to use not something instead of something == false. Boolean logic FTW!)
Help us help you: attach a .love.
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: How to refer to a table inside another table?

Post by ejmr »

Robin wrote:(Generally, you'd want to use not something instead of something == false. Boolean logic FTW!)
I disagree. Every one of the following results in boolean false:

Code: Select all

print(1 == false)
print(0 == false)
print(true == false)
print(" " == false)
print(not 1)
print(not 0)
print(not true)
print(not " ")
So I do not believe there is an inherent readability difference between ‘==’ and ‘not’.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: How to refer to a table inside another table?

Post by Robin »

ejmr wrote:So I do not believe there is an inherent readability difference between ‘==’ and ‘not’.
Wat. Your conclusion does not follow from your argument, or you missed a step in between.

People using == true and == false are the bane of my existence.
Help us help you: attach a .love.
foo0
Prole
Posts: 35
Joined: Sun Apr 27, 2014 10:25 am

Re: How to refer to a table inside another table?

Post by foo0 »

Code: Select all

if stuff == not false then
	print("blood boiling")
end
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: How to refer to a table inside another table?

Post by Ref »

Code: Select all

happy = true
if happy then print( 'Smile ') end
User avatar
Exasperation
Prole
Posts: 11
Joined: Sat Oct 02, 2010 7:11 pm

Re: How to refer to a table inside another table?

Post by Exasperation »

Wait a second... this:

Code: Select all

not nil
returns true while this:

Code: Select all

nil == false
returns false.

So, since the two options behave differently for the same input, shouldn't we use the one that provides the desired behavior in whatever particular situation we're using it?
User avatar
szmol96
Citizen
Posts: 51
Joined: Mon Oct 07, 2013 4:24 pm
Location: Horvátkút, Hungary

Re: How to refer to a table inside another table?

Post by szmol96 »

I have difficulties again... :oops: The same function gives me a nil value error. It says: "attempt to index local 'image' (a nil value)".
What do I do now?
All your code are belong to us.
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests