Jump to content

[HELP] Tables


ma2med

Recommended Posts

Posted
  
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 :)

  • MTA Team
Posted

Make a new function (table.find) to search trough your table if there is an index with the same message.

  • MTA Team
Posted
you can sort table with this
table.sort 

He wants to prevent "spam" - in his example there is 2x "apple" and he wants to output it only once.

Posted
you can sort table with this
table.sort 

He wants to prevent "spam" - in his example there is 2x "apple" and he wants to output it only once.

Yes

Posted

Why do you need to insert two values ?

local table = {  
  [ "Apple" ] = 25, 
}   
  
function _setValue (  ) 
    table [ "Apple" ] = math.random ( 50 ) 
end 

if you need to tow values post your full code or tell me what you want to do ?

Posted

Show your full script

Take the example it's same.

Then you are doing it wrong, sir. Take the fruit name as the index, and then add +whatevervalue to that index.. if you don't show your full code then we can't help you much more..

  • MTA Team
Posted

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  

Posted
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.

Posted

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 

Posted
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.

  • 1 month later...
Posted
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

  • MTA Team
Posted

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.

Posted
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

  • MTA Team
Posted

How do you use that Backpack "class" in your code?

Your given example code gives me no errors.

Posted

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 
  

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...