Jump to content

[HELP] Tables


ma2med

Recommended Posts

  
fruits = {} 
table.insert(fruits,{"banana",5}) 
table.insert(fruits,{"apple",1}) 
table.insert(fruits,{"cherry",4}) 
table.insert(fruits,{"banana",2}) 
table.insert(fruits,{"apple",8}) 
for i,v in ipairs(fruits) do 
outputChatBox(v[1].." = "..v[2]) 
end 
  

Hi, I'm searching a way for outputchatbox :

  
---[[ 
banana = 7 
apple = 9 
cherry = 4 
]]-- 
  

But it's output ..

  
---[[ 
banana = 5 
apple = 1 
cherry = 4 
banana = 2 
apple = 8 
]]-- 
  

But I don't find, thanks in advance :)

Link to comment
  • MTA Team

Not sure if you are going to like that style (because of metatable usage - advanced sh0t)

  
Backpack = { } 
  
function Backpack:create() 
    local new = { items = { } } 
    return setmetatable(new, {__index = Backpack}) 
end 
  
function Backpack:add(item, amount) 
    self.items[item] = math.max(0, (self.items[item] or 0) + amount) 
end 
  
function Backpack:remove(item, amount) 
    self.items[item] = math.max(0, (self.items[item] or 0) - amount) 
end 
  
function Backpack:drop(item) 
   self.items[item] = nil  
end 
  
function Backpack:getItems() 
    local copy = { } 
    for item, amount in pairs(self.items) do 
        copy[item] = amount 
    end 
    return copy 
end 
  
function Backpack:getItemCount(item) 
    local count = (self.items[item] or 0) 
    return count 
end 
  
function Backpack:hasItem(item) 
    local count = self:getItemCount(item) 
    return (count > 0) 
end 
  
-- Example 
example = Backpack:create() 
example:add("apple", 3) 
example:add("apple", 3) 
example:add("apple", 3) 
example:add("banana", 2) 
example:add("cherry", 4) 
  
-- Gather every item collected 
local items = example:getItems() 
  
-- Show items in chatbox 
for name, amount in pairs(items) do 
    outputChatBox("* ".. name .. ": " .. amount) 
end 
  

Result:

* banana: 2 
* apple: 9 
* cherry: 4  

Link to comment
Not sure if you are going to like that style (because of metatable usage - advanced sh0t)
  
Backpack = { } 
  
function Backpack:create() 
    local new = { items = { } } 
    return setmetatable(new, {__index = Backpack}) 
end 
  
function Backpack:add(item, amount) 
    self.items[item] = math.max(0, (self.items[item] or 0) + amount) 
end 
  
function Backpack:remove(item, amount) 
    self.items[item] = math.max(0, (self.items[item] or 0) - amount) 
end 
  
function Backpack:drop(item) 
   self.items[item] = nil  
end 
  
function Backpack:getItems() 
    local copy = { } 
    for item, amount in pairs(self.items) do 
        copy[item] = amount 
    end 
    return copy 
end 
  
function Backpack:getItemCount(item) 
    local count = (self.items[item] or 0) 
    return count 
end 
  
function Backpack:hasItem(item) 
    local count = self:getItemCount(item) 
    return (count > 0) 
end 
  
-- Example 
example = Backpack:create() 
example:add("apple", 3) 
example:add("apple", 3) 
example:add("apple", 3) 
example:add("banana", 2) 
example:add("cherry", 4) 
  
-- Gather every item collected 
local items = example:getItems() 
  
-- Show items in chatbox 
for name, amount in pairs(items) do 
    outputChatBox("* ".. name .. ": " .. amount) 
end 
  

Result:

* banana: 2 
* apple: 9 
* cherry: 4  

Perfect, thanks you.

Link to comment

I think this is more efficient.

local fruits = {} 
  
function addItem(item, count) 
    if item and count then 
        item = tostring(item) 
        fruits[item] = (fruits[item] or 0) + tonumber(count) 
    end 
end 
  
