Page 1 of 2
How to refer to a table inside another table?
Posted: Mon May 12, 2014 6:43 pm
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:
I get another error, which says: "map.lua:11:attempt to index global 'map' (a nil value)"
So, how do I refer to 'a1'?
Re: How to refer to a table inside another table?
Posted: Mon May 12, 2014 7:02 pm
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
Re: How to refer to a table inside another table?
Posted: Mon May 12, 2014 7:15 pm
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?
Re: How to refer to a table inside another table?
Posted: Mon May 12, 2014 8:15 pm
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!)
Re: How to refer to a table inside another table?
Posted: Tue May 13, 2014 2:32 am
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’.
Re: How to refer to a table inside another table?
Posted: Tue May 13, 2014 8:22 am
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.
Re: How to refer to a table inside another table?
Posted: Tue May 13, 2014 9:08 am
by foo0
Code: Select all
if stuff == not false then
print("blood boiling")
end
Re: How to refer to a table inside another table?
Posted: Tue May 13, 2014 3:05 pm
by Ref
Code: Select all
happy = true
if happy then print( 'Smile ') end
Re: How to refer to a table inside another table?
Posted: Tue May 13, 2014 3:22 pm
by Exasperation
Wait a second... this:
returns true while this:
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?
Re: How to refer to a table inside another table?
Posted: Tue May 13, 2014 5:24 pm
by szmol96
I have difficulties again...
The same function gives me a nil value error. It says: "attempt to index local 'image' (a nil value)".
What do I do now?