Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update C# signal documentation #6110

Merged
merged 1 commit into from
Aug 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion getting_started/first_2d_game/03.coding_the_player.rst
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ Add the following at the top of the script. If you're using GDScript, add it aft
// Don't forget to rebuild the project so the editor knows about the new signal.

[Signal]
public delegate void Hit();
public delegate void HitEventHandler();

.. code-tab:: cpp

Expand Down
4 changes: 2 additions & 2 deletions getting_started/first_2d_game/06.heads_up_display.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Now add this script to ``HUD``:
// Don't forget to rebuild the project so the editor knows about the new signal.

[Signal]
public delegate void StartGame();
public delegate void StartGameEventHandler();
}

.. code-tab:: cpp
Expand Down Expand Up @@ -302,7 +302,7 @@ signal of ``StartButton`` and add the following code to the new functions:
public void OnStartButtonPressed()
{
GetNode<Button>("StartButton").Hide();
EmitSignal("StartGame");
EmitSignal(nameof(StartGame));
}

public void OnMessageTimerTimeout()
Expand Down
2 changes: 1 addition & 1 deletion getting_started/first_3d_game/06.jump_and_squash.rst
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ destroy the mob.

// Emitted when the played jumped on the mob.
[Signal]
public delegate void Squashed();
public delegate void SquashedEventHandler();

// ...

Expand Down
6 changes: 3 additions & 3 deletions getting_started/first_3d_game/07.killing_player.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ a ``die()`` function that helps us put a descriptive label on the code.

// Emitted when the player was hit by a mob.
[Signal]
public delegate void Hit();
public delegate void HitEventHandler();

// ...

