0 votes

I have an HBoxContainer that I want to fill the width of the screen, and stretch / shrink with the screen when resized.

Here is how I currently have the tree set up:

Control > Panel > HBoxContainer

The HBoxContainer has size flags set to expand horizontally to fill the panel, and both the panel AND the Control are set to "full rect," and they visually fill the viewport in the editor. However, when I run the scene, my debugging script tells me that the width of both the control and panel nodes are at 0 (which doesn't make sense because I can visually see the panel fills the screen still).

What am I doing wrong?

Godot version 3.4.2
in Engine by (75 points)

1 Answer

0 votes

I attempted to replicate your set-up. Control as root of scene, Panel as child of Control, and HBoxContainer as child of Panel. Both Control and Panel are sect to "full rect" and "HBoxContainer" is set to "expand horizontal."

Visually, the panel occupies the entire viewport (as expected) and resizes when I resize the window. As far as I can tell, you observe that behavior too.

However, when I sample the rect_size of Control and Panel frame by frame, it gives me expected results (starts at the window size, then changes as I resize the window). I'm not sure what the issue is in your case--what property are you checking to determine the width of the window?

EDIT: I should note that I'm running on Godot 3.4.4, so that may make a difference. I don't know.

by (74 points)

The issue seems to have fixed itself...

I had a script set up on the HBoxContainer that included this function:

func _input(event):
    if event.is_action_pressed("debug"):
        print(get_parent().get_size().x)

Previously, the script would only return a 0, even though that didn't make sense visually. Accordingly, my other scripts that were affected by the container size wouldn't work properly.

However, it's working as expected now, I'm not sure what I changed or if this was just a glitch...

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.