VGUI: Delete all children?

Hello,

I’m working on a scoreboard, and I’m gonna have a DScrollPanel for the player list, and loop through each player when the player opens the scoreboard and make a DButton, and when they open the scoreboard, the DScrollPanel is going to be removed then re-created (trying to shake off the DButtons from the loop). Is there a way to remove all the children on a DScrollPanel, or is there an easier way to do this?

Thanks.

Not sure if there’s a build in function for this, but you can certainly keep a table of each child object you want to keep track of, and then iterate through it and remove them all when you please.

[LUA]

for k,v in ipairs( children ) do

v:Remove()

end

[/LUA]

I made a console command to do just this: https://dl.dropboxusercontent.com/u/26074909/tutoring/_utilities/concommand_clearvgui.lua

It’ll show you how to loop through the children. Also, the counter starts at 0 instead of 1 and therefore ends at n - 1 instead of n.



	for k,v in pairs(player.GetHumans()) do
		local PButton = vgui.Create("DButton", mw3scoreboard.Frame)	
	end


The problem here is the buttons are all local variables :confused:

[lua]
local oldCreate = vgui.Create

function vgui.Create( element, parent )
if not parent then
return oldCreate( element )
else
local child = oldCreate( element, parent )
if not parent.children or not type( parent.children ) == ‘table’ then
parent.children = {}
end

  table.insert( parent.children, child )
  return child

end
end
[/lua]

I think this is what I was looking for! let me test it and see what happens.

It doesn’t matter how they’re added, or if all of the creates are done using a local variable.

Every vgui element parents to the game window, which is accessible, or the hudpanel, which is accessible but elements aren’t returned. So, if you need to remove all vgui, mine shows how to do that by removing all children from the game window.

Alternatively, if you know the specific parent panel, you can target that and remove it that way.

An alternative way of adding panels in a loop, instead of using local within the loop, create a table outside the loop, then just add table[ k ] = vgui.Create within the loop, all elements are easily accessible then.

Alright, so I try to remove all the children, and then I get this error:




[ERROR] lua/vgui/dscrollpanel.lua:96: Tried to use invalid object (type Panel) (Object was NULL or not of the right type)
  1. SizeToChildren - [C]:-1
   2. Rebuild - lua/vgui/dscrollpanel.lua:96
    3. unknown - lua/vgui/dscrollpanel.lua:151


How i’m doing it:



function UpdateScoreboard()
	for k,v in pairs(mw3scoreboard.DScroll.children) do
		v:Remove()
	end

	for k,v in pairs(player.GetAll()) do

		local PButton = vgui.Create("DButton", mw3scoreboard.DScroll)
		PButton:SetSize(mw3scoreboard.DScroll:GetWide(), ScreenScale(14))
		PButton:SetPos(0, mw3scoreboard.DScroll:GetTall()/12 + (ScreenScale(16) * k));
		PButton:SetText("");
	end

	for k,v in pairs(player.GetBots()) do
		
	end
end

mw3scoreboard.Frame:SetVisible(false);

hook.Add("ScoreboardShow", "ShowScoreboard", function()
	UpdateScoreboard();
	mw3scoreboard.Frame:SetVisible(true);
	return true
end)