Box Container Separation

Godot Version

4.2.1 Stable Mono

Question

I have a function that dynamically adds buttons to a Vbox container.
These buttons have HBOX containers within them that contain multiple
labels. I want to set the separation value for these hbox containers as
they’re added but evidently, You can’t edit the Separation parameter for
a Box Container in GDscript which seems like a massive oversight.

I looked through the documentation and found this “add_spacer” method
which takes a bool for some reason and does absolutely nothing regardless
of if it’s set to true or false. Which means the only possible way to do this
is to create a control variable for every item in the hbox and add them
manually which is incredibly inefficient not to mention a pain. The “add_spacer”
method has a total of 31 words dedicated to it across the entire Godot docs
website. So either the add_spacer method is broken or the documentation is missing critical information.

Does anyone know a better way to do this?

pretty sure separation will change the box container’s theme override.

$HBoxContainer.separation = 20

add_spacer says

Adds a Control node to the box as a spacer. If begin is true, it will insert the Control node in front of all other children

it returns the “spacer” node so you may alter it however you see fit.

1 Like

If you want to override a constant theme property of any Control you can use Control.add_theme_constant_override()

Example:

extends HBoxContainer


func _ready() -> void:
	add_theme_constant_override("separation", 200)

Result:

You also have all the others Control.add_theme_*_override() to change the different theme properties. Check the documentation.

3 Likes