OptionButton: Passing an id with add_item

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

Hey,
im trying to use the OptionButton node to display a certain amount of options to the player while giving each option a custom id (eg. 11 instead of 0 for the first one)

I’m trying to do this by using the add_item( String label, int id=-1 ) but whatever I use as my id, the game returns the normal one and ignores my id.

My code looks like this:

func _on_Button_toggled(button_pressed):
    if button_pressed:
	    clear()
	    add_item("item a", 11)
	    add_item("item b", 12)
	    add_item("item c", 13)
	    add_item("item d", 14)

What I think should happen: Player clicks on item a and item index is returned as 11
What actually happens: Player clicks on item a and item index is returned as 0

What am I doing wrong? I am using Godot 3.06.
Thanks!

Is _on_Button_toggled the function you connected to the OptionButton.item_selected signal? If not, can you show how you did this connection and what is the code of that function?

Zylann | 2019-01-23 14:05

Not directly, it works like this:

FirstOptionBox script:

func _on_FirstButton_toggled(button_pressed):
if button_pressed:
        clear()
        add_item("item a", 11)
        add_item("item b", 12)
        add_item("item c", 13)
        add_item("item d", 14)

func _on_SecondButton_toggled(button_pressed):
if button_pressed:
        clear()
        add_item("item a", 21)
        add_item("item b", 22)
        add_item("item c", 23)

func _on_ThirdButton_toggled(button_pressed):
if button_pressed:
        clear()
        add_item("item a", 31)
        add_item("item b", 32)

The buttons are normal TextureButtons connected with my OptionBox. My OptionBox is connected with a item_selected signal with another OptionBox. It is supposed to work like categories (eg. you pick category a and it displays four options in a second optionbox; if you pick category b you’ll get another 4 options in the second optionbox)

it looks like this:

SecondOptionBox script:

func _on_FirstOptionBox_item_selected(ID):
if ID >= 10 and ID <= 20:
	clear()
	add_item("first option a",101)
	add_item("second option a",102)
	add_item("third option a",103)
	add_item("fourth option a",104)
	
if ID >= 20 and ID <= 30:
	clear()
	add_item("first option b",101)
	add_item("second option b",102)
	add_item("third option b",103)
	add_item("fourth option b",104)
	
if ID >= 30 and ID <= 40:
	clear()
	add_item("first option c",101)
	add_item("second option c",102)
	add_item("third option c",103)
	add_item("fourth option c",104)

So, I want it to pick the id of the selection option from my first optionbutton and then display the right options on my second option box.
What I get is simply 0 for the first, 1 for the second…

DaZeus | 2019-01-23 14:45

Can you share a simple test project where it happens?

Zylann | 2019-01-23 20:50

Yeah, here is a demonstration:
File-Upload.net - Datei nicht gefunden

There are 3 different normal buttons which display different options in the first OptionButton. The second OptionButton displays options based on what has been chosen in the first OptionButton.
The code should give every option on the first OptionButton a custom id, which doesn’t work.

DaZeus | 2019-01-23 21:57

I confirm this is either a bug or a monumental documentation mistake. The engine code litterally sends an index to the item_selected signal. I’ll post an issue xD
OptionButton item_selected signal sends an index even though IDs are defined · Issue #25273 · godotengine/godot · GitHub

As a workaround you can convert the index into an ID by grabbing a reference to the OptionButton and calling get_item_id(idx)

You can also connect directly to the popup with get_popup().connect("id_pressed", ...)

Zylann | 2019-01-23 22:26

Okay, I will do it like that. Thank you! :slight_smile:

DaZeus | 2019-01-23 22:45