【问题标题】:Lua property accessorLua 属性访问器
【发布时间】:2014-11-05 15:20:33
【问题描述】:

我对 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 的“文档”中不清楚这是什么。

【问题讨论】:

  • Lua 没有属性(你是说字段吗?)。此外,foo { ... }foo({...}) 的快捷方式(即它的函数调用)
  • 如果还是不行,请出示 Xpl3dSwitch 的代码。
  • 是的,我的意思是字段...不幸的是我没有 Xpl3dSwitch 的完整代码。
  • 那我们真的帮不上忙。 Xpl3dSwitch 对传递的表所做的是它自己的事情;它可能会完全忽略它。

标签: properties lua


【解决方案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}

【讨论】:

    猜你喜欢
    • 2011-05-09
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-08
    相关资源
    最近更新 更多