Is there any way to make a container and a panel expand when adding more children?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By suribe

In a game I’m developing, I have an action bar which contains a fixed number of buttons and a section where I want to add (or remove) buttons depending on whether an action can be executed at the moment. The bar should expand or contract depending on the number of buttons it contains. So at one moment it might have only two buttons, another moment five, but in all cases, the container should expand. I should mention that this action bar has a background (it’s a panel at the moment) which should also contract or expand.

By default it looks like this:
enter image description here

If I add buttons, right now it looks like this:
enter image description here
As I’m using a VBoxContainer, it just expands to the right, but I want the background Panel to expand too.

This is how I would like it to look (changed size by hand now)
enter image description here

I took a look at the container section of the documentation, and played a bit in the editor, but all I found is how to make the contained elements change size, but not how the container itself could react to its children by changing size. Is this even possible with the standard containers and controls? I want to know that I’m not missing on anything, before trying to implement my own solution.

:bust_in_silhouette: Reply From: Ha-kuro

Unfortunately Godot’s container nodes lack an easy way to resize them dynamically, only ways to resize and resort their children. I’ve encountered this before but couldn’t find many native solutions.

The only way I’ve managed to change the content within a container is by changing the child’s visibility. I’d suggest using the .hide() and .show() methods rather than visible = true for readability.

e.g.

func expand():
    $Panel.get_child('invisible_button').show()

Thanks! At least I will now stop looking for such a solution, and in case I really need it, will try to implement something myself.

suribe | 2022-12-03 09:06