How do I connect a handler to buttons in MenuButton?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By imekon

I created them in the designer, adding a few buttons. I get an item list… but how do I connect to “pressed” with each button?

This is Godot V3.0

I believe the second half of that step by step guide will give you what you need
Scripting — Godot Engine (3.0) documentation in English

rustyStriker | 2018-02-05 22:53

I know how to connect ordinary buttons but how do I connect buttons in a MenuButton? I can’t select them in the editor, they’re in a modal dialog.

imekon | 2018-02-05 23:12

In case it helps anyone, it’s hard to find out how to edit the popup items for a MenuButton. You can’t do it in the Inspector; an “Items” button appears in the header of the main viewer when you select the MenuButton in the scene. Click that to edit the items.

garyo | 2018-02-09 18:05

:bust_in_silhouette: Reply From: rustyStriker

Ok, so after a bit of digging and testing(like 10-20 minutes) i found out that you need to use the get_popup method and connect the id_pressed signal from that popup that you got, aka like this:

get_popup().connect("id_pressed",self,"func_name")

and the function is set like this:

func func_name( ID ):
      # Some Beautiful Code Here <3

and ID is the number of the item that was selected…

hope i helped this time was fun exploring it tho

Bingo!

extends PanelContainer

onready var menuButton = $Panel/MenuButton

func _ready():
    var popup = menuButton.get_popup()
    popup.connect("id_pressed", self, "file_menu")

func file_menu( id ):
    match id:
        0:
            print("new")
        1:
            print("open")
        2:
            print("save")
        3:
            get_tree().quit()

And a match statement!

imekon | 2018-02-05 23:45

registered here to say thank you, spent my whole day looking for this staff

dr1nkEj | 2020-10-14 20:48