Selecting Values From OptionButton?

Godot Version

v4.6

Question

Hello, I’m having a very difficult time pulling the chosen values from the OptionButton asset in my game. I’ve tried get_item_id, get_item_text, get_item_index, etc… but none of them output any values to my variable that it is tied to. Apologies in advance if this has been asked previously, but I couldn’t find anything of the sort. I’m assuming because it’s a rather intuitive thing, but I’m just not skilled enough to figure it out on my own.

OptionButtons aren’t tied to any variables you may have, what do you mean by this? Could you share some of your script?

For sure. I have a day selector OptionButton that I would like to output to the variable day_chosen as an integer so I can compare it to a randomly generated value that the user would have to match.

day_chosen default var:

var day_chosen:int = 0

func _on_day_select_item_selected(index: int) → void:

day_chosen = day_select.get_item_id(index)

for the get_item_id part, it’s basically just filler because none of the parameters work.

How are you populating the options in the button?

The buttons are populated through the editor items dropdown

It looks like you just get the item index and store it as a variable. Do you use it after assigning it?

If not, try to double check with a print after what you already have. There should be a value there unless something is wrong elsewhere in the code.

Also, what is index? Is it the index of the item in an array?

Seems like your code should work as is. What do you want to happen?

The end of that line should be ignored, because it’s just an example at this point. I can’t get any of the functions that come with OptionButtons to work. I’ve tried ID, text to int, everything. My debug always says that it returns as 0.

Ideally for the day_chosen variable to contain the number that is selected from the OptionMenu. Right now, that specific OptionMenu is populated with only numbers, and I’m trying to get the numbers from that and feed them into the variable so it can be compared to the user’s input.

Do you want the text content of the options to be converted to a number? Maybe you want get_item_text then to use .to_int()

func _on_day_select_item_selected(index: int) -> void:
    day_chosen = day_select.get_item_text(index).to_int()
1 Like

Worked perfectly. I simplified it to: day_chosen = int(day_select.get_item_text(index))

Thank you everyone for your help, and have a nice night