+20 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 (236 points)

6 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

+25 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 (577 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

+27 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 (317 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

+9 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.

+1 vote

screensize = getviewport_rect().size

by (16 points)
0 votes

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

by (14 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 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 webmaster@godotengine.org with your username.

Categories