Get submenu item ID

Godot Version

4.2.2

Question

The documentation says that “PopupMenu.add_submenu_item” should generate an ID, I can’t see any way to get that ID.

I have a popup menu that is procedurally filled, and all have the same submenu but I cant find a way to see which submenu item generated the submenu, none of the signals seem to work.

From PopupMenu.add_submenu_item():

An id can optionally be provided. If no id is provided, one will be created from the index.

If you don’t provide one then the index of the item will be used. The id is not really necessary to interact with the PopupMenu API as far as I can tell. It’s there for your own code like if you assign an specific id to an item to reference it later.

extends Node


@onready var menu_button: MenuButton = $MenuButton


func _ready() -> void:
	var popup = menu_button.get_popup()
	popup.add_item("First Item")
	popup.add_submenu_item("Submenu", "Submenu")
	popup.add_item("Third Item")
	popup.index_pressed.connect(_on_index_pressed.bind(popup))

	var submenu = PopupMenu.new()
	popup.add_child(submenu)
	submenu.name = "Submenu"
	submenu.add_item("Submenu item 1")
	submenu.add_item("Submenu item 2")
	submenu.index_pressed.connect(_on_index_pressed.bind(submenu))


func _on_index_pressed(index:int, menu:PopupMenu) -> void:
	print('Menu: %s index: %s id: %s text: %s' % [menu.name, index, menu.get_item_id(index), menu.get_item_text(index)])

Prints:

Menu: @PopupMenu@7 index: 0 id: 0 text: First Item
Menu: Submenu index: 0 id: 0 text: Submenu item 1
Menu: Submenu index: 1 id: 1 text: Submenu item 2
Menu: @PopupMenu@7 index: 2 id: 2 text: Third Item