Godot Version
4.6.1 stable.mono.official
Question
Hey folks, I’m struggling to find the correct syntax to connect to a custom signal callback with parameters in C#. I have tried several different approaches including lambdas and .Bind() but lambdas are not valid and .Bind() is not being found with Intellisense ( coding in rider )
I want to collect the custom signals in a SignalBus. This extends node and is pre-loaded with configuration, it’s not in the node tree.
public partial class SignalBus: Node
{
[Signal]
public delegate void PickupEventHandler(PhysicsBody2D pickedUpBody);
}
My player class emits the event on colliding with something in the collectible layer
private void HandleCollectible(PhysicsBody2D collider)
{
EmitSignal(SignalBus.SignalName.Pickup, collider);
}
My GameController class handles the actual event. This is attached to the root node in Godot. The one with no arguments attaches fine, although currently that signal is declared in the PlayerController. I wanted to move all the custom signals to the signalbus to keep things clean.
private Callable gameOverCallback;
private Callable pickupCallback;
public override void _Ready()
{
//works fine
player = GetNode<Node>("level1/Player");
gameOverCallback = Callable.From(OnGameOver);
player.Connect(PlayerController.SignalName.Died, gameOverCallback);
//tried various approaches already, can't get the callback formatted
pickupCallback = Callable.From(physics)=> OnPickup(physics));
player.Connect(...);
}
//callback function, I want to pass the collider to delete the last pickup
private void OnPickup(PhysicsBody2D pickedUpBody)
{
if (!IsInstanceValid(pickedUpBody)) return;
Score += 10;
pickedUpBody.QueueFree();
}
Sharing the working callback too just in case it’s helpful
private void OnGameOver()
{
if (IsInstanceValid(player))
{
player.Disconnect(PlayerController.SignalName.Died, gameOverCallback);
}
GD.Print(“Game Over”);
}
