how to set focus on an item in option button by code

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

good morning ,this is my 4th question here

i’m asking as the question above

on my experience we can set focus by

$somebutton.grab_focus()

its work on the button

what i want here is to grab focus on the item inside option button

:bust_in_silhouette: Reply From: GlitchedCode

I know it has been a few years but, maybe someone else will be wondering or needing to do this as well. In order to do this, I have an optionbutton with the default name and I created a function to handle this kind of focus.

If I understand correctly, you want to focus not on the option button, but the drop down menu within it. To do this I created the following function:

func item_menu_focus(button: OptionButton):
	button.get_popup().visible = true
	button.get_popup().grab_focus()
	button.get_popup().rect_position = button.rect_position
	button.get_popup().rect_position.y += button.rect_size.y
	button.get_popup().rect_size.x = button.rect_size.x

All I have to do now is call this function and pass in the option button for who’s menu I would like to focus on. First we start be getting the dropdown menu related to the option button we pass in, and make it visible on the screen. Next up we use grab_focus on it to, well, grab the current focus. If You have not clicked on this OptionButton at all, the next three lines simply size and position the dropdown menu to where it should be as if you had clicked on it.