fullscreenscreen-resolutiongodot4

How to set godot4 game screen resolution when launched


Because my fullscreen godot game would be avaliable on many platforms (e.g. mac, windows) so I have to make sure that everytime my game launches on it's first time, it checks the OS and device it's on and sets the correct resolution for the device. How do I do this? I want something like this:

func _ready():
    device = project.get_device()
    project_settings.set_setting("window_width", device.width)
    project_settings.set_setting("window_height", device.height)
    project_settings.save()

note that the code I wrote is not actual GDscript, it's just an example of something I want.


Solution

  • You can set project settings like this:

    ProjectSettings.set("display/window/size/viewport_width", 320)
    ProjectSettings.set("display/window/size/viewport_height", 240)
    

    But for the time your code runs, it is too late for that. You could instead get the window and set the size, like this:

    get_window().size = Vector2(320, 240)
    

    Or the viewport:

    get_viewport().size = Vector2(320, 240)
    

    To identify the platforms you can use OS.get_name():

    prints(OS.get_name())
    

    Or work with OS.has_feature()


    But better yet, don't do this by code. Instead go to Project Settings and define overrides for the platforms you want.

    For example, if I want to override the width for Android, I select Viewport Width:

    Viewport Width selected

    Then select the feature "android" from the drop down list at the top:

    Selecting "android"

    Then click on add:

    Hovering on add

    That adds a new "Viewport Width.android" property that will be effective on targets that has the feature "android" (i.e. on Android platforms):

    "Viewport Width.android" selected

    Which you can set to whatever you want.