How is MenuButton node supposed to work

:bust_in_silhouette: Reply From: ld2studio

To use a MenuButton node, you have to add items with the method $MenuButton.get_popup().add_item(“Item 1”, id_1) with id_1 = integer.

When you connect id_pressed to a callable, you retrieve as parameters the id specified previously.

Example :

enum ItemId {
	LEVEL1,
	LEVEL2,
	LEVEL3
}

func _ready():
	menu_button.get_popup().add_item("Level 1", ItemId.LEVEL1)
	menu_button.get_popup().add_item("Level 2", ItemId.LEVEL2)
	menu_button.get_popup().add_item("Level 3", ItemId.LEVEL3)
	menu_button.get_popup().id_pressed.connect(_on_item_menu_pressed)

func _on_item_menu_pressed(id: int):
	print("Item ID: ", id)

Thanks! That worked perfectly. I want to add that it’s not essential to create the items by code, I created them by the engine and it still works.

sauttize | 2023-04-07 03:01

3 Likes