How to properly set project settings and change resolution with code for 2D game?

Godot Version 4.2

problem

2D game, hand drawn sprites.
I’m making a resolution change option. But when I’m changing it, game dispaly brokes.
native resolution for game is 1080p.

project settings

resolution change code

var resolutions := { # resolutions list
	"4k":Vector2(3840,2160),
	"1440p":Vector2(2560,1440),
	"1080p":Vector2(1920,1080),
	"720p":Vector2(1280,720)
}
# change res function
func Change_resolution(index):
	var selected_res = resolutions.get(res_selector.get_item_text(index))
	get_viewport().size = selected_res

screenshots

noraml 1080p


1080p → 1440p

1080p → 4k

Expected behaviour

I expect that the game will be always in fullscreen and in screen border. With resolution change will change scale of interface and quality of art.

what I tried but it didn’t work

I tried use method ProjectSettings.set_setting() but it didnt do anyting
I tried different project settings, but it behaves wrong, or resolution doesnt change.

QUESTION

How to properly set project settings and change resolution with code for 2D game (with hand drawn sprites)?

Godot Version 4.2

You can use DisplayServer

Using integer scaling will add black borders if the size of the window is not an integer multiple of the base viewport size. 1440/1080 = 1.33333 so it will show the black borders. More information about supporting multiple resolutions: Multiple resolutions — Godot Engine (stable) documentation in English

If you want to change the size of the Window at runtime you can use Node.get_window() to get the window and change its Window.size there