【问题标题】:Lua array of objectsLua 对象数组
【发布时间】:2021-12-20 06:12:28
【问题描述】:

我想要一个对象列表,我可以在列表顶部添加一个新对象,所以当我想要这个对象时,我会得到最新的对象。我对lua很陌生。我是怎么理解的。

--input
local dataName = 'deaths'
local data = {
    id = 1,
    weapon = 'ak'
}
--list
local list = {}

function addlist(data)
    cache[dataName] = data
end

这只是替换旧对象。我想要旧的和新的对象。你可以添加

cache[dataName][number] = data

只记录数字,但我将如何做到这一点,或者有更好的方法吗?

它只是一个对象数组。

【问题讨论】:

    标签: arrays list object lua


    【解决方案1】:

    您有一个未使用的表 list,并且您使用了一个未在您的 sn-p 中定义的表 cache。所以我就用我自己的名字来避免混淆。

    对于您的列表,您可以使用一个简单的 Lua 表。

    local stack = {}
    

    您现在可以通过

    将新项目添加到您的列表中
    stack[#stack+1] = newItem
    

    table.insert(stack, newItem)
    

    要将项目添加到最后,只需获取索引最高的元素。

    local lastItem = stack[#stack]
    -- if you want to remove it from the list additionally do
    stack[#stack] = nil
    

    或简称:

    local lastItem = table.remove(stack)
    

    如果您将 nil 分配给 1#stack 之间的任何索引,这将不起作用

    【讨论】:

      【解决方案2】:

      据我了解,您想在游戏进行时在不同时间为玩家添加击杀或死亡或更换武器。 在您的情况下,您应该使用 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
      

      【讨论】:

        猜你喜欢
        • 2017-09-02
        • 1970-01-01
        • 2020-06-09
        • 2013-12-13
        • 2013-10-12
        • 2013-03-20
        • 2011-02-15
        • 2015-08-08
        • 2012-06-26
        相关资源
        最近更新 更多