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