Derma - Detecting which item was clicked

Hello everyone.

So basically I have a question about Derma. So, I have a table that contains every item that will be within my DermaPanelList. However, I would like to know how to check which item within the list was clicked, here is my code:


local items = {}

items[ 1 ] = "models/weapons/w_pist_usp.mdl"

local SheetItemTwo = vgui.Create( "DPanelList", DermaPanel ) 

SheetItemTwo:SetParent( DermaPanel )
SheetItemTwo:EnableVerticalScrollbar( true ) 
SheetItemTwo:EnableHorizontal( true ) 
SheetItemTwo:SetPadding( 4 ) 
SheetItemTwo:SetPos( 10,30 )
SheetItemTwo:SetSize( 200, 160 )

for k,v in pairs( items ) do
local icon = vgui.Create( "SpawnIcon", SheetItemTwo )
icon:SetParent( DermaPanel )
icon:SizeToContents()
icon:SetModel( v )
icon:SetSize( 50, 50 )
SheetItemTwo:AddItem( icon )
icon.DoClick = function( icon ) 

surface.PlaySound( "ui/buttonclickrelease.wav" ) RunConsoleCommand("dm_buy_usp", v)

end

end	

I already setup a concommand to handle dm_buy_usp. But, how would I check which item was clicked? I am going to have M4 and other weapons on that table, any ideas? Also, this isn’t my code, I found this on the Wiki and thought it might be helpful to implement a shop system for my gamemode.

The DoClick method gets called when you click the button. The function body to the doclick will be scoped alongside the other variables in the file so just access them to get what you want.

But how does one “Access” them? Do you mean like this?



if icon == items[ 1 ] then
    RunConsoleCommand("dm_buy_usp", v)
end

If you mean that, then I’ve tried to do something like that. But I don’t exactly know programming terminology that well, so what do you propose I do? I mean, I know the DoClick() is when the button is clicked. However, I don’t exactly know what to do.

Put a “print(icon)” in there and see what it prints.

That is a good idea, Exho. That helped alot!