Cannot figure out how to change the screen resolution in runtime?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PancakePuncher

Hello Everyone,

So probably a fairly simple question and I’ve spent a lot longer trying to figure it out then I want to admit.

I’m attempting to setup a basic options page with some screen modes and screen resolution selections. The issue is… no matter how I pass those selections I cannot for the life of me get it to actual change the game screen.

I’ve attempted several methods:

My method thus far is that I have an options page with two drop down list - one for resolution and one for green mode.

When you select something in the drop down I call the _item_selected(index) func and I simply match that to the index. So 1: for 1920 x 1080. Then based on that match I perform the process of adjusting the necessary variables – the issue is no matter what I change – the resolution doesn’t change.

I’ve attempted to adjust the width/height of the display under the “display/window/size/width” and “display/window/size/height” paths From what I can discern this seems to be the correct way to set the resolution – but adjusting these in-code has not done anything for me using:

ProjectSettings.set_setting("display/window/size/width", "Example")

I’ve attempted to adjust the “rect” of the root node of the tree.

I’ve attempted to adjust the view port size.

Code wise this is where I have ended up:

extends OptionButton

var x = 1024
var y = 600

var ScreenRes = Vector2(x, y)

var SelectedRes = null

func _on_ResolutionOptions_item_selected(index):
	SelectedRes = index
	match SelectedRes: #1024 x 600
		0:
			x = 1024
			y = 600
		1: #1920 x 1080
			x = 1920
			y = 1080
	get_node("../../..").set_size(ScreenRes)

Help would be much appreciated.

:bust_in_silhouette: Reply From: p7f

Hi!
You have to use get_tree().set_screen_stretch(). You can check out this tutorial: Game Settings · GDQuest

Basically, you have the same options that you have in settings, as parameters to that function, for example:

get_tree().set_screen_stretch( SceneTree.STRETCH_MODE_2D, SceneTree.STRETCH_ASPECT_KEEP, settings.resolution)