据我了解,您想在游戏进行时在不同时间为玩家添加击杀或死亡或更换武器。
在您的情况下,您应该使用 OOP,它很容易在表中添加、删除和更改值。
当每位玩家加入您的游戏或服务器时,您必须为其提供一个 id。
local data = {}
function data:AddNewPlayer()
data[#data+1] = {
weapon = 'ak',
kills = 0,
deaths = 0
}
return #data -- returns player id
end
现在我们有了一个函数,当玩家加入我们的游戏时,它会为玩家提供一个数据 id。
所以我们添加了一些函数来添加和获取它们:
local data = {}
function data:AddNewPlayer()
data[#data+1] = {
weapon = 'ak',
kills = 0,
deaths = 0
}
return #data -- returns player id
end
-- Kills
function data:AddPlayerKill(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
self[playerId].kills = self[playerId].kills + 1 -- self is data table and self[playerId] is data[given id], So we add 1 new kill.
return true
end
return false
end
return false
end
function data:GetPlayerKills(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
return data[playerId].kills
end
return false
end
return false
end
还有死亡,我们应该对他们做同样的事情:
local data = {}
function data:AddNewPlayer()
data[#data+1] = {
weapon = 'ak',
kills = 0,
deaths = 0
}
return #data -- returns player id
end
-- Kills
function data:AddPlayerKill(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
self[playerId].kills = self[playerId].kills + 1 -- self is data table and self[playerId] is data[given id], So we add 1 new kill.
return true
end
return false
end
return false
end
function data:GetPlayerKills(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
return data[playerId].kills
end
return false
end
return false
end
-- Deaths
function data:AddPlayerDeath(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
self[playerId].deaths = self[playerId].deaths + 1 -- self is data table and self[playerId] is data[given id], So we add 1 new death.
return true
end
return false
end
return false
end
function data:GetPlayerDeaths(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
return data[playerId].deaths
end
return false
end
return false
end
对于武器,我们添加了一些函数来改变玩家的武器并将其作为字符串获取。
local data = {}
function data:AddNewPlayer()
data[#data+1] = {
weapon = 'ak',
kills = 0,
deaths = 0
}
return #data -- returns player id
end
-- Kills
function data:AddPlayerKill(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
self[playerId].kills = self[playerId].kills + 1 -- self is data table and self[playerId] is data[given id], So we add 1 new kill.
return true
end
return false
end
return false
end
function data:GetPlayerKills(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
return data[playerId].kills
end
return false
end
return false
end
-- Deaths
function data:AddPlayerDeath(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
self[playerId].deaths = self[playerId].deaths + 1 -- self is data table and self[playerId] is data[given id], So we add 1 new death.
return true
end
return false
end
return false
end
function data:GetPlayerDeaths(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
return data[playerId].deaths
end
return false
end
return false
end
-- Weapon
function data:SetPlayerWeapon(playerId,newWeapon)
if playerId and type(playerId)=="number" then
if data[playerId] then
if newWeapon and type(newWeapon)=="string" then
data[playerId].weapon = newWeapon
return true
end
return false
end
return false
end
return false
end
function data:GetPlayerWeapon(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
return data[playerId].weapon
end
return false
end
return false
end
最后,让我们试试吧:
local data = {}
function data:AddNewPlayer()
data[#data+1] = {
weapon = 'ak',
kills = 0,
deaths = 0
}
return #data -- returns player id
end
-- Kills
function data:AddPlayerKill(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
self[playerId].kills = self[playerId].kills + 1 -- self is data table and self[playerId] is data[given id], So we add 1 new kill.
return true
end
return false
end
return false
end
function data:GetPlayerKills(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
return data[playerId].kills
end
return false
end
return false
end
-- Deaths
function data:AddPlayerDeath(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
self[playerId].deaths = self[playerId].deaths + 1 -- self is data table and self[playerId] is data[given id], So we add 1 new death.
return true
end
return false
end
return false
end
function data:GetPlayerDeaths(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
return data[playerId].deaths
end
return false
end
return false
end
-- Weapon
function data:SetPlayerWeapon(playerId,newWeapon)
if playerId and type(playerId)=="number" then
if data[playerId] then
if newWeapon and type(newWeapon)=="string" then
data[playerId].weapon = newWeapon
return true
end
return false
end
return false
end
return false
end
function data:GetPlayerWeapon(playerId)
if playerId and type(playerId)=="number" then
if data[playerId] then
return data[playerId].weapon
end
return false
end
return false
end
-- Let's try it:
local myPlayer = data:AddNewPlayer() -- returned 1
local myPlayer2 = data:AddNewPlayer() -- returned 2
local myPlayer3 = data:AddNewPlayer() -- returned 3
data:AddPlayerKill(myPlayer) -- We have a specific player with a returned id (myPlayer) and we have add a new kill to his table id.
data:AddPlayerDeath(myPlayer) -- We have a specific player with a returned id (myPlayer) and we have add a new death to his table id.
data:AddPlayerDeath(myPlayer) -- We have a specific player with a returned id (myPlayer) and we have add a new death to his table id.
data:SetPlayerWeapon(myPlayer,"M4") -- We have a specific player with a returned id (myPlayer) and we have changed his weapon to M4.
local playerKills = data:GetPlayerKills(myPlayer)
local playerDeaths = data:GetPlayerDeaths(myPlayer)
local playerWeapon = data:GetPlayerWeapon(myPlayer)
print("Kills: "..playerKills) -- Output : Kills: 1
print("Deaths: "..playerDeaths) -- Output : Deaths: 2
print("Weapon: "..playerWeapon) -- Output : Weapon: M4