【问题标题】:Table keys and values in Lua - don't know howLua中的表键和值 - 不知道如何
【发布时间】:2016-10-12 23:10:45
【问题描述】:

我想用两条信息制作动态玩家表。以字符串形式提供的玩家 SteamID 将用作键,值应为数字。

应该看起来像table = { "ExampleSteamID" = 3, "ExampleSteamID2" = 4 }

我找到了类似table.insert(table, { key = x, value = z}) 的东西,但它不起作用。

gameevent.Listen("player_connect")

local function AdminBotOnJoinCheck(data)
    local ply = data.networkid -- SteamID of joining player
    local tableisempty = true -- some random stuff
    for k, tableply in pairs(adminbot_players) do --checking for players already writed to table, maybe need fix
        if not ply == tableply then
            --need code here
            MsgC("\nAdminBot: Player table updated | ", ply, "\n")
        end
        tableisempty = false --clear table = table break - just dont execute code. 
    end
    if tableisempty == true then
        --here same code
        MsgC("\nAdminBot: Player table updated | ", ply, "\n")
    end
    if file.Exists("adminbotplayers.txt", "DATA") == true and adminbot_teamkills_use_file == true then -- Random stuff for file writing
        local adminbot_players_json = util.TableToJSON(adminbot_players)
        file.Write("adminbotplayers.txt", adminbot_players_json)
    end
end

【问题讨论】:

  • 我强烈建议您阅读Programming in Lua,从前到后 - 第 2.5 章对您非常有用。考虑阅读参考手册,尤其是tables 及其manipulation 上的部分。
  • 我不能在 gmod 服务器的脚本中使用 "::=" :/
  • ::=BNF 的一部分,该符号用于描述 Lua 的语法,意思是“定义为”。 = 用于语言中的赋值。

标签: lua lua-table garrys-mod


【解决方案1】:

所以你基本上想在现有的桌子上添加一个新玩家。如果是这样,那么就这么简单:

theTable[key] = value

在您的情况下,如果 Steam ID 存储在 ply 中,那么我们只需使用键 ply 将您的值添加到表 adminbot_players 中。在这种情况下,它将是:

adminbot_players[ply] = 5

【讨论】:

  • Table.insert 仅适用于数字索引,在字典上使用 insert 没有任何意义
【解决方案2】:

要附加新密钥,请使用:table[newkey] = newvalue

您对唯一值的实现不正确:

local yourwantedkeydefinedsomewhere = "yourwantedkeydefinedsomewhere"
local found = false
for k, tableply in pairs(adminbot_players) do --checking for players already writed to table, maybe need fix
           if ply == tableply then
                  found = true
                     break
            end
           tableisempty = false --clear table = table break - just dont execute code. 
    end

       if not found then
            adminbot_players[yourwantedkeydefinedsomewhere] = ply
            MsgC("\nAdminBot: Player table updated | ", ply, "\n")
       end

在您的情况下,for k,table ... 循环任何测试的密钥都将被定义为唯一的。

【讨论】:

    【解决方案3】:

    我找到了使用“table.insert”的其他方法。 我为玩家制作了自定义 ID,用于搜索选定玩家的值。 不幸的是,我使用了两张桌子,但这样做更容易。
    小架构:

    • 搜索播放器。如果存在将表号保存到变量中。
    • 如果不存在则添加新播放器。同时保存号码
    • 为具有相同 ID 的其他表添加值。

    感谢您的回答。这很有帮助:)

    【讨论】:

      【解决方案4】:

      这一行的 Lua 语法不正确

      table = { "ExampleSteamID" = 3, "ExampleSteamID2" = 4 }
      

      正确方法:

      table = { ["ExampleSteamID"] = 3, ["ExampleSteamID2"] = 4 }
      

      您的代码的简化版本(希望我的想法正确)

      adminbot_players = {}
      local function AdminBotOnJoinCheck(data)
          local ply = data.networkid
          adminbot_players[ply] = { ["ExampleSteamID"] = 3, ["ExampleSteamID2"] = 4 };
          MsgC("\nAdminBot: Player table updated | ", ply, "\n");
          if file.Exists("adminbotplayers.txt", "DATA") and adminbot_teamkills_use_file then
              file.Write("adminbotplayers.txt", util.TableToJSON(adminbot_players))
          end
      end
      

      【讨论】:

        【解决方案5】:

        所以如果我正确理解你想要得到什么......

        您想要计算每个玩家的团队杀伤力,并且还希望该值保持不变。
        但是,您的方法不是很理想。团队杀伤计数器应该是玩家的一部分。这也将简化和缩短您的代码。

        这是一个示例,您可以如何做到这一点。

        -- You want to run this ASAP after the server is started and the world loaded
        
        local playerMeta = FindMetaTable( "Player" ) -- get the meta table for player
        
        function playerMeta:AddTeamkill() -- this function will increase the counter
            local teamkills = self:GetPData("teamkills", 0)
            self:SetPData("teamkills", teamkills + 1)
        end
        
        function playerMeta:GetTeamkills() -- this function will return the current counter
            return self:GetPData("teamkills", 0)
        end
        
        -- Feel free to add more functions here... Maybe a 'SetTeamkills' or 'ResetTeamkills'
        

        然后你可以像这样使用它:

        -- Add a teamkill
        local player_that_teamkilled = player.GetByID(1) -- you obviously would get the player a different way...
        player_that_teamkilled:AddTeamkill()
        
        -- Get teamkills
        local somePlayer = player.GetByID(1) -- same as above
        somePlayer:GetTeamkills()
        

        您不必乱用代码来加载和保存计数器。您也没有与坐在不同桌子上的玩家相关的信息。这意味着您不必跟踪表中的哪一行属于谁。这意味着,您不必在(可能很大的)表中搜索正确的行来修改(或读取)一个数字。

        计数器将保存到 sv.db 中,或者如果您在 cl.db 中的客户端上运行此代码(您应该这样做)

        请注意:您必须手动将计数器同步到客户端,这应该没什么大不了的。

        延伸阅读:
        FindMetaTable()
        Player:GetPData()
        Player:SetPData()

        【讨论】:

          猜你喜欢
          • 2012-09-08
          • 1970-01-01
          • 1970-01-01
          • 2011-07-12
          • 1970-01-01
          • 1970-01-01
          • 2020-09-16
          • 2012-10-01
          • 2018-12-11
          相关资源
          最近更新 更多