+36 votes

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

in Engine by (259 points)

10 Answers

+6 votes
Best answer

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"))
by (65 points)
selected by

This is not working for Godot 3RC 1?

Not sure. I did not try Godot 3 yet.

it's ProjectSettings.get_setting("display/window/size/width")) for Godot 3.0

+44 votes

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()
by (580 points)
edited by
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.
see the edited version
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 Actually yeah I have this issue too. It returns wrong values like you got.

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.

use

OS.get_windows_size

in Godot 3.x
in generell most getter function got replaced by member variables in godot 3.0
see http://docs.godotengine.org/en/3.0/classes/class_os.html

+50 votes

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

by (335 points)

Best up-to-date answer :)

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

+19 votes
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)
by (32 points)

Thanks, very nice! All the flavors here.

Best answer, containing all possibilities. Thank you.

+1 vote

screensize = getviewport_rect().size

by (16 points)

only this worked for me on mobile

+5 votes

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

by (24 points)
+2 votes

In Godot 3.3 the function is:

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

and:

ProjectSettings.get("display/window/size/height")
by (18 points)

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.

Oh, that's nice

+3 votes

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).

by (4,187 points)

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

0 votes

There is this for 3

onready var screenWidth = gettree().getroot().size.x
onready var screenHeight = gettree().getroot().size.y

by (65 points)
+4 votes

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.

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

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.

OS.window_size

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.

by (58 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.