How to auto update rect size of control children in hboxcontainer after child visibilty change while in runtime?

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

I have an hboxcontainer with 3 control node children. They are each set to fill and expand. While in the editor, if I change the visibility of one to off, the other two will expand to fill in the space. Likewise when I turn it back on, the other two shrink. However, when I change to visibility in code during runtime, they don’t update their rect_size.

HBoxContainer

  • Slot1
    Slot2
    Slot3

The children start with visibility off, except the first child. I’ve tried turning that off too.
I am changing visibility using .visible = true.
I’m avoiding hard coding rect_size so that they will easily adjust with screen size.

I’ve tried using NOTIFICATION, though I might be using it wrong. I’ve tried sort_queue on the hboxcontainer after making them visible. I’ve tried to call draw() and update() to them as well, though I could have done that wrong too.

I’m hoping this is just an obvious oversight on my part and not a limitation of the engine.

:bust_in_silhouette: Reply From: voidshine

Yeah, after digging around in canvas_item.cpp and other bits of engine code, I found that visibility handling is quite subtle (as of 3.2.3). The intention is to optimize performance by not calculating for nodes that aren’t showing in the tree, but unfortunately this means that any other nodes or script logic that make use of hidden node data (rect, etc.) will be using dirty/incorrect values.

I haven’t found a clean way to solve this yet, and the best advice I can give so far is to make sure things are visible for long enough to have their values updated. You can start visible and CallDeferred("hide") (C#). This gives just enough time to lay things out. Or you could start hidden, Show(), and then wait a frame before making use of data updated on visible nodes. You may have to manually Update() nodes once tree visibility changes.

I hope to find a better answer at some point, but this is what I’ve got so far.