【问题标题】:What's the correct way to access tables in lua?在lua中访问表的正确方法是什么?
【发布时间】:2018-03-02 20:39:08
【问题描述】:

我决定利用空闲时间做一些有趣的活动,为 Garry's Mod 制作一些有趣的插件,角色扮演。

我正在尝试制作的插件是允许特定门出租,然后当有人租用该门时,主要所有者将收到定期付款!

我遇到了一些问题,即无法访问该表。

-- 门是一个元函数,是所有实体的属性。 -- 在这种情况下,它属于门。

cRentableDoors = cRentableDoors or {} -- Global table

    function door:makeRentableSub( doorParent )
    -- GetNWBool() is a function used to network information
    -- on entities from server to client. ( so we can draw HUDs etc )

        if self:GetNWBool( "IsRentable" ) == false or nil then
                self:SetNWBool( "IsRentable", true ) 
                self:SetNWBool( "IsSub", true ) 

                cRentableDoors[doorParent:MapCreationID()] = {
                    MainDoor = doorParent,
                    SubDoors = {{ent = self, key = self:MapCreationID()}}
                }

        end
    end

只有一个问题。当我想添加几个子门时,'SubDoors' 表只打印 1 个索引。我在这里覆盖了什么吗?

如有任何疑问,请随时提出!

谢谢!

【问题讨论】:

  • SubDoors = {ent = self, key = self:MapCreationID()} 剥离另一个 {}。目前SubDoors 是一个表。
  • 非常感谢,谢谢!
  • @HenriMenke,把它变成一个答案,这样它就可以被接受并且更容易被其他人找到。

标签: lua garrys-mod


【解决方案1】:

问题出在一行

SubDoors = {{ent = self, key = self:MapCreationID()}}

这里有两层大括号,这使得这个表达式等价于

SubDoors = { [1] = { ent = self, key = self:MapCreationID() } }

这是一个包含一个条目的表。只需去掉外面的一对括号就可以了,即

SubDoors = {ent = self, key = self:MapCreationID()}

【讨论】:

    猜你喜欢
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-16
    相关资源
    最近更新 更多