Getting the rendered screen size?

Godot Version

v4.4.1 stable

Question

I am trying to use the warp_mouse() method to move the mouse to a specific position on the screen.

Input.warp_mouse(Vector2(get_viewport().get_visible_rect().size.x*(cursor_position.x/960.0),get_viewport().get_visible_rect().size.y*(cursor_position.y/540.0)))

This works well in fullscreen and when not maximized, but breaks when it is maximized due to the black bars at the sides of the screen that appear due to the difference in resolution. Is there a method to get the size of the viewport based on how large the screen is, or to get the scale factor of the viewport?

Given that you use (960, 540,), which is half of 1080p resolution, I assume you want to teleport the mouse to the center of the game window? In that case, this code will work:

get_viewport().warp_mouse(get_viewport().size / 2)
3 Likes

Thank you so much! I didn’t realize you could call warp_mouse() on a viewport as opposed to Input.

1 Like