How to get the screen dimensions in GDScript?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By chanon
:warning: Old Version Published before Godot 3 was released.

In GDScript, how can one get the screen dimensions that correspond to the value set in Project Settings / Display / width and height?

:bust_in_silhouette: Reply From: Mohammad Hadi Aliakb

you can find the size of viewport using this:

get_viewport().get_rect().size

or you can find the windows_size by

OS.get_windows_size()

This gets the size of the viewport which sometimes isn’t the same as the size in Project Settings / Display / width and height. For example I have test_width and test_height set to a different value than width and height and I have stretch_mode as 2D, in this case the code aboves shows the size of test_width and test_height instead of width and height.

chanon | 2016-02-24 04:43

see the edited version

Mohammad Hadi Aliakb | 2016-02-24 04:55

hmm… That still returns the wrong number for me. Test case:
(1) set width to 750, set height to 1333
(2) set resizable to false
(3) set test_width to 500, test_height to 887
I want to get the width/height as 750 and 1333

chanon | 2016-02-25 03:15

@chanon Actually yeah I have this issue too. It returns wrong values like you got.

codevanya | 2016-11-22 17:41

This method worked perfect in Godot 2.x, but doesn’t work on Godot 3.x ; in fact, the method isn’t even available any longer.

Brinux | 2018-02-10 13:59

use

OS.get_windows_size

in Godot 3.x
in generell most getter function got replaced by member variables in godot 3.0
see OS — Godot Engine (3.0) documentation in English

coffeeDragon | 2018-02-16 10:29

As of Godot 3.5.1, at least, get_viewport() no longer has a get_rect() method; it’s been replaced by get_visible_rect().

Speak2Animals | 2023-01-01 22:33

In Godot4:

DisplayServer.window_get_size()
get_viewport().get_visible_rect().size

d2clon | 2023-04-26 16:24

:bust_in_silhouette: Reply From: Reyalpsirc

If you want the values that you set on the Project Settings, then you need to use Globals:

var projectResolution=Vector2(Globals.get("display/width"),Globals.get("display/height"))

This is not working for Godot 3RC 1?

Jackain | 2018-01-21 00:08

Not sure. I did not try Godot 3 yet.

Reyalpsirc | 2018-01-22 00:22

it’s ProjectSettings.get_setting(“display/window/size/width”)) for Godot 3.0

arguskos | 2018-02-04 12:35

This is now:

ProjectSettings.get_setting("display/window/size/viewport_width")

for Godot 4.x

Juksefantomet | 2023-04-25 20:34

:bust_in_silhouette: Reply From: Brinux

In Godot 3, it has changed to this:

for the x (horizontal) dimension, use

get_viewport().size.x

and for the y (vertical) dimension, use:

get_viewport().size.y

Best up-to-date answer :slight_smile:

InCrIpTiOn | 2019-03-28 15:31

the only up-to-date answer here! thx
position.x = get_viewport().size.x / 2 or position.y = get_viewport().size.y / 2 for centering Sprites btw

CopperyMarrow15 | 2020-08-03 22:54

:bust_in_silhouette: Reply From: trevor
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)

Thanks, very nice! All the flavors here.

springeo | 2019-03-08 07:33

Best answer, containing all possibilities. Thank you.

Losatu | 2022-04-19 13:04

:bust_in_silhouette: Reply From: Pedro

screen_size = get_viewport_rect().size

only this worked for me on mobile

Hossein Vatani | 2020-12-25 08:19

:bust_in_silhouette: Reply From: iamcmnut

In Godot 3
You can also use
OS.window_size.x and OS.window_size.y
to get window current size.

:bust_in_silhouette: Reply From: davidbasile

In Godot 3.3 the function is:

ProjectSettings.get("display/window/size/width")

and:

ProjectSettings.get("display/window/size/height")

Please note that OS.get_window_size().x and OS.get_window_size().y still works in 3.3, likewise OS.window_size.x and OS.window_size.y.

As well as get_viewport().size.x and get_viewport().size.y (on the main viewport).

All of the above solutions will still work.

Yuminous | 2021-07-22 23:59

Oh, that’s nice

davidbasile | 2021-07-23 16:45

:bust_in_silhouette: Reply From: Dlean Jeans

Apparently in Godot 4, OS.window_size has been changed to:

DisplayServer.window_get_size()

for handling multiple windows. It takes 1 parameter window_id (default 0).

To add to this answer: To get the size of the monitor/screen, you can call DisplayServer.screen_get_size()

Erveon | 2022-08-04 08:42

:bust_in_silhouette: Reply From: patatra

There is this for 3

onready var screenWidth = get_tree().get_root().size.x
onready var screenHeight = get_tree().get_root().size.y

:bust_in_silhouette: Reply From: WolframR

There is an important distinction lacking from this thread, which is why there are so many answers. hopefully this will clear some things up.

get_viewport().size.x
get_viewport().size.y

The above retrieves the size dimensions of the viewport specifically. note that in godot, a viewport and the window are not always the same thing. sometimes a viewport is just a minimap, or a sniper scope view. in many first and third person games, it may be the size of the window itself, so it may not matter, but shrinking the viewport may not shrink the window the viewport is inside of if it isn’t root. (root is the root viewport so it’s like the whole window)

get_viewport() is a function of the node class, and is meant to retrieve the viewport the node is a child of, so the viewport returned will depend on which node or node script it’s being called on or in. thus, using this requires being aware of where it is being called, lest it return something you didn’t expect, and can only be used in or on nodes. maybe not a huge issue, ofc, but there you go.

this will retrieve the values the project was set at in the editor. HOWEVER this value does not change when the window size changes at runtime, and changing this will only affect the window after the game restarts. it’s a configuration setting.

this talks with the operating system directly to get the window size. while it probably works, something feels off about bypassing inbuilt godot functionality. it’ll return a vector xy for the size of the window.

get_tree().get_root().size.x
get_tree().get_root().size.y

this uses the root viewport, which is probably the best solution. it’s an inbuilt godot feature, changing it’s value will affect the entire window, and you will always at a glance be able to tell you are editing the actual full game window.

:bust_in_silhouette: Reply From: ULViDO

in godot 4 you need to use DisplayServer singleton instead of OS.

For example:

var window_size = DisplayServer.window_get_size(0) # 0 is window index

You can read the documentation about DisplayServer here.

:bust_in_silhouette: Reply From: CharlesGrimes

To get the screen dimensions that correspond to the value set in Project Settings / Display / width and height in GDScript, you can use the OS class.

Here’s an example code snippet:

var screen_width = OS.get_screen_size().x
var screen_height = OS.get_screen_size().y

The OS.get_screen_size() method returns a Vector2 object containing the width and height of the screen in pixels. The x and y properties of the Vector2 object correspond to the screen width and height, respectively.

Note that this will give you the actual screen size, not necessarily the size of the game window if you are running in a windowed mode. If you need to get the size of the game window, you can use the get_viewport_rect() method of the Viewport node that represents the game window. For example:

var viewport = get_viewport()
var window_width = viewport.get_rect().size.x
var window_height = viewport.get_rect().size.y

This will give you the width and height of the game window in pixels.