Removing all currently drawn SpawnIcons?

I have an inventory menu. I’d like to be able to have it refresh whenever I removed an item from my inventory without having to close and reopen my menu.

Right now, when the inventory button is clicked, it draws the panels/etc and then calls the function DrawModels() to draw the SpawnIcons.

DrawModels() uses a for k, v in pairs(tab) loop to look through the player’s database and then draw an icon for each item. This works great.

The problem is that if I call the function whenever an item is used ( to redraw the spawn icons ) the old icons are still there. It does indeed redraw them, but the old ones are left over.

I can’t add “icon:Remove()” to the DoClick part for the icon ( which also removes that item from the inventory ) because then it will only remove that single icon, and I need them to all refresh.

Any help would be greatly appreciated!

Show your code.

function DrawModels()
        RunConsoleCommand( "u_getitems" )
	RunConsoleCommand( "u_getitemscmd" )
	local i = {}
	net.Receive( "u_getitems", function( len, ply )
		local f = net.ReadString()
		i = util.KeyValuesToTable( f )
	net.Receive( "u_getitemscmd", function( len, ply )
		local tab = net.ReadString()
		tab = util.KeyValuesToTable( tab )
		local items = tab["items"]
		local spacing = 0
		for k, v in pairs( items ) do
			if ( k != 1 ) then
				if ( v > 0 ) then
					local j = vgui.Create( "SpawnIcon", InvPanel )
					j:SetPos( 24, 50+spacing )
					j:SetModel( i[k].model )
					j.DoClick = function()
						RunConsoleCommand( "u_drop", k )
					spacing = spacing + 80

Where k is the item’s ID (like “water”) and v is the amount the player has.

Due to the fact that I made them using a for k, v in pairs(tab) loop, I can’t just remove each one because they are all assigned to j. Putting j:Remove() in the j.DoClick statement wont do much of anything but delete that one specific icon. I need to remove them all when j.DoClick triggers.

I know how to write Lua. It’s very easy compared to C++, which I went to college for.
I am simply confused on removing previously drawn SpawnIcons. I’m sure that I could just close the parent and reopen it when it refreshes but I’m not sure if that will work since InvPanel is parented to another piece of derma.

Put all panels into a table and loop through all the panels in table and remove them when needed.
TIP: Make the table variable outside of any functions, global if needed.

Also just as a tip you can shorten “if ( k != 1 ) then if ( v > 0 ) then” to just “if ( k != 1 ) && ( v > 0 ) then” Sorry OCD got to me.

You should reconsider your derma structure.

I’d suggest using one of the derma lists that are available like a DPanelList or a DIconLayout.

Preferably a DPanelList since you can remove item’s a lot easier IMO.

Alright. I am always very interested in optimizing my code and in finding new methods to do things. Thank you all for the help.