• Help With Inventory
    11 replies, posted
Hey there, I'm still having trouble with an inventory system and could use some help. I'm wondering how I can store and load items even after shutdown, would I need to recreate the entities by ents.Create and how would I add them to the player inventory so that they're not technically a physical object in the world but in the player?
Take a look at glon. Also you don't create the entity when it's not in the world you just keep a reference of the type in a table on the player, such as, ply.Inventory["my_sent_name"] = amount . then make functions to add and remove the variable from the table and spawn the entity. Also make your entity's use function give the entity to the player then remove its self. For loading and saving use glon in a hook for initialspawn and disconnect.
Save the data to a sql database or a txt file.
fileExitst,fileWrite,fileLoad
Any idea how I can get the things to show up clientside? I am using this code; [code] function OpenAgentPanel() local inventoryWindow=vgui.Create("DFrame") inventoryWindow:SetPos(0,0) inventoryWindow:SetSize(ScrW(),ScrH()) inventoryWindow:SetTitle("Myself...") inventoryWindow:SetVisible(true) inventoryWindow:SetDraggable(false) inventoryWindow:ShowCloseButton(true) inventoryWindow:MakePopup() local selfPlayer=LocalPlayer() if(selfPlayer.inventory!=nil) then local lastX=0 local lastY=10 local wid=ScrW() local hgt=ScrH() local widLeft=wid-lastX selfPlayer:ChatPrint("You have an item!") for k,v in pairs(selfPlayer.inventory) do if(widLeft<50) then lastY=lastY+50 end local newSlot=vgui.Create("SpawnIcon",inventoryWindow) newSlot:SetModel(v:GetModel()) newSlot:SetPos(0,0) newSlot:SetSize(lastX,lastY) newSlot.DoClick=function(btn) local slotOptions=DermaMenu() if(v:GetClass()=="spies_doc") then local destObj=v:GetVar("DestObj") local destName=v:GetVar("DestName") slotOptions:AddOption("Check Destination",function() selfPlayer:ChatPrint("Destination point is: "..destName) end) end end end end end concommand.Add("OpenAgentPanel",OpenAgentPanel) [/code]
Use a user message to send the table client-side when loading and changing values. Also i'm not entirely sure why everyones rating runa dumb and wrong you will need to look at those if your wanting to use glon.
What is Glon? And I think I am using usermessages correctly, but here's the code anyways in case I've done something wrong; [code] function inventoryHook(mess) local selfPlayer=LocalPlayer() selfPlayer.inventory={} local invInd=mess:ReadLong() local indEnt=mess:ReadEntity() end usermessage.Hook("InventorySend",inventoryHook) [/code] [code] function GM:ShowSpare1(ply) local curInd=0 if(ply.inventory!=nil) then for k,v in pairs(ply.inventory) do umsg.Start("InventorySend",ply) umsg.Long(curInd) umsg.Entity(v) umsg.End() curInd=curInd+1 end end ply:ConCommand("OpenAgentPanel") end [/code]
Glon is an encoding and decoding set of functions to save tables and other variables to a text file. [url]http://wiki.garrysmod.com/?title=Glon[/url] .
Okay but do you know why the things that I've picked up aren't being added to my inventory? Did I do the usermessages wrong? [editline]18th March 2011[/editline] Could it be because SetSize isn't the same for SpawnIcon? If so I still need to use SetIconSize() to setup the initial size?
Add prints on client to make sure you are getting the correct data on client [editline]18th March 2011[/editline] And if it is then post your code that draws the inventory
It is getting the correct information in the usermessage however the issue is that it doesn't appear to properly set selfPlayer.inventory. Does that help at all? [editline]18th March 2011[/editline] [QUOTE=TheNerdPest14;28669557]It is getting the correct information in the usermessage however the issue is that it doesn't appear to properly set selfPlayer.inventory. Does that help at all?[/QUOTE] Here is the latest code if it helps. [code] function OpenAgentPanel() local inventoryWindow=vgui.Create("DFrame") inventoryWindow:SetPos(50,0) inventoryWindow:SetSize(ScrW()-50,ScrH()) inventoryWindow:SetTitle("Myself...") inventoryWindow:SetVisible(true) inventoryWindow:SetDraggable(false) inventoryWindow:ShowCloseButton(true) inventoryWindow:MakePopup() local selfPlayer=LocalPlayer() if(selfPlayer.inventory!=nil) then if(selfPlayer.inventory[0]==nil) then selfPlayer:ChatPrint("Odd, I have no items!") else local newIcon=vgui.Create("SpawnIcon",inventoryWindow) newIcon:SetPos(0,20) newIcon:SetModel(selfPlayer.inventory[0]:GetModel()) end end end concommand.Add("OpenAgentPanel",OpenAgentPanel) function inventoryHook(mess) local selfPlayer=LocalPlayer() local invInd=mess:ReadLong() local indEnt=mess:ReadEntity() selfPlayer:ChatPrint("Index is: "..invInd) if(selfPlayer.inventory==nil) then selfPlayer.inventory={} selfPlayer.inventory[invInd]=indEnt else table.insert(selfPlayer.inventory,invInd,indEnt) end end usermessage.Hook("InventorySend",inventoryHook) [/code]
Give me the code where your putting it into the table because on the current code you are never inserting it into the table on client :ninja: [editline]18th March 2011[/editline] This is an example from the inventory I am currently working on for my gamemode I hope it gives you a better idea of how to do this [lua] Inventory = { } usermessage.Hook( "SendItem", function (data) Item = {} Item.Class = data:ReadString() Item.Name = data:ReadString() Item.Dec = data:ReadString() Inventory[Item.Class] = Item end) [/lua]
Sorry, you need to Log In to post a reply to this thread.