Expand Down Expand Up @@ -268,7 +268,7 @@ Next is ``Mob.gd``.
{
// Emitted when the played jumped on the mob.
[Signal]
public delegate void Squashed();
public delegate void SquashedEventHandler();

// Minimum speed of the mob in meters per second
[Export]
Expand Down Expand Up @@ -377,7 +377,7 @@ Finally, the longest script, ``Player.gd``.
{
// Emitted when the player was hit by a mob.
[Signal]
public delegate void Hit();
public delegate void HitEventHandler();

// How fast the player moves in meters per second.
[Export]
Expand Down
4 changes: 2 additions & 2 deletions getting_started/first_3d_game/08.score_and_replay.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ line.
{
// ...
// We connect the mob to the score label to update the score upon squashing one.
mob.Connect(nameof(Mob.Squashed), GetNode<ScoreLabel>("UserInterface/ScoreLabel"), nameof(ScoreLabel.OnMobSquashed));
mob.Squashed += GetNode<ScoreLabel>("UserInterface/ScoreLabel").OnMobSquashed;
}

This line means that when the mob emits the ``squashed`` signal, the
Expand Down Expand Up @@ -438,7 +438,7 @@ Here is the complete ``Main.gd`` script for reference.
mob.Initialize(mobSpawnLocation.Translation, playerPosition);

AddChild(mob);
mob.Connect(nameof(Mob.Squashed), GetNode<ScoreLabel>("UserInterface/ScoreLabel"), nameof(ScoreLabel.OnMobSquashed));
mob.Squashed += GetNode<ScoreLabel>("UserInterface/ScoreLabel").OnMobSquashed;
}

public void OnPlayerHit()
Expand Down
4 changes: 2 additions & 2 deletions getting_started/first_3d_game/09.adding_animations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ Here's the *Player* script.
{
// Emitted when the player was hit by a mob.
[Signal]
public delegate void Hit();
public delegate void HitEventHandler();

// How fast the player moves in meters per second.
[Export]
Expand Down Expand Up @@ -491,7 +491,7 @@ And the *Mob*'s script.
{
// Emitted when the played jumped on the mob.
[Signal]
public delegate void Squashed();
public delegate void SquashedEventHandler();

// Minimum speed of the mob in meters per second
[Export]
Expand Down
4 changes: 2 additions & 2 deletions getting_started/step_by_step/signals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ reaches 0.
public class CustomSignal : Node2D
{
[Signal]
public delegate void HealthDepleted();
public delegate void HealthDepletedEventHandler();

private int Health = 10;
}
Expand Down Expand Up @@ -480,7 +480,7 @@ names between parentheses:
public class CustomSignal : Node
{
[Signal]
public delegate void HealthChanged(int oldValue, int newValue);
public delegate void HealthChangedEventHandler(int oldValue, int newValue);

private int Health = 10;
}
Expand Down
8 changes: 6 additions & 2 deletions tutorials/best_practices/godot_notifications.rst
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,15 @@ nodes that one might create at runtime.
case NOTIFICATION_PARENTED:
ParentCache = GetParent();
if (ConnectionCheck())
ParentCache.Connect("InteractedWith", this, "OnParentInteractedWith");
{
ParentCache.Connect("InteractedWith", OnParentInteractedWith);
}
break;
case NOTIFICATION_UNPARENTED:
if (ConnectionCheck())
ParentCache.Disconnect("InteractedWith", this, "OnParentInteractedWith");
{
ParentCache.Disconnect("InteractedWith", OnParentInteractedWith);
}
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions tutorials/networking/http_request_class.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ Below is all the code we need to make it work. The URL points to an online API m
{
public override void _Ready()
{
GetNode("HTTPRequest").Connect("request_completed", this, "OnRequestCompleted");
GetNode("Button").Connect("pressed", this, "OnButtonPressed");
GetNode("HTTPRequest").RequestCompleted += OnRequestCompleted;
GetNode("Button").Pressed += OnButtonPressed;
}

public void OnButtonPressed()
Expand Down
2 changes: 1 addition & 1 deletion tutorials/plugins/editor/making_plugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ clicked. For that, we'll need a script that extends from
{
public override void _EnterTree()
{
Connect("pressed", Clicked);
Pressed += Clicked;
}

public void Clicked()
Expand Down
5 changes: 3 additions & 2 deletions tutorials/scripting/c_sharp/c_sharp_differences.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,12 @@ Signal keyword

Use the ``[Signal]`` attribute to declare a signal instead of the GDScript ``signal`` keyword.
This attribute should be used on a `delegate`, whose name signature will be used to define the signal.
The `delegate` must have the ``EventHandler`` suffix, an `event` will be generated in the class with the same name but without the suffix, use that event's name with ``EmitSignal``.

.. code-block:: csharp

[Signal]
delegate void MySignal(string willSendsAString);
delegate void MySignalEventHandler(string willSendAString);

See also: :ref:`doc_c_sharp_signals`.

Expand Down Expand Up @@ -171,7 +172,7 @@ Example:

.. code-block:: csharp

Input.Singleton.Connect("joy_connection_changed", this, nameof(Input_JoyConnectionChanged));
Input.Singleton.JoyConnectionChanged += Input_JoyConnectionChanged;

String
------
Expand Down
16 changes: 8 additions & 8 deletions tutorials/scripting/c_sharp/c_sharp_features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,17 @@ For more advanced type checking, you can look into `Pattern Matching <https://do
C# signals
----------

For a complete C# example, see the **Handling a signal** section in the step by step :ref:`doc_scripting` tutorial.
For a complete C# example, see the :ref:`doc_signals` section in the step by step tutorial.

Declaring a signal in C# is done with the ``[Signal]`` attribute on a delegate.

.. code-block:: csharp

[Signal]
delegate void MySignal();
delegate void MySignalEventHandler();

[Signal]
delegate void MySignalWithArguments(string foo, int bar);
delegate void MySignalWithArgumentsEventHandler(string foo, int bar);

These signals can then be connected either in the editor or from code with ``Connect``.
If you want to connect a signal in the editor, you need to (re)build the project assemblies to see the new signal. This build can be manually triggered by clicking the "Build" button at the top right corner of the editor window.
Expand All @@ -112,8 +112,8 @@ If you want to connect a signal in the editor, you need to (re)build the project

public void SomeFunction()
{
instance.Connect("MySignal", this, "MyCallback");
instance.Connect(nameof(MySignalWithArguments), this, "MyCallbackWithArguments");
instance.MySignal += MyCallback;
instance.MySignalWithArguments += MyCallbackWithArguments;
}

Emitting signals is done with the ``EmitSignal`` method.
Expand All @@ -123,7 +123,7 @@ Emitting signals is done with the ``EmitSignal`` method.
public void SomeFunction()
{
EmitSignal(nameof(MySignal));
EmitSignal("MySignalWithArguments", "hello there", 28);
EmitSignal(nameof(MySignalWithArguments), "hello there", 28);
}

Notice that you can always reference a signal name with the ``nameof`` keyword (applied on the delegate itself).
Expand All @@ -144,8 +144,8 @@ It is possible to bind values when establishing a connection by passing a Godot
var plusButton = (Button)GetNode("PlusButton");
var minusButton = (Button)GetNode("MinusButton");

plusButton.Connect("pressed", this, "ModifyValue", new Godot.Collections.Array { 1 });
minusButton.Connect("pressed", this, "ModifyValue", new Godot.Collections.Array { -1 });
plusButton.Pressed += () => ModifyValue(1);
minusButton.Pressed += () => ModifyValue(-1);
}

Signals support parameters and bound values of all the `built-in types <https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/built-in-types-table>`_ and Classes derived from :ref:`Godot.Object <class_Object>`.
Expand Down
2 changes: 1 addition & 1 deletion tutorials/scripting/instancing_with_signals.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Here is the code for the player using signals to emit the bullet:
public class Player : Sprite2D
{
[Signal]
delegate void Shoot(PackedScene bullet, Vector2 direction, Vector2 location);
delegate void ShootEventHandler(PackedScene bullet, Vector2 direction, Vector2 location);

private PackedScene _bullet = GD.Load<PackedScene>("res://Bullet.tscn");

Expand Down