ma2med Posted November 16, 2014 Posted November 16, 2014 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
Feche1320 Posted November 16, 2014 Posted November 16, 2014 It outputs what you inserted on the table..
ma2med Posted November 16, 2014 Author Posted November 16, 2014 It outputs what you inserted on the table.. No, I want organize it. (this is an example with the fruits)
MTA Team botder Posted November 16, 2014 MTA Team Posted November 16, 2014 Make a new function (table.find) to search trough your table if there is an index with the same message.
MTA Team botder Posted November 16, 2014 MTA Team Posted November 16, 2014 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.
ma2med Posted November 16, 2014 Author Posted November 16, 2014 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
Sasu Posted November 16, 2014 Posted November 16, 2014 Why don't you use the name of the fruit as index? It would be easier.
ma2med Posted November 17, 2014 Author Posted November 17, 2014 Show your full script Take the example it's same.
#DRAGON!FIRE Posted November 17, 2014 Posted November 17, 2014 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 ?
Feche1320 Posted November 17, 2014 Posted November 17, 2014 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 botder Posted November 17, 2014 MTA Team Posted November 17, 2014 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
ma2med Posted November 18, 2014 Author Posted November 18, 2014 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.
Sasu Posted November 18, 2014 Posted November 18, 2014 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
MTA Team botder Posted November 18, 2014 MTA Team Posted November 18, 2014 Looks almost like my code except you are limited to 1 "backpack/table" with data.
ma2med Posted November 18, 2014 Author Posted November 18, 2014 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.
iAxel Posted January 12, 2015 Posted January 12, 2015 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 botder Posted January 12, 2015 MTA Team Posted January 12, 2015 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.
iAxel Posted January 12, 2015 Posted January 12, 2015 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 botder Posted January 12, 2015 MTA Team Posted January 12, 2015 How do you use that Backpack "class" in your code? Your given example code gives me no errors.
iAxel Posted January 13, 2015 Posted January 13, 2015 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
MTA Team botder Posted January 13, 2015 MTA Team Posted January 13, 2015 You have to create a backpack for each player. There is no magic done behind my "class" which will automatically give each player an individual backpack. You have to do that part on your own. I had the time and extended that class: items.lua http://pastebin.com/EukyHHfH backpack.lua http://pastebin.com/07HPp8kr player.lua http://pastebin.com/0UzmRhys
iAxel Posted January 14, 2015 Posted January 14, 2015 You have to create a backpack for each player. There is no magic done behind my "class" which will automatically give each player an individual backpack. You have to do that part on your own.I had the time and extended that class: items.lua http://pastebin.com/EukyHHfH backpack.lua http://pastebin.com/07HPp8kr player.lua http://pastebin.com/0UzmRhys Thank you very much!
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now