How to get Dropdown menu Popup ID?

Godot Version 4

Hello, I know this has been asked before but I didn’t understand the explanations.
I am looking to access the dropdown sub menu ID in order to link them to an event in script.

I have a simple food button, that when you click on it, has 3 options: one for bone, kibble, and meat. I would like to attach a function via script to each menu option, but am pretty sure I need to get the sub menu ID.

I’ve tried some of the partial code answers I’ve found here to no avail. Any help would be appreciated!

I assume you’re talking about OptionButton?

You can connect to its signal item_selected(index: int) and read the index parameter to perform different actions based on the selection.

I am actually trying to use the “menu button” and it doesnt seem to have the same signals available. I tried out the options button style, and i just do not like it as much.

the item selected index like you pointed out for the options style is exactly what i’m trying to do with the menu button style though :slight_smile:

1 Like

You will have to get the underlying PopupMenu object. This is done with MenuButton’s get_popup() function, then connecting to id_pressed

func _ready() -> void:
	var popup: PopupMenu = $MenuButton.get_popup()
	popup.id_pressed.connect(_on_id_pressed)

func _on_id_pressed(id: int) -> void:
	print(id)
1 Like

thank you so much, that worked!