• How would I go about finishing this? (An inventory)
    9 replies, posted
Hey all, I've created the base of a inventory system but I'm trying to add it into a menu. My friend told me I needed to network the two I know this involves usermessages and I've set up hooks for my message but now I'm clueless as to how I can add the weapons to something that panels can read. Here's my code so far: [lua] function meta:GiveItem(item,amt) amt = amt or 1 amt = (self.Inventory[item] or 0) + amt self.Inventory[item] = amt umsg.Start("sendItem",self) umsg.String(item) umsg.Short(amt) umsg.End() self:SetPData("Inventory",glon.encode(self.Inventory)) end function meta:HasItem(item,amt) return self.Inventory[item] end function meta:TakeItem(item,amt) amt = amt or 1 amt = math.Max(0,(self.Inventory[item] or 0) - amt) if amt < 1 then amt = nil end self.Inventory[item] = amt umsg.Start("sendItem",self) umsg.String(item) umsg.Short(amt or 0) umsg.End() self:SetPData("Inventory",glon.encode(self.Inventory)) end function meta:Use(item,amt) if self:HasItem(item,amt) then if self.Inventory[item] >= amt then self:TakeItem(item,amt) self:Give(item) end end end [/lua] Any examples or suggestions on what I should do would be appreciated.
[lua]local Items = {} usermessage.Hook("sendItem", function(um) local _Item = um:ReadString() local _Amt = um:ReadShort() or 0 if !_Item or !_Amt then return end table.insert(Items,{_Item._Amt}) end) [/lua] Return the players data like this: [lua] function ItemTable() return Items end [/lua] You can use in derma like this: [lua]for _,v in pairs(ItemTable()) do local _Item = v[1]; local_Amt = v[2] --Derma Shizzle end [/lua] Not so hard, thats just the basic adding system, to update you could do something like: [lua] usermessage.Hook("updateItem", function(um) local _Item = um:ReadString() for k,v in pairs(ItemTable()) do if v[1] == _Item then --Has the item so do stuff here end end) [/lua]
Thanks mate! I've never really done anything like this before so I never had an idea of where to head.
[QUOTE=Science;30474427] [lua] function ItemTable() return Items end [/lua] [/QUOTE] No
Yes it's a silly function. But I'm just giving him some example snipits that could be useful.
Please post the whole code when done, I need such thing but I have much more work.
LOL? :frog:
Why are you making a new table for each item? You could use the item name as the key and the amount as the value. [lua]Items[name] = amount[/lua]
You could. But you dont have to.
Your making this so there is only one inventory. Store them with ply.Variables, or some other method to store it on the player. I'm talking to science, btw.
Sorry, you need to Log In to post a reply to this thread.