WolfOdrade
WolfOdrade

Reputation: 370

Lua property accessor

I'm confused by how Lua properties are working in some of the code I'm trying to maintain. I've spent a good amount of time in the Lua documentation before this. So...

There is a function in one of those Lua tables, like this (we'll call this the 'nested tables' example):

function addItem(item)

index = itemTable.getIndex(item.position[1], item.position[2])

itemTable.items[index] = item

end;


a = Xpl3dSwitch { position = { 27, 0, 1, 1} }
itemTable.addItem(a) --doesn't seem to 'register' the position property

whereas

a = Xpl3dSwitch { }

a.position[0] = 27
a.position[1] = 0

itemTable.addItem(a) --this 'registers' the position properties

...etc, seems to work. Why are the position tables not sticking in the 'nested table' example?

Also, regarding 'a = Xpl3dSwitch { }' - is it an object constructor? It's not clear from the Lua 'documentation' what this is.

Upvotes: 1

Views: 1058

Answers (1)

CHlM3RA
CHlM3RA

Reputation: 133

Look inside the table a and compare them. That should point you in the direction where the error happens.

to look inside a use something like:

function getTableContent(tab, str)
str = str or "table"
for i, v in pairs(tab) do
    if type(v) == "table" and v ~= _G then
        str = str.."->"..tostring(i)
        getTableContent(v, str)
    else
        print(str.."  Index: "..tostring(i).."  Value: "..tostring(v))
    end
end
end


getTableContent(a)
io.read()

Once you know how the working and the not working one are structured you should be able to make the adjustments needed.

Edit:

Also you could use:

a = Xpl3dSwitch { }

a.position = {27, 0, 1, 1}

Upvotes: 1

Related Questions