How do I pass arguments with signals in C#

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

If I add a signal to a class in C#:

public delegate void Died();


and add handlers in other classes:

alien.Connect("Died", this, "OnAlienDied");

private void OnAlienDied()

How do I pass arguments via this signal?

I tried:

public delegate void Died(int score);

EmitSignal("Died", 30);

alien.Connect("Died", this, "OnAlienDied");  // <- do I need something here?

private void OnAlienDied(int score)

I tried this but it crashes Godot itself.

Just a tip for anyone in the future because I looked at this and had messed up despite me following the answers: make sure whatever object your connecting to is correct and will actually emit it. I ended up just using the “this” object.

wooshuwu | 2022-01-19 01:35

:bust_in_silhouette: Reply From: imekon

C# already has a signal system built into the language, called events. They allow arguments to be passed:

public delegate void AlienDiedDelegate(int score);

public event AlienDiedDelegate AlienDied;

public override void _Ready()
	AlienDied += OnAlienDied;

private void _onButtonPressed()

private void OnAlienDied(int score)
	GD.Print($"Alien died: {score}");

Note that this won’t show up in the Godot signal editor, so it’s only of use to dynamic instances of objects.

1 Like
:bust_in_silhouette: Reply From: Barrrettt

You can pass parameters on c#, example :

buElevations.Connect("pressed", this, "downbuttonclick",new Godot.Collections.Array{0});
buStyles.Connect("pressed", this, "downbuttonclick",new Godot.Collections.Array{1});


private void downbuttonclick(int buttonIndex){
    GD.Print("CLICK "+ buttonIndex);

Other example, declare:

[Signal] public delegate void signalTileSelected(int row, int col);




map.Connect("signalTileSelected", this, nameof(showTileSelected));


private void showTileSelected(int row, int col){
  GD.Print("Pos "+row+","+col)