This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

I want to create a Modal in Godot using C#. Then I decided to create a Singleton of it, because it will be used by all the project and then to call this Modal, you need to pass actions to be assigned to the modal buttons.

I achieved something like that:

public void Alert(
        string title,
        string message,
        List<OptionButton> optionButtons
    )
    {
        Show();
        Title.Text = title;
        Message.Text = message;
        for (int i = 0; i < Buttons.Length; i++)
        {
            if (i < optionButtons.Count)
            {
                Buttons[i].Text = optionButtons[i].Text;
                Buttons[i].TooltipText = optionButtons[i].Tooltip;
                Action callback = optionButtons[i].Action;
                Buttons[i].Connect(
                    "pressed",
                    new Callable(this, nameof(callback))
                );
            }
            else
            {
                Buttons[i].Hide();
            }
        }
    }

The problem is that the "callback" variable is an anonymous function, and not a class function. Is there a way to solve this?

Godot version 4.0
in Engine by (12 points)

1 Answer

+1 vote

You can use the Callable.From function:

            Action callback = optionButtons[i].Action;
            Buttons[i].Connect(
                "pressed",
                Callable.From(callback)
            );

Or even directly, of course:

            Buttons[i].Connect(
                "pressed",
                Callable.From(optionButtons[i].Action)
            );
by (69 points)

Thank you!!! It worked!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.