+5 votes


is there a window resize event or signal from the engine?
For example: You want to draw a rectangle as a console background:

draw_rectangle(0, 0, window_width, some_height, ...)

However, you don't need this to be updated every frame, but when a window resize happens. Something like:

func _on_resize():  # this does not exist
>> update() 

Or an alternative approach: Can you somehow force a GUI node to stretch with the window automatically?


in Engine by (124 points)

2 Answers

+16 votes
Best answer

As already mentioned by @bobokox, you can do this easily do this with stretch mode in the project settings. It should only take a minute of fiddling with the check boxes and resizing your windows to see what they do.

The details on the mode choices are here: http://docs.godotengine.org/en/stable/tutorials/engine/multiple_resolutions.html#stretch-settings

The only non-obvious things are 2D/Viewport options. 2D is what you would expect, and viewport takes the resulting image of the viewport and scales that. So a 3D scene could become pixelated or squashed.

The other options are between distorting the image to fill the stretch, or preserve the ratio using black bars. The first let's it decide which black bar to us, the other two are a choice between forcing a vertical or horizontal black bar.

If you still would like to some kind of code version, you can get the root viewport, and use it's resize signal to alert you when the window is being resized.

extends Node2D

func _ready():
    get_tree().get_root().connect("size_changed", self, "myfunc")

func myfunc():
    print("Resizing: ", get_viewport_rect().size)
by (5,206 points)
selected by

The code is perfect! Thanks!

+1 vote

Control nodes have a resized() signal which emits when the Control's rect changes size.

Can you somehow force a GUI node to stretch with the window automatically? Yes, using Anchors and/or Size Flags.

by (1,330 points)

Can you help me out with an example?

Let's take the panel node. I want it to be as wide as the window. When the window resizes, so does the panel. The height does not matter.

How do I setup anchors and size flags?

In the project settings -> display change stretch_mode to viewport and stretch_aspect to keep_width.
it should do exactly what you want.

Yea, but in my case I don't need the aspect ratio nor the resolution to stay the same. So it is intended that you can actually see more of the game when you resize the window.

I still don't know how to use these anchors and size flags. Please help!

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.