How to center a button vertically inside an Hoxcontainer?

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

I currently have a button which is smaller than the bounds of the HBoxContainer it is inside of, so it is very awkwardly anchoring to the top left of the container. I don’t seem to be able to change the anchors like I used to in order to make it snap to center left, even through code, so is there a way to make them snap to the center left of the container? Any attempts to make it do so through code seem to be overridden by the parent, so I can’t do it “manually” with some math either. Changing the alignment property of the HBoxContainer also doesn’t work, I assume that changes alignment along the x axis rather than y?

:bust_in_silhouette: Reply From: njamster

Changing the alignment property of the HBoxContainer also doesn’t work, I assume that changes alignment along the x axis rather than y?

Correct. For VBoxContainer it’s the y-axis instead.
So one way to achieve what you want would be:

- VBoxContainer (Alignment: Center)
  - HBoxContainer
    - Button

Alternatively you can keep your tree as is and change the vertical size flags of the Button from “Fill” to “Shrink Center”.

Setting the size flag did exactly what I needed, and also saved the headache of changing node paths in scripts. Thanks.

fader | 2020-05-24 06:11