2
0
Эх сурвалжийг харах

Update signals documentation in C#

Paul Joannon 7 жил өмнө
parent
commit
be134b058a

+ 11 - 0
getting_started/scripting/c_sharp/c_sharp_differences.rst

@@ -40,6 +40,17 @@ Export keyword
 
 Use the ``[Export]`` attribute instead of the GDScript ``export`` keyword.
 
+Signal keyword
+--------------
+
+Use the ``[Signal]`` attribute instead of the GDScript ``signal`` keyword.
+This attribute should be used on a `delegate`, whose name signature will be used to define the signal.
+
+.. code-block:: csharp
+
+    [Signal]
+    delegate void MySignal(string willSendsAString);
+
 Singletons
 ----------
 

+ 43 - 21
getting_started/scripting/c_sharp/c_sharp_features.rst

@@ -62,32 +62,54 @@ Signals
 
 For a complete C# example, see the **Handling a signal** section in the step by step :ref:`doc_scripting` tutorial.
 
-You can use ``Connect("SomeSignal", someObject, "SomeMethod")`` to connect to signals.
-``AddUserSignal("SignalName")`` is used to define custom signals.
-Emitting signals is done with ``EmitSignal("SignalName")``. Params can be given, like ``EmitSignal("SignalName", arg1, arg2, ...)``.
+Declaring a signal in C# is done with the ``[Signal]`` attribute on a delegate.
 
-**Custom signals**
+.. code-block:: csharp
+
+    [Signal]
+    delegate void MySignal();
+
+    [Signal]
+    delegate void MySignalWithArguments(string foo, int bar);
+
+These signals can then be connected either in the editor or from code with ``Connect``.
 
 .. code-block:: csharp
-   :emphasize-lines: 5, 10
-   :linenos:
 
-    public class ExampleScript : Node
+    public void MyCallback()
     {
-        public override void _Ready()
-        {
-            AddUserSignal("YourSignal");
-        }
-
-        public override void _Process(float delta)
-        {
-            EmitSignal("YourSignal");
-        }
+        GD.Print("My callback!");
     }
 
-Above in line 5, ``AddUserSignal()`` is used to define the new custom signal ``YourSignal``.
-In line 10 ``EmitSignal()`` is used to emit that custom signal on every frame in ``_Process()``.
+    public void MyCallbackWithArguments(string foo, int bar)
+    {
+        GD.Print("My callback with: ", foo, " and ", bar, "!");
+    }
 
-Make sure that ``AddUserSignal()`` is always executed before any calls using that signal (``EmitSignal()`` and ``Connect()``).
-If you are using both ``AddUserSignal()`` and ``Connect()`` or ``EmitSignal()`` in ``_Ready()``, this is especially important as load order of your node may change,
-and thus the order in which your various ``_Ready()`` functions are called.
+    public void SomeFunction()
+    {
+        instance.Connect("MySignal", this, "MyCallback");
+        instance.Connect(nameof(MySignalWithArguments), this, "MyCallbackWithArguments");
+    }
+
+Emitting signals is done with the ``EmitSignal`` method.
+
+.. code-block:: csharp
+
+    public void SomeFunction()
+    {
+        EmitSignal(nameof(MySignal));
+        EmitSignal("MySignalWithArguments", "hello there", 28);
+    }
+
+Notice that you can always reference a signal name with the ``nameof`` keyword (applied on the delegate itself).
+
+Finally, signals can be created by calling ``AddUserSignal``, but be aware that it should be executed before any use of said signals (with ``Connect`` or ``EmitSignal``).
+
+.. code-block:: csharp
+
+    public void SomeFunction()
+    {
+        AddUserSignal("MyOtherSignal");
+        EmitSignal("MyOtherSignal");
+    }