How to set shrink with an OptionButton

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

I have written this:

extends OptionButton

var optionID

func _on_OptionButton_item_selected(index):
	get_item_id(index)
	var optionID = index


func _process(delta):
	if optionID == 1:
		get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_VIEWPORT,  SceneTree.STRETCH_ASPECT_EXPAND, Vector2(480,360),1)
	elif optionID == 1:
		get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_VIEWPORT,  SceneTree.STRETCH_ASPECT_EXPAND, Vector2(960,720),2)
	elif optionID == 3:
		get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_VIEWPORT,  SceneTree.STRETCH_ASPECT_EXPAND, Vector2(1440,1080),3)
	elif optionID == 4:
		get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_VIEWPORT,  SceneTree.STRETCH_ASPECT_EXPAND, Vector2(1920,1440),4)

but changing the options doesn’t seem to do anything. The function _on_OptionButton_item_selected is connected from the OptionButton to itself, and connecting it to the base node isn’t working because the get_item_id function doesn’t work there

:bust_in_silhouette: Reply From: zeludtnecniv

Why have you created a variable optionID instead of using the already existing “selected” variable ?
Secondly there is a error on the first elif of your _process function, i think you mean to try if optionID is 2.

Try with this :

extends OptionButton

func _process(delta):
    if selected == 1:
        get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_VIEWPORT,  SceneTree.STRETCH_ASPECT_EXPAND, Vector2(480,360),1)
    elif selected == 1:
        get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_VIEWPORT,  SceneTree.STRETCH_ASPECT_EXPAND, Vector2(960,720),2)
    elif selected == 3:
        get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_VIEWPORT,  SceneTree.STRETCH_ASPECT_EXPAND, Vector2(1440,1080),3)
    elif selected == 4:
        get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_VIEWPORT,  SceneTree.STRETCH_ASPECT_EXPAND, Vector2(1920,1440),4)

So now when I run the game, shrink is set to 2, but when I chose any of the options, it changes only to 1 and the resolution always stays at 480x360
Edit: yes, that was a typo that i already fixed

iNeedMentalHelp | 2023-06-25 14:57

If you want to change the window size as well, use this. By the way do it in the process is really not efficient, reuse your selection change signal. Note : selected start at index 0 (for the first element) i suppose it correspond to 480x360. If you don’t want the game to be zoom in just change delete “set_screen_stretch”.

func _on_OptionButton_item_selected(index):
    var size = Vector2(480,360)
    if selected == 1:
	    size = Vector2(960,720)
    elif selected == 2:
	    size = Vector2(1440,1080)
    elif selected == 3:
	    size = Vector2(1920,1440)
    get_tree().set_screen_stretch(SceneTree.STRETCH_MODE_VIEWPORT,  SceneTree.STRETCH_ASPECT_EXPAND, size,selected + 1)
    OS.set_window_size(size)
    OS.center_window()

zeludtnecniv | 2023-06-25 15:32

Thanks, I had to change up the code a bit myself (which i don’t blame you for since you don’t have access to my files), but I eventually got it to work. I just have a question about OptionButtons: is there any way to make OptionButtons not respond to the mouse without disabling the cursor in any way?

iNeedMentalHelp | 2023-06-29 13:21

Nevermind, I didn’t see the ignore option in Control>Mouse>Filter before

iNeedMentalHelp | 2023-06-29 13:59