I have a very simple scene:
> top_node: Node
>> other_nodes
One of the other nodes should add a very simple toast with a test at the bottom of the screen, centered horizontally:

I made a control scene to do this:
> toast: MarginContainer
>> PanelContainer
>>> CenterContainer
>>>> Label
I am struggling to correctly set the anchors and margins. As far as I understand, the anchors mean "what proportion of the parent node should my side's margin be relative to?". I set top & bottom to 1 and left/right to 0.5.
However, as the left border takes precedence over the right one, if I set left/right margins to 0, the toast won't be centered, rather its left edge will be at the center. I could set left margin to, say -20
and right one to 20
, but I'd like these numbers to depend on the actual size, not to be hardcoded (otherwise it'll create slack if label is smaller than 40 and won't be perfectly centered if it's bigger than 40).
- Do I have to recalculate the margins somewhere based on the current size every time I change the text in the label?
After testing the toast scene separately, I tried adding it to the main scene by: get_tree().get_current_scene().add_child()
. This put the toast in the top-left corner, as if the scene itself had size 0,0 (and thus anchors didn't have the effect). When I used get_tree().get_root()
instead, all worked correctly. I would prefer if the dynamically created nodes were the children of the scene and not the root viewport though.
2. Is there a way to make the top_node
fill its viewport? Or maybe it's actually better for the control nodes like this to be children of the viewport?
3. Given that margins set the position of the node, what is the point in having a separate position field for the Control nodes? (When) should I use it at all?
Sorry for the long post, these margins/anchors are hard to wrap one's head around