Quick VGUI question

Is there any disadvantage to creating all your GUI panels when you first spawn, and then hiding them (until they’re needed), or is it best to create/delete dynamically as they’re needed (as opposed to hiding them)?
Personally I feel like it’s just a question of RAM usage versus CPU usage, but I’m not sure as I’ve never tested it properly.

It’s best to create them when they’re first needed, then hide and show every time it’s opened or closed after that. Creating a panel is slower than making it visible.

Example pseudo-code :

[lua]
if ( !panel ) then
create()
else
panel:SetVisible( true );
end
[/lua]

I’m already aware of that method though, I should’ve been more clear. It’s more a question of whether dynamically creating and deleting panels is worth it?

It’s more like “creating panels as soon as player spawns and hiding them worth it”? Because it is easier to create the panels as they are needed and remove them when they are closed. ( What is happening by default ). Creating panels and hiding them only worth it if the panel has a lot of children ( parented to it panels ), so the client don’t lag for a bit every time the panel is created. Example of such panel is spawnmenu.

In that case, like Robot said; it’s only worth it if it’s worth it. In a sense you’ll always get a certain amount of lag doing a certain the same task, what’s best is to conceal it as much as possible, it’s a bad idea to create panels as soon as the client runs Lua, that can cause problems, so what I’d do if you had a big panel that needs creating is create a panel part by part using timers in the Initialize hook.

Better way is to create the panel once when player needs it and hide it.

I agree, that seems the most reasonable.