Hello all. I've a question regarding the capture event of a window been resized

My code is as follows. Works fine if i don't change the resolution (works with full screen and windowed screen). But I would like to call the _center_hud when there's a change in the window dimension.

Thanks

extends CenterContainer

var screen_x =0
var screen_y =0

# Called when the node enters the scene tree for the first time.
func _ready():
	#screen_metrics()
	_center_hud()
	
		
func _center_hud():
	screen_x=OS.get_real_window_size().x
	screen_y=OS.get_real_window_size().y
	self.rect_position.x=screen_x/2
	self.rect_position.y=screen_y/2

func screen_metrics():
	print("[Screen Metrics]")
	print("Display size: ", OS.get_screen_size())
	print("Decorated Window size: ", OS.get_real_window_size())
	print("Window size: ", OS.get_window_size())
	print("Project Settings: Width=", ProjectSettings.get_setting("display/window/size/width"), " Height=", ProjectSettings.get_setting("display/window/size/height")) 
	print(OS.get_window_size().x)
	print(OS.get_window_size().y)


func _on_Control_size_flags_changed():
	_center_hud()

I've found that the size_changed signal from the root viewport works pretty well for detecting if the window has been resized or not. You can connect the signal with something like this (untested):

func _ready():
	get_tree().root.connect("size_changed", self, "_on_viewport_size_changed")
func _on_viewport_size_changed():
	# Do whatever you need to do when the window changes!
	print ("Viewport size changed")

The only thing I'm not totally sure about is whether the signal will be sent when the game is set to full-screen and/or minimized. If you need to react to when the application window resizes, I think the size_changed signal should work.

a year later

This works like a charm! Thank you!

Note for future readers: You shouldn't center or scale your HUD manually when the window size changes. Instead, use the 2d stretch mode, expand stretch aspect and configure UI anchors correctly in your Control nodes. See Multiple resolutions in the documentation for more information.

a year later

Future reader here. For getting a TextureRect filling the parent Node2D (the viewport) tiling, it doesn't work using any anchors or controls I can find. Have tried hundreds of combinations for several hours. Doing it in code works like a charm. If there is a working example of a tiling TextureRect in a parent Node2D please post it. Using version 3.4.4 stable.

For getting a TextureRect filling the parent Node2D (the viewport) tiling, it doesn't work using any anchors or controls I can find.

This is expected, as Viewport doesn't inherit from Viewport (or even from Node2D – it actually inherits a non-positional Node).

7 months later