Container size is reset after add_child() method call

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

Hi,

I have a main scene called “Game.tscn” and a UI node called “StartMenu.tscn”

My project start with the scene “Game.tscn”. A script “Game.gd” is attached into it. It allow to pick a UI node and display it. My problem is that the UI node selected lost it rect size values after the “add child” method call.

Game.gd script:

StartMenu standalone:

StartMenu from the Game scene:

Scene tree after the add_child() method (all node under Game is the StartMenu)

Info:

  • Project size is 1024/576
  • StartMenu root container size is 1024/576
  • StartMenu root containuer is set Expand on vertical and horizontal axis

Can you post your actual scene tree? Maybe it gets reset because you are adding the UI as child of a UI container node, and such containers do override the size of their children.

Zylann | 2020-05-18 12:03

Thank you for taking your time for me. I edited my question as requested. You can now see my scene tree

Bornide | 2020-05-18 12:11

:bust_in_silhouette: Reply From: Zylann

I’m not sure what the logic is behind this behavior, but changing the root node Game to be a Node instead of a Node2D solved it for me. Perhaps it’s because Node2D has no size (these nodes are merely just points), so putting the UI as child of it will make it shrink to minimum size.

It should also work if you change the root into a Control that covers the whole screen.

Edit: after checking with a Sprite root node, it’s indeed what happens. If I use a Sprite (which has a size given by its texture), then the menu takes the space occupied by that, not the whole screen. If you want your UI to fill the whole screen through its anchors, the parent of that UI must either be Node or have the size of the screen.

Ok, I understand, thank you!

Bornide | 2020-05-18 13:45