Resizing contents of a scene to fit on the window, but only a bit after the window has had its size changed

Godot Version

Godot 4.0

Question

Hello! I have implemented a thingy to fit the contents of a scene to the window, when the window has its size changed. However, it is resizing way too often and I do not like it:


Here is a recreation of the code:

func _ready():
	get_tree().get_root().size_changed.connect(window_size_changed)

func window_size_changed():
	# Tell everybody to change!

Is there a way to delay when the signal can be sent or received, so that the contents are only resized once ~0.5 seconds after the window’s size has been changed?

Thanks in advance! :smiley:

Hi,

The signal being part of the engine code, you cannot delay it.
However, you can absolutely delay the receiver function, by adding a second function with an await call inside of it:

func _ready():
    get_tree().get_root().size_changed.connect(window_size_changed)

func window_size_changed():
    await window_size_changed_delayed()

func window_size_changed_delayed():
    await get_tree().create_timer(0.5).timeout
    # Tell everybody to change!

You have to call that function with the await keyword for the 0.5s waiting to actually work. More info on that topic here.
Let me know if that helps.

1 Like

Hello!

Thanks for the response, but that isn’t exactly what I am looking for, as this:

func window_size_changed_delayed():
    await get_tree().create_timer(0.5).timeout
    # Tell everybody to change!

…Would delay every call for 0.5 seconds.

Instead, I would like to delay when it can call (0.5 seconds after the last “window resizing”).

But, I did figure out how to do it. The solution was a Timer -node! Now, instead of telling everything to change, “window_size_changed()” starts a timer for 0.5 seconds and when that timer reaches zero, then I tell everything to resize. If the window is scaled before the timer reaches zero, it interrupts it and starts it again:

@onready var WINDOW_RESIZE_TIMER: Timer = $window_resize_timer

func _ready():
	get_tree().get_root().size_changed.connect(window_size_changed)

func window_size_changed():
	WINDOW_RESIZE_TIMER.start(0.5)
	

func _on_window_resize_timer_timeout():
	# Tell everybody to change!
	

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.