How do I change the Viewport/Window size without going into project settings?

Godot Version

4.2.2

Question

The default ViewPort size in Project Settings is 1152 X 648.
I want to change the viewport size to a custom size stored in a Vector2 variable, without changing the default value in Project Settings. I have set the stretch settings I want for the project:

I have tried every solution I could think of:

func _ready()
    ProjectSettings.set_setting("display/window/size/viewport_height", map_size_px.x)
    ProjectSettings.set_setting("display/window/size/viewport_width", map_size_px.y)
func _ready()
    ProjectSettings.set_setting("display/window/size/window_width_override", map_size_px.x)
    ProjectSettings.set_setting("display/window/size/window_height_override", map_size_px.y)

Both of these settings don’t work at all, the viewport/window size is default, and the stretch settings are unchanged:

func _ready():
	get_viewport().size = map_size_px

This is only changing the window size, the resolution and the aspect ratio of the project remains same:
Screenshot 2024-06-02 113234

I don’t know of any other setting to change.

when you change project settings directly like that, iirc you have to restart the project aka reopen it but if you reopen it, it will reset back the value again, so nothing will ever change

But to set window size of your game, you do it from
DisplayServer.window_set_size()

It still does the same thing as get_viewport().size. The window size has changed but the aspect ratio did not…

I was actually trying to follow the intro to gdscript 2 tutorial by GDquest. He uses get_tree().set_screen_stretch(......) in godot 3. I couldn’t find this method even in the documentation of godot 4.
In the tutorial, the in-game resolution changes with the window size:
Screenshot 2024-06-02 171416

i always suggest people to learn from the same engine major version, if you are very new to godot. trying to convert godot 4 to godot 3 or the other way will make you come to this place and ask. which is correct to do. but really it can be avoided if you just learn from godot 4 tutorials directly.
but ok, what you are looking for is the set_screen_stretch on godot 4, which is actually this on project settings:


try change the stretch mode to canvas_items

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.