我想用两条信息制作动态播放器表。作为字符串提供的播放器的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
答案 0 :(得分:2)
所以你基本上想要在现有的表中添加一个新的播放器。如果是这样,那就这么简单:
theTable[key] = value
在您的情况下,如果蒸汽ID存储在ply
中,那么我们只需要使用密钥adminbot_players
将您的值添加到表ply
。在这种情况下,它将是:
adminbot_players[ply] = 5
答案 1 :(得分: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 ...
循环中,任何经过测试的密钥都将被定义为唯一。
答案 2 :(得分:0)
我找到了使用" table.insert"的其他方法。
我为用于搜索所选玩家的值的玩家制作了自定义ID。
不幸的是我使用了两张桌子,但这样做更容易。
小架构:
谢谢你的回答。这很有帮助:))
答案 3 :(得分: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
答案 4 :(得分:0)
所以,如果我正确理解你想要的东西......
你想要计算每个球员的队伍技能,并希望这个值能够持久 然而,你的方法不是很理想。 teamkill计数器应该是玩家的一部分。这也会简化和缩短您的代码。
这是一个如何做到这一点的例子。
-- 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
中的客户端(不>)上运行此代码请注意:您必须手动将计数器同步到客户端,这应该不是什么大问题。