1

我想用两条信息制作动态玩家表。以字符串形式提供的玩家 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
4

5 回答 5

2

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

theTable[key] = value

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

adminbot_players[ply] = 5
于 2016-10-12T23:36:07.410 回答
1

要附加新密钥,请使用: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 ...循环中,任何经过测试的密钥都将被定义为唯一的。

于 2016-10-12T23:44:15.983 回答
0

此行的 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
于 2016-10-23T01:31:43.680 回答
0

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

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

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

-- 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()

于 2017-07-04T14:02:18.280 回答
0

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

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

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

于 2016-10-13T17:33:10.003 回答