How to change in-game resolution?

Godot Version

4.4

Question

How can I scale my game for different resolutions? For example, I added a menu setting that allows for changing resolution. My goal here is to make the game render that resolution, not display it so to say. I run my game in 1280x720 window and in the screenshot below you can see what happens when I try changing the resolution.

A couple of unwanted elements show up (vertical black bars, light gray window, dark gray window…) but I only want the pure black window to take the full screen and scale the rest of the UI with it.

As for project settings I use viewport set to 1280x720, Windowed mode. Stretch settings are canvas_items and aspect keep. With canvas_items changed to viewport same thing happens.

Code:

func _ready() -> void:
	resolution_option.add_item("1280x720")
	resolution_option.add_item("1680x1050")
	resolution_option.add_item("1920x1080")
	resolution_option.add_item("3840x2160")
	
	resolution_option.item_selected.connect(_on_resolution_selected)
	mute_button.toggled.connect(_on_mute_toggled)
	$SettingsPanel/BackButton.pressed.connect(_on_back_pressed)
	
	$SettingsPanel.hide()

func _on_resolution_selected(index: int) -> void:
	var selected_resolution: String = resolution_option.get_item_text(index)
	var resolution_parts: Array = selected_resolution.split("x")
	var width: int = int(resolution_parts[0])
	var height: int = int(resolution_parts[1])

	get_window().content_scale_size = Vector2(width, height)

Before this, my last part of the second function was this but with this nothing changed on selection.

func _on_resolution_selected(index: int) -> void:

DisplayServer.window_set_size(Vector2i(width, height))

	DisplayServer.window_set_position(DisplayServer.screen_get_position() + (DisplayServer.screen_get_size() - Vector2i(width, height)) / 2)

I just want to torture my graphics card rendering my game at 8k.

I assume that you want the effect of changing the viewport size in Project Settings, but on runtime? Then you should change the viewport size of the root node on runtime. The root node is a Window, which extends Viewport.

get_tree().root.size = new_size

Then you can change the window size with DisplayServer.

1 Like

I want to do it just like the AAA games. Choose a predefined resolution, then render and display it without changing the UI size or window. For this matter I think full screen mode is necessary though.