All 3 containers have ‘Shrink Center’ as horizontal and vertical size flags.
I dynamically insert other instanced scenes into the hbox_container and also remove them from time to time using script.
My intended behavior is that the panel_container as well as the hbox_container will resize themselves to the smallest possible rect.
It acts as expected when I insert new children, and both panel_container and hbox_container expands to accommodate them. However, when I remove them, they don’t shrink back and remain at the last expanded size. Only when I insert new children they change to appropriate size.
How do I force the containers to recalculate size upon removing children?
What seems to work for me is to hide the top level container, that needs to have it’s layout recalculated, and then add a deferred call to show it again.
In your case, your top level container of interest is panel_container.
var panel_container = $".".find_node("panel_container")
panel_container.visible = false
panel_container.call_deferred("set_visible", true)
This works for me, but it’s pretty hacky and I’d like to avoid the 1 frame delay. Any other good workaround?
Probably not entirely related, but I had a similar problem trying to retrieve the position property of a Container node after dynamically manipulating instanced scenes inside of it, with the values coming outdated.
The Container calculation seems to happen at the end of the frame. To fix this in Godot 4, you may use await get_tree().process_frame before requesting the updated values.
This leads to a one-frame flickering. I did solve it with a hack
func _on_visibility_changed():
var backup_global_position = global_position
# hide the first frame from the user
global_position = Vector2(10000, 10000)
await get_tree().process_frame
# get_rect().size is now known
global_position = backup_global_position
... do the repositioning