# Table oddness

This has passed my “two hours of fruitless attempts to fix” threshold, so I’m wondering if anyone can tell me what I’m doing wrong. This code has been stripped of much of its context, so I don’t think an explanation of my overall aims will help. I know bits of it seem circuitous, but there’s a historical reason for it. The variable names will get overhauled at some point but that’s going to take a bit of time in the actual code and I don’t want to do it while this is (to my mind) behaving oddly.

My aim is to get the bubble instance “b” to generate some attributes self.colr, self.gravity and self.radiustarget. In the actual code there are many more attributes, but these three illustrate the point. Self.colr and self.gravity work fine but the attempt to generate self.radiustarget throws an error.

The print statements were in there to help me work out exactly when it was going wrong.

All help extremely gratefully received!

``````

--# Bubble
Bubble = class()

function Bubble:init(x)
-- you can accept and set parameters here
self:createSpecific()
end

function Bubble:createSpecific()

for key, value in pairs(Table) do
local z = Bubbleattribs[key]
print(Table[key])
if (Table[key].SEQUENCE) then
print("yes1")
local zz = math.random(#Table[key].SEQUENCE)
print("yes2")
self.z = Table[key].SEQUENCE[zz]
print(key.." done")
else
print(value)
self.z = value
print(key.." done")
end
end
end

--# Main

-- Use this function to perform your initial setup
function setup()
Table = {

size = 40, colour = color(255, 66, 0, 255), density = {SEQUENCE = {0.3, 0.6}}

}

Bubbleattribs = {colour = "colr", density = "gravity",size = "radiustarget"}

b = Bubble()
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)

-- This sets the line thickness
strokeWidth(5)

-- Do your drawing here

end

``````

This code seems unnecessarily complex, but here is what to change: replace

``````        if (Table[key].SEQUENCE) then
``````

by

``````        if (type(value)=="table" and value.SEQUENCE) then
``````

when value is 40, you cannot address value.SEQUENCE because 40 is not a table.

I was getting confused because self.color kept being defined correctly, but thinking about it colors ARE tables in Codea, so it’s not invalid to ask if .SEQUENCE is defined in that table. Thanks @Jmv38.

(And yes, it is unnecessarily complex, but that will change!)

@epicurus101 No, colors aren’t tables. They are `userdata`.