node size before drawing it?

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

Premise: I’m new to Godot.

I’m working on a 2D UI project, specifically an interactive slideshow. My aim is to make it responsive to different screen resolutions. I’m using GDScript to instantiate a scene that contains a Control node with an HBoxContainer. I dynamically add a row of themed buttons to the HBoxContainer using GDScript. The buttons retrieve their text from a dialogue manager, and their width can vary.

I want to arrange the buttons in multiple rows (HBoxContainers) if their total width exceeds the screen width. However, when I try to retrieve the size.x of the HBoxContainer or the buttons, it returns 0. Interestingly, I have a debug node that uses draw_rect to visualize the HBoxContainer, and it correctly displays the HBoxContainer’s size.

So, why isn’t the size available immediately upon instantiation? Is there a way to retrieve the size before rendering a frame?


as ld2studio suggested I’m using HFlowContainer now, graphically I have the result I was looking for without adding further HBoxContainer.
For the size 0 problem I added an await get_tree().process_frame before using it (I tried await instance.ready but it seems that everything after it is not executed, hanging forever?).
It seems to work as I expect and without glitches, it’s not clear to me why, I feel like I’m missing some fundamental piece of what happens between instantiate() and the frame render.

:bust_in_silhouette: Reply From: ld2studio

You can use the FlowContainer node that arranges its child controls horizontally or vertically and wraps them around at the borders.

Thanks for the suggestion, I tried FlowContainer but I was in a bit of a rush, I probably discarded it too quickly, I’ll try again right now.

xand271 | 2023-06-18 17:35

FlowContainer does exactly what I’m looking for, but then I’ve the same problem, I try to get its height to make it scroll up with a tween, but the size.y is 0

xand271 | 2023-06-18 18:19