Properties Lua属性访问器

Properties Lua属性访问器,properties,lua,Properties,Lua,我对Lua属性在我试图维护的一些代码中的工作方式感到困惑。在此之前,我在Lua文档中花了很多时间。所以 其中一个Lua表中有一个函数,如下所示(我们称之为“嵌套表”示例): 鉴于 a = Xpl3dSwitch { } a.position[0] = 27 a.position[1] = 0 itemTable.addItem(a) --this 'registers' the position properties …等等,似乎奏效了。为什么“嵌套表”示例中的位置表没有粘住 另外,关于“

我对Lua属性在我试图维护的一些代码中的工作方式感到困惑。在此之前,我在Lua文档中花了很多时间。所以

其中一个Lua表中有一个函数,如下所示(我们称之为“嵌套表”示例):

鉴于

a = Xpl3dSwitch { }

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

itemTable.addItem(a) --this 'registers' the position properties
…等等,似乎奏效了。为什么“嵌套表”示例中的位置表没有粘住


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

查看表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}

Lua没有属性(您是指字段吗?)。另外,
foo{…}
foo({…})
的一个快捷方式(即它是一个函数调用)。如果这还不能清除它,请显示Xpl3dSwitch的代码。是的,我指的是字段…不幸的是,我没有Xpl3dSwitch的完整代码。那么我们就无能为力了。
Xpl3dSwitch
对传递的表所做的是它自己的事情;它可能完全忽略了这一点。
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()