OptionButton clear() doesn't work and/or OptionButton updates from gdscript broken

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

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.

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

Andrea | 2021-01-18 22:10

:bust_in_silhouette: Reply From: brancoliticus

As seen in https://forum.godotengine.org/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: