Advertisement
NanoBob

inv

Aug 18th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.37 KB | None | 0 0
  1. local inventory={items={}}
  2. local elementInventories={}
  3.  
  4. function inventory:addItem(item,count)
  5.     if count<=0 then
  6.         outputDebugString("Error at iventory:addItem(), can't add negative ammount of tiems")
  7.         return false
  8.     end
  9.     if self.items[item]==nil then
  10.         self.items[item]=0
  11.     end
  12.     self.items[item]=self.items[item]+count
  13.     return true
  14. end
  15.  
  16. function inventory:takeItem(item,count)
  17.     local itemCount=self.items[item]
  18.     if itemCount==nil or itemCount<count then
  19.         outputDebugString("Error at iventory:takeItem(), Not enough "..item.." in inventory.")     
  20.         return false,itemCount
  21.     end
  22.     self.items[item]=self.items[item]-count
  23.     return true
  24. end
  25.  
  26. function inventory:hasItems(item,count)
  27.     local itemCount=self.items[item]
  28.     if itemCount==nil then
  29.         return 0
  30.     end
  31.     return itemCount
  32. end
  33.  
  34. function inventory:getAllItems()
  35.     return self.items
  36. end
  37.  
  38.  
  39. function setElementInventory(element)
  40.     local inv={}
  41.     for id,data in pairs(inventory) do
  42.         outputChatBox(id..","..type(data))
  43.         inv[id]=data
  44.     end
  45.     elementInventories[element]=inv
  46. end
  47.  
  48. function getElementInventory(element)
  49.     if elementInventories[element]==nil then
  50.         setElementInventory(element)
  51.     end
  52.     return elementInventories[element]
  53. end
  54.  
  55. local vehicle=createVehicle(463,0,0,3)
  56. local inv=getElementInventory(vehicle)
  57. inv:addItem("Weed",5)
  58. inv:takeItem("Weed",3)
  59. outputDebugString(toJSON(inv:getAllItems()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement