How to use PopupMenu from MenuButton?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Rokkimies
:warning: Old Version Published before Godot 3 was released.

So the MenuButton will bring up a popupmenu, how do you track if an item in the menu is clicked?

Normal popupmenus will track events, for example pressed or motion, but so far I haven’t been able to get ANY event from MenuButton’s PopupMenu. If I attach a script in MenuButton the func _input_event(event): only tracks what happens with MenuButton, but the popupmenu itself is completely ignored.

This makes me wonder why is popupmenu generated by MenuButton working completely different than normal popupmenu, they are same thing, right?

I’ve read the documentation and forums through, spent around 3 or 4 hours on this now. There’s literally nothing on any forum and documentation doesn’t answer to anything.

Help would be appreciated. Even knowing that MenuButton generated popupmenu is useless would have saved me hours.

:bust_in_silhouette: Reply From: mollusca

Use get_popup() in MenuButton to get the popup menu, then connect the id_pressed signal. Example:

extends MenuButton

var popup

func _ready():
    popup = get_popup()
    popup.add_item("item a")
    popup.add_item("item b")
    popup.add_item("item c")
    popup.connect("id_pressed", self, "_on_item_pressed")

func _on_item_pressed(ID):
    print(popup.get_item_text(ID), " pressed")

Oh, thank you! But you make one mistake. Not item_pressed.The signal is called id_pressed

Ezillo | 2017-10-18 17:18

This: get_popup() says it is non-existent and apparently rthis is the only way? OMG im so frustrated, 3 weeks trying to make a simple popup work, this is bad, either Im a complete idiot, and I can make a popup work on other engines, or this is just weird.

rafah | 2019-03-11 14:15

User rafah - Godot Engine - Q&A u can only use get_popup in :

extends MenuButton

I was NOT aware of that neither

Okan Ozdemir | 2019-10-22 19:14

:bust_in_silhouette: Reply From: emmBee

Adding a C# Example - as of Godot 3.3.3, with simple radio-button exclusion behaviors

class SomeController {

      MenuButton MyMenu;
      int menuIndex = 0;

      public override void _Ready() {
           MyMenu = (MenuButton)this.FindNode("MyMenu");
           MyMenu.Text = "Item0";
           PopupMenu popup = MyMenu.GetPopup();
           popup.AddRadioCheckItem("Item0");
           popup.AddRadioCheckItem("Item1");
           popup.SetItemChecked(0, true);
           popup.Connect("index_pressed", this, "HandleMenu");
     }

     private void HandleMenu(int index) {
          PopupMenu popup = MyMenu.GetPopup();
          popup.SetItemChecked(menuIndex, false);
          popup.SetItemChecked(index, true);
          menuIndex = index;
          MyMenu.Text = popup.GetItemText(index);
    }
}