It looks like you're new here. If you want to get involved, click one of these buttons!
I'm trying to learn about tables within tables and how to reference variables within.
The following code errors out with main:38 attempt to index a number value (field '?')
which is this line: text(globalIndex[x][y].vA, codX, codY)
where I'm trying to print the variable vA located in globalIndex[x][y]
I had this working and then started to do other things. then it stopped, and what I thought I understood, now I don't.
Can someone explain what's going on and what I'm doing wrong?
function setup()
math.randomseed( os.time()) -- Generate random seed
w = 20
h = 20
globalIndex = {}
fontSize(10)
-- generate
for x = 1 , w do
globalIndex[x] = {}
for y = 1 , h do
globalIndex[x][y] = populateCell(x,y)
end
end
end
function draw()
-- display
for x = 1 , w do
for y = 1 , h do
local codX = (x * 15)
local codY = (y * 15) + 300
text(globalIndex[x][y].vA, codX, codY)
end
end
end
function populateCell(x,y)
print("Populating:" .. x .." " .. y)
local vA = math.random(0,255)
local vB = math.random(0,255)
local vC = math.random(0,255)
local vD = math.random(0,255)
local vE = math.random(0,255)
return vA, vB, vC, vD, vE
end
Comments
You are returning a list from the function,you need to return some kind of table. This should work:
return {vA = vA, vB = vB, vC = vC, vD = vD, vE = vE}
Makes perfect sense. Thanks Yojimbo.