Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be taking a brief and quick look at the OS singleton class in regards to window sizes understanding window resolution is fundamental to game programming with most games it's beneficial to understand how we can retrieve and edit the game view resolution before we begin let's understand what a resolution is and then aspect ratio a resolution also known as say display resolution is the distinct pixels in each dimension that can be displayed since we are dealing in two dimensional space the dimensions usually refer to the x axis and the y axis.
Let's move on. So In Godot, we have two different window values we can retrieve the first value is the player's device screen resolution. This could be their laptop monitor even a cell phone. The next value we can retrieve is our game window resolution sex and that's basically where we're drawing our images to the screen it's our game window. Now lucky for us. Godot offers us the global OS singleton class and that is the class we use when we want to retrieve the display resolution of the device along with the game resolution and for setting our game view window.
Let's go ahead and see how we can retrieve the player device resolution Godot only offers us a getter method in this regard and in order to get our device resolution we use the OS global singleton class capital O and S.. Keep in mind case sensitive followed by the dot notation altered by the method. Get screen size and keep in mind that this method returns back a vector to value the first value being a float value that represents the width in pixels and the second value being the height and pixel value.
Now keep in mind you have to think about your display settings. So for example if you're using a MacBook or a Mac you may have a retina display option. And so even though you think you may be 19 20 by 10 80 in reality you may be a little lower if you're using retina. For example if you're a Mac user you can download easy rez so you can see the values that get screen size returns back to you while being able to edit your computer's screen resolution size.
Moving on the second type of window that Godot allows us to retrieve is of course our own game view resolution. And on top of that we can also set its value. So there are two ways to set the game view resolution first is through Project settings under the display option you can edit the width and height over the second way is more direct and through code and I'm going to show you in this slide so to set your game view resolution you use the OS singleton class method called set windows size it takes in a vector to data type as an argument again the argument being a float value and pixel size for your width and the second value being the y axis which is again your height or rather the pixel height you want your game view resolution to be to retrieve your game view resolution you would use the get window size method and it returns back to you a vector to data type one thing to keep in mind when we want to edit our game view window is that we may want to set its position in setting position is quite simple again it offers us a getter and setter method the setter method is called set window position it takes in a vector to data type as an argument and to retrieve its position we use the get window position method and of course it gives us back a vector to value one thing to keep in mind is that when you're setting the windows position the offset for your window screen is the top left corner so again window position changes the offset which is located at the top left corner and we position it to the value you passed inset window position method as the argument moving on.
One thing to keep in mind is that by default Godot sets window resizing to true the window of recyclable property if you don't want your user to change the window size or at least the ability to manipulate window size by dragging it we can use the setter method called set window over a sizable and it takes in a boolean as an argument and if you don't want them to resize or window you just set it to false and of course it comes with a getter method called is window row sizable and it returns back boolean.
One thing to keep in mind is that a property that returns back a boolean we'll start with the is keyword. So if you see is in a getter method you can think to yourself oh OK I'm going to get back a billion value so what's the use for understanding how to retrieve and set resolution sizes. Well in this pseudocode example through three lines of code we can set our screen to full screen no matter what device the player is using. So the first line we need to retrieve the device resolution and we can use the get screen size method course we assign that to a variable.
In this case it's called Max because I don't have enough screen real estate where a clearer variable name. After that you would want to set your windows size and of course we would want to set our game resolution window to the exact same size as the devices resolution. And so we use these set windows size method and pass it a vector. Lastly keep in mind that those default window position is somewhere close to the center of the screen. And so in order for your user to see the entire game window we must set the window position to the top left corner of the devices screen.
And so we use vector 2 0 0. And what that does is it just adds it to the very corner. The origin of the user's device and then from there we will have a game that occupies the entire width and height of the screen. Keep in mind that there's going to be a little bar where the user can click to exit minimize or expand. Of course that's the harder way even though it's three lines. We can reduce the amount of lines we write in order to do full screen. And lucky for us Godot offers a property called Window for screen with a getter instead or method.
And of course to set this property we use the set window full screen and we pass in a boolean argument in this case true. And through one line of code we are able to set for screen on top of that I want to show you that we do have a property called Window borderless and to use that we use the setter method set borderless window we pass any boolean argument in this case it's true. And what that does is it gets rid the small bar at the top which allows the user to again exit minimize and expand.
Now if you set the borderless window to true just keep in mind that your user has no way of dragging or moving your game window. Now you may be wondering what's the purpose of understanding how to set and retrieve the game window resolution. And the simplest reason would be if you want to position your game object at the center of your game window. So we don't know what the game window size is going to be. And because of that X Max represents any width and Y Max represents at a height. And of course the center are our game object in the game window the x value will of course be x Max divided by two.
Again the unknown width divided by two. That's of course greater than zero. And to set the height we call them Y Max divided by two again being an unknown height that we know is going to be greater than zero. Let's take a look at another positioning example in this example we have a grid based game. And of course we would like to position our peak at you at the bottom left. So we know the origin is 0 0. However where exactly do we position it. And if we retrieve the game windows height the vector values passed to us when we get the game window size we'll be positive numbers.
But regardless from that value from get windows size we are able to position our peak true to the bottom left. And of course it would be zero. Why Max. Because again the vector to value being passed to us from get window size are positive integers or excuse me float values. And of course from there if we nor square size in this case 32 we can of course move our game object one unit to the rect and that's just a very basic example of why we want to understand getting our game window size.
And of course setting the game window size as well. Well that's all I have for you in this episode. Thank you so much for joining me. Thank you for clicking the Subscribe button. And thank you for clicking the like button. If you have any questions or comments please feel free to leave them in the comments section down below. I look forward to seeing you in the next episode. Have an amazing day