2014-11-02 60 views
1

我很困惑Lua属性在我试图维护的一些代码中如何工作。在此之前,我在Lua文档中花了很多时间。所以......Lua属性访问者

有那些Lua中的表的一个功能,像这样的(我们称其为 '嵌套表' 为例):

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 

a = Xpl3dSwitch { } 

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

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

...等,似乎工作。为什么位置表不会粘在'嵌套表'示例中?

此外,关于'a = Xpl3dSwitch {}' - 它是一个对象构造函数吗?从Lua的“文档”中不清楚这是什么。

+1

Lua中没有的属性(你的意思是田地?)。此外,'foo {...}'是'foo({...})'的快捷方式(即其函数调用) – 2014-11-02 23:36:33

+0

如果没有清除它,请显示Xpl3dSwitch的代码。 – 2014-11-02 23:43:50

+0

是的,我的意思是......不幸的是,我没有Xpl3dSwitch的完整代码。 – WolfOdrade 2014-11-03 02:52:22

回答

1

查看表格a并对其进行比较。这应该指出错误发生的方向。

看一个使用类似的内部:

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() 

一旦你知道如何在工作和不工作的一个是结构化,你应该能够做出必要的调整。

编辑:

你也可以使用:

A = Xpl3dSwitch {}

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