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?
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:
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!