How to use PopupMenu from MenuButton in C#?

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

This option does NOT suit me, since the Connect() method was changed in Godot 4

class SomeController {

  MenuButton MyMenu;
  int menuIndex = 0;

  public override void _Ready() {
       MyMenu = (MenuButton)this.FindNode("MyMenu");
       MyMenu.Text = "Item0";
       PopupMenu popup = MyMenu.GetPopup();
       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);

The method should look something like this

Connect("pressed", Callable.From(() => Test()));

More in the video

The error is reporting an argument count mismatch; you still need to provide args in your Callable, e.g.

 Callable.From((index) => Test(index));

Or even just Callable.From(Test) (assuming Test can receive the correct parameters. Even better, and idiomatic to C#:

popup.Pressed += Test;

It’s not 100% clear what you desire, just going off the error in the linked video. Hope that’s gives you a steer in the right direction.

spaceyjase | 2023-04-10 17:12