Attention
Topic was automatically imported from the old Question2Answer platform.
Asked By
imekon
If I add a signal to a class in C#:
[Signal]
public delegate void Died();
EmitSignal("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:
[Signal]
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
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()
{
AlienDied?.Invoke(100);
}
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
system
June 20, 2021, 7:57pm
3
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});
Handle:
private void downbuttonclick(int buttonIndex){
GD.Print("CLICK "+ buttonIndex);
}
Other example, declare:
[Signal] public delegate void signalTileSelected(int row, int col);
Emmit:
EmitSignal(nameof(signalTileSelected),row,col);
Connect:
map.Connect("signalTileSelected", this, nameof(showTileSelected));
Handle:
private void showTileSelected(int row, int col){
GD.Print("Pos "+row+","+col)
}