Search code examples
game-enginegame-developmentgodotgodot4

Dynamic window resizing


I have created a setting item on the main screen in Godot 4, but I want to create a setting item to change the screen size in the settings, and I am having trouble getting it to work, even after looking at references and such.

What I was hoping to do is to have a setting screen and be able to change the screen size with the left and right cross keys, but I have already finished programming the cross keys.

What we tried was to use the ProjectSettings

ProjectSettings.set_setting("display/window/size/width", 1920)
ProjectSettings.set_setting("display/window/size/height", 1080)

I tried to write the above but it does not work. Is there a function to apply it?


Solution

  • In Godot 4, from a Node in the scene tree, you an get a reference to the Window it is in with get_window():

    var window := get_window()
    

    And it has a size property. A property, that you can set. In GDScript uou can either set it by component:

    var window := get_window()
    window.size.x = 1920
    window.size.y = 1080
    

    Or you can set a new size (which is what you would do in any other language). It is a Vector2i (as a look in the documentation would confirm):

    var window := get_window()
    window.size = vector2i(1920, 1080)
    

    And, of course, you can do the whole thing in a single line:

    get_window().size = vector2i(1920, 1080)
    

    It does not get any simpler than than.