Up to date

This page is up to date for Godot 4.1. If you still find outdated information, please open an issue.

C# signals

For a detailed explanation of signals in general, see the Using signals section in the step by step tutorial.

While it is still possible to use signals through the Connect/Disconnect API, C# gives us a more idiomatic way to implement the observer pattern.

Signals as C# events

To provide more type-safety, Godot signals are also all available through events. You can handle these events, as any other event, with the += and -= operators.

  1. Timer myTimer = GetNode<Timer>("Timer");
  2. myTimer.Timeout += () => GD.Print("Timeout!");

In addition, you can always access signal names associated with a node type through its nested SignalName class. This is useful when, for example, you want to await on a signal (see await keyword).

  1. await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);

Note

Godot will take care of disconnecting all the signals you connected through events when your nodes are freed. Meaning that: as you don’t need to call Disconnect on all signals you used Connect on, you don’t need to -= all the signals you used += on.

Custom signals as C# events

To declare a custom event in your C# script, use the [Signal] attribute on a public delegate type. Note that the name of this delegate needs to end with EventHandler.

  1. [Signal]
  2. public delegate void MySignalEventHandler();
  3. [Signal]
  4. public delegate void MySignalWithArgumentEventHandler(string myString);

Once this is done, Godot will create the appropriate events automatically behind the scenes. You can then use said events as you’d do for any other Godot signal. Note that events are named using your delegate’s name minus the final EventHandler part.

  1. public override void _Ready()
  2. {
  3. MySignal += () => GD.Print("Hello!");
  4. MySignalWithArgument += SayHelloTo;
  5. }
  6. private void SayHelloTo(string name)
  7. {
  8. GD.Print($"Hello {name}!");
  9. }

Warning

If you want to connect to these signals in the editor, you will need to (re)build the project to see them appear.

You can click the Build button in the upper-right corner of the editor to do so.

Signal emission

To emit signals, use the EmitSignal method. Note that, as for signals defined by the engine, your custom signal names are listed under the nested SignalName class.

  1. public void MyMethodEmittingSignals()
  2. {
  3. EmitSignal(SignalName.MySignal);
  4. EmitSignal(SignalName.MySignalWithArgument, "World");
  5. }

In contrast with other C# events, you cannot use Invoke to raise events tied to Godot signals.

Signals support arguments of any Variant-compatible type.

Consequently, any Node or Reference will be compatible automatically, but custom data objects will need to inherit from GodotObject or one of its subclasses.

  1. using Godot;
  2. public partial class DataObject : GodotObject
  3. {
  4. public string MyFirstString { get; set; }
  5. public string MySecondString { get; set; }
  6. }

Bound values

Sometimes you’ll want to bind values to a signal when the connection is established, rather than (or in addition to) when the signal is emitted. To do so, you can use an anonymous function like in the following example.

Here, the Button.Pressed signal do not take any argument. But we want to use the same ModifyValue for both the “plus” and “minus” buttons. So we bind the modifier value at the time we’re connecting the signals.

  1. public int Value { get; private set; } = 1;
  2. public override void _Ready()
  3. {
  4. Button plusButton = GetNode<Button>("PlusButton");
  5. plusButton.Pressed += () => ModifyValue(1);
  6. Button minusButton = GetNode<Button>("MinusButton");
  7. minusButton.Pressed += () => ModifyValue(-1);
  8. }
  9. private void ModifyValue(int modifier)
  10. {
  11. Value += modifier;
  12. }

Signal creation at runtime

Finally, you can create custom signals directly while your game is running. Use the AddUserSignal method for that. Be aware that it should be executed before any use of said signals (either connecting to them or emitting them). Also, note that signals created this way won’t be visible through the SignalName nested class.

  1. public override void _Ready()
  2. {
  3. AddUserSignal("MyCustomSignal");
  4. EmitSignal("MyCustomSignal");
  5. }