Can't determine when viewport or window size changes

Godot Version

4.3 Stable

Question

I have tried both of these in a node, in the root node, and in my global autoload singleton. I resize the window repeatedly, but the functions never print my debug text, so I guess they never fire.

Is there a better way? Ultimately, I’m trying to fix an off-screen indicator that disappears off the right and bottom screen edges when the window is resized or maximized. I run it in viewport stretch mode, keeping aspect.

get_tree().get_root().size_changed.connect(update_window) # window has been resized
get_viewport().size_changed.connect(update_viewport) # viewport size has changed

In a simple project I just tried:

func _ready() -> void:
	get_tree().get_root().size_changed.connect(update_window)

func update_window():
	print("Foo")

Must be something with the stretch mode then, mine was set to disabled

Ah doesn’t seem to fire with stretch_mode equal viewport.

1 Like

Thanks for trying! I get that the pixel dimensions might be considered the same in that mode, but I don’t know why the offscreen indicator would break if the viewport or window weren’t considered different.

Stretch mode means, that the size of the viewport doesn’t change, when the size of the window changes and the Viewport.size_changed signal acts on viewport size changes.

You should overwrite the _notification function of the window and listen for NOTIFICATION_WM_SIZE_CHANGED. (to achieve this, this project could help)

The following proposal and pull request might also be of interest to you, if you compile the engine yourself.

2 Likes

That is nice that the low level _notification() works, cool. Nice to know. So many things to take into account.

For any future searchers, I had to rethink things because get_viewport().size doesn’t account for window scale. I solved by comparing get_viewport().get_visible_rect().size instead (I wasn’t even aware of that one at the time).

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