addItem("banana", 5) 
addItem("apple", 1) 
addItem("cherry", 4) 
addItem("banana", 2) 
addItem("apple",8 ) 
  
for i,v in pairs(fruits) do 
    outputChatBox(tostring(i).." = "..tostring(v)) 
end 

Output:

banana = 7 
apple = 9 
cherry = 4 

Link to comment
I think this is more efficient.
local fruits = {} 
  
function addItem(item, count) 
    if item and count then 
        item = tostring(item) 
        fruits[item] = (fruits[item] or 0) + tonumber(count) 
    end 
end 
  
addItem("banana", 5) 
addItem("apple", 1) 
addItem("cherry", 4) 
addItem("banana", 2) 
addItem("apple",8 ) 
  
for i,v in pairs(fruits) do 
    outputChatBox(tostring(i).." = "..tostring(v)) 
end 

Output:

banana = 7 
apple = 9 
cherry = 4 

I find but thanks anyway.

Link to comment
  • 1 month later...
Not sure if you are going to like that style (because of metatable usage - advanced sh0t)
  
Backpack = { } 
  
function Backpack:create() 
    local new = { items = { } } 
    return setmetatable(new, {__index = Backpack}) 
end 
  
function Backpack:add(item, amount) 
    self.items[item] = math.max(0, (self.items[item] or 0) + amount) 
end 
  
function Backpack:remove(item, amount) 
    self.items[item] = math.max(0, (self.items[item] or 0) - amount) 
end 
  
function Backpack:drop(item) 
   self.items[item] = nil  
end 
  
function Backpack:getItems() 
    local copy = { } 
    for item, amount in pairs(self.items) do 
        copy[item] = amount 
    end 
    return copy 
end 
  
function Backpack:getItemCount(item) 
    local count = (self.items[item] or 0) 
    return count 
end 
  
function Backpack:hasItem(item) 
    local count = self:getItemCount(item) 
    return (count > 0) 
end 
  
-- Example 
example = Backpack:create() 
example:add("apple", 3) 
example:add("apple", 3) 
example:add("apple", 3) 
example:add("banana", 2) 
example:add("cherry", 4) 
  
-- Gather every item collected 
local items = example:getItems() 
  
-- Show items in chatbox 
for name, amount in pairs(items) do 
    outputChatBox("* ".. name .. ": " .. amount) 
end 
  

Result:

* banana: 2 
* apple: 9 
* cherry: 4  

Please Add features such as:

That when creating the backpack could set the slot.

Function Backpack:getSlot().

Function destroy the backpack.

Function how busy slots.

Thanks in advance to you

p.s I`m noob, I beg you

Link to comment
  • MTA Team

What do you mean with slots? This backpack "class" allows unlimited items.

You could of course give the items more variation by adding weight, expiration time, poison level, alcoholic level, max stack size. Furthermore, your backpack could also have more variation by adding different types with other base-weight, slot count (you probably mean that), durability and more.

Link to comment
What do you mean with slots? This backpack "class" allows unlimited items.

You could of course give the items more variation by adding weight, expiration time, poison level, alcoholic level, max stack size. Furthermore, your backpack could also have more variation by adding different types with other base-weight, slot count (you probably mean that), durability and more.

I mean that's such functions

  
Backpack = {} 
  
function Backpack:create(slot) 
    local new = { 
        slots = slot, 
        items = {} 
    } 
    return setmetatable(new, {__index = Backpack}) 
end 
  
function Backpack:getSlots() 
    local slot = (self.slots) 
    return slot 
end 
  
Backpack:create(8) 
  

But then an error

Link to comment

I can not know a slot backpack

addCommandHandler('backpack', 
    function (player) 
        for name, amount in pairs(items) do 
            player:outputChat(name..': '..amount) 
        end 
        player:outputChat('Slot '..Backpack:getSlots()) 
    end 
) 

  
[2015-01-13 00:29:40] ERROR: [new_project]\project\inventory\backpack.lua:56: attempt to concatenate a nil value 
  

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...