فهرست منبع

Update C# signal documentation

Updates C# signal documentation and code examples to the new API in 4.0
Raul Santos 2 سال پیش
والد
کامیت
3b36a94d35

+ 1 - 1
getting_started/first_2d_game/03.coding_the_player.rst

@@ -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
 

+ 2 - 2
getting_started/first_2d_game/06.heads_up_display.rst

@@ -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
@@ -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()

+ 1 - 1
getting_started/first_3d_game/06.jump_and_squash.rst

@@ -325,7 +325,7 @@ destroy the mob.
 
     // Emitted when the played jumped on the mob.
     [Signal]
-    public delegate void Squashed();
+    public delegate void SquashedEventHandler();
 
     // ...
 

+ 3 - 3
getting_started/first_3d_game/07.killing_player.rst

@@ -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();
 
     // ...
 
@@ -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]
@@ -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]

+ 2 - 2
getting_started/first_3d_game/08.score_and_replay.rst

@@ -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
@@ -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()

+ 2 - 2
getting_started/first_3d_game/09.adding_animations.rst

@@ -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]
@@ -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]

+ 2 - 2
getting_started/step_by_step/signals.rst

@@ -426,7 +426,7 @@ reaches 0.
     public class CustomSignal : Node2D
     {
         [Signal]
-        public delegate void HealthDepleted();
+        public delegate void HealthDepletedEventHandler();
 
         private int Health = 10;
     }
@@ -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;
     }

+ 6 - 2
tutorials/best_practices/godot_notifications.rst

@@ -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;
             }
         }

+ 2 - 2
tutorials/networking/http_request_class.rst

@@ -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()

+ 1 - 1
tutorials/plugins/editor/making_plugins.rst

@@ -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()

+ 3 - 2
tutorials/scripting/c_sharp/c_sharp_differences.rst

@@ -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`.
 
@@ -171,7 +172,7 @@ Example:
 
 .. code-block:: csharp
 
-    Input.Singleton.Connect("joy_connection_changed", this, nameof(Input_JoyConnectionChanged));
+    Input.Singleton.JoyConnectionChanged += Input_JoyConnectionChanged;
 
 String
 ------

+ 8 - 8
tutorials/scripting/c_sharp/c_sharp_features.rst

@@ -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.
@@ -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.
@@ -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).
@@ -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>`.

+ 1 - 1
tutorials/scripting/instancing_with_signals.rst

@@ -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");