Search code examples
settingsgdscriptgodot4

How can I change the configuration of a Godot project through code?


I created this topic so I could ask you a question, how can I change the configuration of a Godot project through code. What happens is that I want to create a configuration menu for my project, which would be to change the resolution to give an example.

It sounds simple and it really is, but it turns out that most of the information is very old and is no longer compatible with Godot 4, since the majority would be for version 3.

I have tried using plugins like Godot Game Settings to make the task easier, but it turned out that it greatly reduced performance and caused many errors in its execution. Although this can be caused by many reasons not linked to the plugins.


Solution

  • how can I change the configuration of a Godot project through code.

    You can use ProjectSettings to read and write project settings. This is particularly useful for editor plugins.


    What happens is that I want to create a configuration menu for my project, which would be to change the resolution to give an example.

    There are various points when Godot reads the project settings. However, once it have read it, changing it has no effect. For example, if you game already started, changing the resolution in project settings is of no use.

    For the resolution, you probably want to manipulate the main Window, which is the root of the scene tree (get_tree().root).

    You can use get_tree().root.mode to change between windowed and fullscreen. And, of course, the size of the window is get_tree().root.size. Also see the content_scale_* properties which control how the Window will scale and stretch its contents.

    You might also be interested in DisplayServer.