0 votes

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.

asked Jun 2, 2018 in Engine by imekon (229 points)
edited Jun 2, 2018 by imekon

1 Answer

+1 vote
Best answer

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.

answered Jun 3, 2018 by imekon (229 points)
edited Jun 3, 2018 by imekon
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 How to use this Q&A? before posting your first questions.