The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+1 vote
    private void _Ready(){
        SomeButton = GetNode<Button>("SomeButton");
        String Param1 = "Very cool parameter.";    
        SomeButton.Connect(PopupMenu.SignalName.IndexPressed, new Callable(this, nameof(this.CoolMethod)));
    }
    private void CoolMethod(string Param1){ GD.Print(Param1); }

this is the code I have currently to load my game without error.
however I also want the method to be called with 'Param1' as the parameter, in gdscript I can just type the function and give what ever parameter directy but I can't figure out how to use these callables in c# with custom parameters.

Godot version godot 4 beta 2 mono
in Engine by (307 points)

4 Answers

+3 votes
Best answer

You can also create an Action, which can use any variable or value available in its current scope, and then create a Callable from it. In that way, you can do practically whatever you want without being tied to the limitations of Callable.

private void CoolMethod(string Param1) {
  GD.Print(Param1);
}


private void _Ready() {
    SomeButton = GetNode<Button>("SomeButton");
    String Param1 = "Very cool parameter.";
    Action myAction = () => { CoolMethod(Param1); };
    SomeButton.Connect(PopupMenu.SignalName.IndexPressed, Callable.From(myAction)));
}
by (69 points)
selected by

Perfect! Thanks a lot!

0 votes

For now, I guess you can use the Callable.bind(params...) method after creating the callable.
egg:

Callable method = new Callable(this, callbackName).bind(new Godot.Collections.Array<Godot.Node> { boss });
timer.Timeout += method;

It seems this method is not ported to C# yet. :/

by (14 points)
edited by

I tried your code but it throws Cannot resolve symbol bind.

Instead, this works for me.

StateMachine.cs

[Signal]
public delegate void CurStateUpdateEventHandler(string stateName);
...
EmitSignal(SignalName.CurStateUpdate, stateName);

StateMachineDebugger.cs

stateMachineNode.CurStateUpdate += stateName => UpdateLabelText(label, stateName);
...
private void UpdateLabelText(Label label, string text)
{
    label.Text = text;
}

doc

this one worked for me, but I've discovered it's an undeveloped feature in the beta and apparently is to be fixed on full release of 4

+1 vote

this works for me. https://docs.godotengine.org/en/latest/tutorials/scripting/c_sharp/c_sharp_features.html#doc-c-sharp-signals

StateMachine.cs

[Signal]
public delegate void CurStateUpdateEventHandler(string stateName);
...
EmitSignal(SignalName.CurStateUpdate, stateName);

StateMachineDebugger.cs

stateMachineNode.CurStateUpdate += stateName => UpdateLabelText(label, stateName);
...
private void UpdateLabelText(Label label, string text)
{
    label.Text = text;
}
by (16 points)
+1 vote

Thanks to the answer above (suribe) I tried it this way and it works.

private Button buStart;

public override void _Ready() {
    buStart = GetNode<Button>("%buPlay");
    buStart.Pressed += () => { onClickStartGame(true, true); };
}

private void onClickStartGame(bool server, bool withPlayer) {
    GD.Print("Oleee!");
}
by (50 points)
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.