Tiramisu question:Getting player invertory.

Does anybody know how do I get the player’s invertory? I want to create a derma multichoice that will list all items the player has. I tried
[lua]local inventory = CAKE.GetCharField( ply, “inventory” )[/lua]
But it says that GetCharField is nill.
I tried
[lua]local function LoadInvertory()
if file.Exists( CAKE.Name … “/personaldata/inventory.txt” ) then
CAKE.SavedPositions = glon.decode( file.Read( CAKE.Name … “/personaldata/inventory.txt” ))
else
SavePositions()
end
end[/lua]
and it didn’t seem to work.
Please help.

The thing I want:
[lua]local MultiChoice2 = vgui.Create( “DMultiChoice”, InnerPanel )
local fuckingInvertory = I_dunno
for k, v in pairs( CAKE.InventorySlot[ fuckingInvertory.Class ) do
MultiChoice2:AddChoice( v )
end
MultiChoice2:SizeToContents()
MultiChoice2.OnSelect = function(panel,index,value) Ingr2 = value end
InnerPanel:AddItem( MultiChoice2 )[/lua]

[editline]30th July 2011[/editline]

UPDATE. That should be easy. I use

[lua]if file.Exists( CAKE.Name … “/personaldata/inventory.txt” ) then
local inventoryTab = file.Read( CAKE.Name … “/personaldata/inventory.txt” )
local inventory = string.ToTable(inventoryTab)[/lua]
How do I make it so for k, v in pairs will add a multichoice for each item listed? I will get you the inventory table in a second.

Please help. This one is easy, isn’t it?

Update:Damn! The inventory table comes out really bad. Aka


] inventory
1	=	
2	=	a
3	=	c
4	=	l
5	=	o
6	=	t
7	=	h
8	=	i
9	=	n
10	=	g
11	=	_
12	=	p
13	=	o
14	=	l
15	=	i
16	=	c	


etc

uh.
[lua]
if file.Exists( CAKE.Name … “/personaldata/inventory.txt” ) then
local inventoryTab = file.Read( CAKE.Name … “/personaldata/inventory.txt” )
local inventory = string.ToTable(inventoryTab)

local MultiChoice2 = vgui.Create( “DMultiChoice”, InnerPanel )
for k,v in pairs(inventory) do
MultiChoice2.AddChoice( v )
end
MultiChoice2:SizeToContents()
MultiChoice2.OnSelect = function(panel,index,value) Ingr2 = value end
InnerPanel:AddItem( MultiChoice2 )
[/lua]
?? seems simple enough from what you’ve posted

er. edit- just saw your edit

use string.explode, what character does it use as a seperater for items?

It won’t work due to how ugly inventory comes out after string.ToTable

Thanks for a response though!

look at my edit plz

^^

figure I might as well elaborate so this post seems less spammish

what character seperates it if there’s multiple entries? usually it’s something simple like a comma or a space
or is it a glon encoded table?

I have no idea though I’ve never used Tiramisu

The inventory saving and laoding thing (for position preferences in the example) looks this way
[lua]local function SavePositions()
if CAKE.InventorySlot then
for k, v in ipairs( CAKE.InventorySlot ) do
if v:GetItem() then
CAKE.SavedPositions[v:GetItem().Class] = k
end
end
end
file.Write( CAKE.Name … “/personaldata/inventory.txt”, glon.encode( CAKE.SavedPositions ) )
end

–Loads a player’s icon positioning preferences.
local function LoadPositions()
if file.Exists( CAKE.Name … “/personaldata/inventory.txt” ) then
CAKE.SavedPositions = glon.decode( file.Read( CAKE.Name … “/personaldata/inventory.txt” ))
else
SavePositions()
end
end[/lua]

Will try to use glon.decode

oh, then it’s a glon encoded table :stuck_out_tongue:

[lua]
if file.Exists( CAKE.Name … “/personaldata/inventory.txt” ) then
local inventoryTab = file.Read( CAKE.Name … “/personaldata/inventory.txt” )
local inventory = glon.decode(inventoryTab)
PrintTable(inventory)
[/lua]

that will return it in table form, but I have no idea what content is in each table, so if you need more help you could post the result from the bit of code above :wink:

But, I have to go! it’s harry potter in IMAX 3d time!

Thank you a lot. Now I can finish all that myself.


clothing_police	=	1
helmet_police	=	3
helmet_balaclava	=	2
resource_scrap_wood	=	14
weapon_357	=	1
clothing_formal	=	1
m_wood_axe	=	10
broken_rcs_glock	=	24
shoe	=	4
gun_parts	=	22
lightbeer	=	15
beans	=	18
darkbeer	=	9

Yeeea. Now I just need to make it add key to multichoice

[editline]30th July 2011[/editline]

Damn. Need more help now. So I made it to print me the keys from the inventory table, but the choices won’t get added.


clothing_police
helmet_police
helmet_balaclava
resource_scrap_wood
propitem
clothing_formal
m_wood_axe
broken_rcs_glock
shoe
gun_parts
lightbeer
beans
darkbeer
weapon_357
clothing_police
[@lua\vgui\dmultichoice.lua:128] bad key to string index (number expected, got string)

The lua code is now
[lua]
function RCLICK.Click(target,ply)

local Ingr1 = nil
local Ingr2 = nil
if file.Exists( CAKE.Name .. "/personaldata/inventory.txt" ) then
local inventoryTab = file.Read( CAKE.Name .. "/personaldata/inventory.txt"  )
local inventory = glon.decode(inventoryTab)



local Window = vgui.Create( "DFrameTransparent" )
Window:SetTitle( "Craft an item" )
Window:SetDraggable( false )
Window:ShowCloseButton( true )
Window:SetColor( CAKE.BaseColor )

local InnerPanel = vgui.Create( "DPanelList", Window )
InnerPanel:SetSpacing( 5 )
InnerPanel:SetPadding( 5 )
-- multichoice 1
local MultiChoice = vgui.Create( "DMultiChoice", InnerPanel )
for k, v in pairs( inventory ) do
	print( k )
	MultiChoice:AddChoice( k )

end
MultiChoice:SizeToContents()
MultiChoice.OnSelect = function(panel,index,key) Ingr1 = key end
InnerPanel:AddItem( MultiChoice )
-- multichoice 2
local MultiChoice2 = vgui.Create( "DMultiChoice", InnerPanel )
for k,v in pairs(inventory) do
	print( k )
    MultiChoice2.AddChoice( k )
end
MultiChoice2:SizeToContents()
MultiChoice2.OnSelect = function(panel,index, key) Ingr2 = key end
InnerPanel:AddItem( MultiChoice2 )

	local ButtonPanel = vgui.Create( "DPanel", Window )
ButtonPanel:SetTall( 30 )

local Button = vgui.Create( "DButton", ButtonPanel )
Button:SetText( "Accept" )
Button:SizeToContents()
Button:SetTall( 20 )
Button:SetWide( Button:GetWide() + 20 )
Button:SetPos( 5, 5 )
Button.DoClick = function() Window:Close() ply:ConCommand("rp_craft "..tostring(Ingr1).." "..tostring(Ingr2))
end

end
end[/lua]

[editline]30th July 2011[/editline]

My god, I see 14 people reading the page and nobody can tell me how to make multichoice show me keys and not values

[editline]30th July 2011[/editline]

I need just a multichoice with a string instead of number

I don’t know whether this will fix it, but you’re doing

[lua]MultiChoice2.AddChoice( k )[/lua]

instead of

[lua]MultiChoice2:AddChoice( k )[/lua]