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