Lua物业访问者

时间:2014-11-02 23:32:08

标签: properties 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 个答案:

答案 0 :(得分: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}

相关问题