How to keep the selected value of an OptionButton

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

I’m using an OptionButton to change the resolution, but when I exit the Options menu and go back to it, it instantly changes back to the default resolution
This is the script:

extends OptionButton

var size

func _process(_delta):
	if selected == 0:
		get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_2D,  SceneTree.STRETCH_ASPECT_KEEP, Vector2(480,360), 1)
		size = Vector2(480,360)
	elif selected == 1:
		get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_2D,  SceneTree.STRETCH_ASPECT_KEEP, Vector2(960,720), 2)
		size = Vector2(960,720)
	OS.set_window_size(size)

I tried putting

func _ready():
	if size == Vector2(480,360):
		selected = 0
	if size == Vector2(960,720):
		selected = 1

in there but it didn’t work.
If it was possible I would like to put it in a save file so it stays the same even after restarting the game

How you’re showing this menu? Because if you’re re instancing every time that want to change the resolution, the option button will reset for the saved scene value.

Also why you’re checking this in _process()? Instead you can use item_selected signal to know when user choice a new resolution: OptionButton — Godot Engine (3.5) documentation in English

You can use ConfigFile to save the resolution info and load later:
ConfigFile — Godot Engine (3.5) documentation in English

matheusmdx | 2023-06-30 14:06

I tried making it so that the option menu get added to the title screen as a child, but that didn’t work, and I don’t think that matters anyway since the game whill have many, possibly even hudreds of scenes and I can’t just let them all run at once since that will slow down the game and maybe even crash the computer.

I don’t exactly remember what happened but I tried using the itemselected signal and it didn’t work out

But couldn’t I make it work out somehow with this?

func _ready():
    if size == Vector2(480,360):
        selected = 0
    if size == Vector2(960,720):
        selected = 1

Also I already found out about the ConfigFile just after posting this question, but I’m not sure how I would save the selection in a configfile. Maybe something put it in the OptionButton script and write something like this:?

var config = ConfigFile.new()

config.set_value("savefile", "selected", "selected")

config.save("user://Resolution.cfg")

iNeedMentalHelp | 2023-06-30 14:57