So I am making a inventory for my gamemode and I ran into this problem of my inventory just completely disappearing.
[lua]
AddCSLuaFile( "clientside.lua" )
function PlayerInitialSpawn( ply )
ply.inv = {}
ply.inv.weight = 0
ply.inv.items = {}
ply.inv.equipment = {}
AddItem("Stick", 10, ply)
AddItem("Skull", 5, ply)
PrintTable(ply.inv.items) // works :)
end
hook.Add("PlayerInitialSpawn","initSpawn",PlayerInitialSpawn);
function AddItem( name, amount, ply )
local inItems = false
if(Items[name] != nil) then
for k,v in pairs(ply.inv.items) do
if(v[1] == name) then
inItems = true
v[2] = amount + v[2]
umsg.Start( "ChangeItem", ply );
umsg.String( name );
umsg.Short( v[2] );
umsg.End();
break
end
end
if(!inItems) then
table.insert(ply.inv.items, {name, amount})
umsg.Start( "AddItem", ply );
umsg.String( name );
umsg.Short( amount );
umsg.End();
end
end
end
function GM:ShowTeam( ply )
ply:ConCommand( "inventory" )
end
function UseItem( ply, command, arguments )
PrintTable(ply.inv.items) // EMPTY
for k,v in pairs(ply.inv.items) do
if(v[1] == arguments[1]) then
AddItem(arguments[1],-1,ply)
end
end
end
concommand.Add( "UseItem", UseItem )
[/lua]
So I added 2 comments (line 12, 45). As you can see after adding the items to the serverside inventory ( and adding them clientside) I print the serverside inventory for the player and it works as expected. Then when I run the console command UseItem everything is suddenly gone. Does anybody know why this is happening ?
There must be conflicting code; is there any other part of your code that interacts with ply.inv?
Nope
Just to be sure here is my clientside code :
[lua]
items = {}
function AddItem( data )
item = {}
item.Name = data:ReadString()
item.amount = data:ReadShort()
table.insert(items,item)
end
usermessage.Hook( "AddItem", AddItem )
function ChangeItem( data )
local name = data:ReadString()
for k,v in pairs(items) do
if(v.Name == name) then
v.amount = data:ReadShort()
break
end
end
end
usermessage.Hook( "ChangeItem", ChangeItem )
function ShowInventory()
ply = LocalPlayer()
local inventoryPanel = vgui.Create( "DFrame" )
inventoryPanel:SetPos( ScrW() / 2 - 250, ScrH() / 2 - 250) // where 250 = half width / heigth
inventoryPanel:SetSize( 500, 500 )
inventoryPanel:SetTitle( "Inventory" )
inventoryPanel:SetVisible( true )
inventoryPanel:SetDraggable( false )
inventoryPanel:ShowCloseButton( true )
inventoryPanel:MakePopup()
local iconList = vgui.Create( "DPanelList" )
iconList:SetParent(inventoryPanel)
iconList:EnableVerticalScrollbar( true )
iconList:EnableHorizontal( false )
iconList:SetSpacing( 5 )
iconList:SetPadding( 5 )
iconList:SetPos( 10, 30 )
iconList:SetSize( 480, 460 )
for k,v in pairs( items ) do
local itemPanel = vgui.Create( "DPanel" )
itemPanel:SetSize(440,130)
local icon = vgui.Create("SpawnIcon", itemPanel)
icon:SetModel(Items[v.Name].Model)
icon:SetPos(5,5)
icon:SetMouseInputEnabled(false)
local name = vgui.Create("DLabel", itemPanel)
name:SetText("Name : " .. v.Name)
name:SizeToContents()
name:SetPos(75, 8)
local amount = vgui.Create("DLabel", itemPanel)
amount:SetText("Amount : " .. v.amount)
amount:SizeToContents()
amount:SetPos(75, 23)
local description = vgui.Create("DLabel", itemPanel)
description:SetText(Items[v.Name].Description)
description:SizeToContents()
description:SetPos(8, 80)
local use = vgui.Create("DButton", itemPanel)
use:SetSize(45, 20)
use:SetPos(420, 5)
use:SetText("Use")
local equip = vgui.Create("DButton", itemPanel)
equip:SetSize(45, 20)
equip:SetPos(420, 30)
equip:SetText("Equip")
if(Items[v.Name].Type & INV_USE != INV_USE) then
use:SetDisabled(true)
else
use.DoClick = function()
RunConsoleCommand("UseItem", v.Name)
end
end
if(Items[v.Name].Type & INV_EQUIP != INV_EQUIP) then
equip:SetDisabled(true)
else
equip.DoClick = function()
RunConsoleCommand("UseItem", v.Name)
end
end
iconList:AddItem(itemPanel)
end
end
concommand.Add( "inventory", ShowInventory )
[/lua]
Tested your code. Works for me; prints the table just fine.
[img]http://luxsource.info/images/inv.png[/img]
Then what am I doing wrong ? this is the code I am using :S
Oh wait a sec I'll see if it works that way (I am calling it from a derma menu)
Results :
[code]
Scanning for downloaded fonts..
1:
1 = Stick
2 = 10
2:
1 = Skull
2 = 5
RunConsoleCommand blocked - sent before player spawned (ZLib_Installed)
================================
=== Wire revision: 0 ===
=== Local Wire revision:0 ===
================================
Redownloading all lightmaps
] UseItem
[/code]
Sorry, but unless you post the rest of your code, I cannot help you. Something is conflicting.
Here is my shared code :
[lua]
INV_USE = 1
INV_CRAFT = 2
INV_EQUIP = 4
AllItems = {}
function Initialize()
for k, v in pairs( file.FindInLua("NightOfZombies/content/data/items/*.lua") ) do
include("NightOfZombies/content/data/items/" .. v)
end
end
hook.Add("Initialize","initGamemode",Initialize)
function RegisterItem( item )
AllItems[item.Name] = item
end
[/lua]
[editline]21st February 2011[/editline]
Here is my full gamemode code : [url]http://encodable.com/cgi-bin/filechucker.cgi?action=landing&path=/&file=gamemode.rar[/url]
You did the following:
[lua]
function PlayerInitialSpawn( ply )
ply.inv = {}
ply.inv.weight = 0
ply.inv.items = {}
ply.inv.equipment = {}
AddItem("Stick", 10, ply)
AddItem("Skull", 5, ply)
PrintTable(ply.inv.items) // EMPTY wtf ?
end
hook.Add("PlayerInitialSpawn","initSpawn",PlayerInitialSpawn);
function GM:PlayerInitialSpawn( ply )
ply:SetTeam(1)
//Diffrent day period stuff
ply:SetNWInt("period",currentPeriod)
ply:SetNWInt("time",math.Round(timeNextCycle))
//Inventory stuff
ply.inv = {}
ply.inv.weight = 0;
ply.inv.items = {}
ply.inv.equipment = {}
end
[/lua]
In your code, you would set-up the players inventory, add items and display it. Which works fine. However, since you also set up the inventory in the base function (GM:PlayerInitialSpawn), the player's inventory would be emptied again. [b]Hooks are called before the base function is.[/b]. Remove the ply.inv = {} stuff from GM:PlayerInitialSpawn and it will work.
I didn't see I still had that part inside of the old code :facepalm:
Thanks bro
Sorry, you need to Log In to post a reply to this thread.