PenT1x
PenT1x

Reputation: 21

Lua array of objects

I want to have a list of objects where I can add a new object to at top of the list, so when I want the object I get the newest object. I am very new to lua. How I have understod to do it is.

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

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

This just replaces the old object. I want the old and new objects. You could add

cache[dataName][number] = data

and just keep track of the number, but how will I do that or is there a better way?

it is just an array of objects.

Upvotes: 2

Views: 4745

Answers (2)

nxFairlywell
nxFairlywell

Reputation: 1

As I understood you want to add kills or deaths or change the weapon for the players in different times while game is playing. In your case you should use OOP it's easy to add and remove and change values from and to the table. You must give an id for every player when he joined your game or your server, Whatever.

local data = {}

function data:AddNewPlayer()
  data[#data+1] = {
    weapon = 'ak',
    kills = 0,
    deaths = 0
  }
  return #data -- returns player id
end

Now we have a function that gives a data id for the player when he join our game.

So we have add some functions to add kills and get them:

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

Also deaths we should do the same way with them:

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

And for weapons, We add some functions to change the player weapon and get it as a string.

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

Finally, Let's try it:

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

Upvotes: 0

Piglet
Piglet

Reputation: 28950

You have a table list that you don't use and you use a table cache that is not defined in your snippet. So I'll just use my own names to avoid confusion.

For your list you can use a simple Lua table.

local stack = {}

You can now append a new item to your list by

stack[#stack+1] = newItem

or

table.insert(stack, newItem)

To get the item added to last, simply get the element with the hightest index.

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

or short:

local lastItem = table.remove(stack)

This won't work if you assign nil to any index between 1 and #stack

Upvotes: 1

Related Questions