This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

My object tree is:

Parent
    OptionButton1
    OptionButton2

On OptionButton1 I have a signal that connects the item_selcted(int) signal to the func in OptionButton1:

func _on_OptionButton1_item_selected(index):
    if index == OB1_ID1:
        get_parent().get_node(
                "OptionButton2").clear()
        get_parent().get_node(
                "OptionButton2").add_item(...)
        get_parent().get_node(
                "OptionButton2").add_item(...)
    elif index == OB1_ID2:
        get_parent().get_node(
                "OptionButton2").clear()
        get_parent().get_node(
                "OptionButton2").add_item(...)
        get_parent().get_node(
                "OptionButton2").add_item(...)

My intention is that I select an item from OptionButton1 and different options appear in OptionButton2 after the call to clear() depending on if OB1_ID1 or OB1_ID2 is selected.

When run OptionButton2 appears empty as expected. In OptionButton1 the item at OB1_ID1 is selected. But when I select the item at OB1_ID2 the items in OptionButton2 become those I expected when OB1_ID1 in OptionButton1 is selected. And when I continue changing the selection in OptionButton1 the items in OptionButton2 stay what I expect when OB1_ID1 is selected, they do not change. In the meantime in other parts of the code using get_selected_id() on OB1_ID1 and OB1_ID2 from OptionButton1 and the respective id's from OptionButton2 works as if there was no problem.

Any insight is welcome.

Godot version v3.2.3 official
in Engine by (19 points)

it took me a solid 5 minutes to figure out that OptionButton is not a button but an item list...

if i understood correctly, it seems the script is not adding the correct items, are you sure the problem is not in the (...) part, instead of the clear() part?

do some debuggin, add some print() to understand where does the code behave in a way you do not expect

1 Answer

0 votes
Best answer

As seen in https://godotengine.org/qa/39689/optionbutton-passing-an-id-with-add_item:

The function _on_OptionButton1_item_selected(index) doesn't get called with the id of the item selected but with the index of the item selected. Therefore it is required to do

func _on_OptionButton1_item_selected(index):
    the_item_id = get_item_id(index)
    if the_item_id == OB1_ID1:

instead of:

func _on_OptionButton1_item_selected(index):
    if index == OB1_ID1:
by (19 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.