The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

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!

in Engine by (12 points)

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?

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...

Can you share a simple test project where it happens?

Yeah, here is a demonstration:
https://www.file-upload.net/download-13481772/OptionButtonTest.rar.html

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.

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
https://github.com/godotengine/godot/issues/25273

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", ...)

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

Please log in or register to answer this question.

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.