@@ -6,8 +6,14 @@ C# signals
66For a detailed explanation of signals in general, see the :ref: `doc_signals ` section in the step
77by step tutorial.
88
9- While it is still possible to use signals through the ``Connect ``/``Disconnect `` API, C# gives us
10- a more idiomatic way to implement the :ref: `observer pattern<doc_key_concepts_signals> `.
9+ Signals are implemented using C# events, the idiomatic way to represent
10+ :ref: `the observer pattern<doc_key_concepts_signals> ` in C#. This is the
11+ recommended way to use signals in C# and the focus of this page.
12+
13+ In some cases it's necessary to use the older
14+ :ref: `Connect()<class_object_method_connect> ` and
15+ :ref: `Disconnect()<class_object_method_disconnect> ` APIs.
16+ See :ref: `using_connect_and_disconnect ` for more details.
1117
1218Signals as C# events
1319--------------------
@@ -30,8 +36,11 @@ In addition, you can always access signal names associated with a node type thro
3036 .. warning ::
3137
3238 While all engine signals connected as events are automatically disconnected when nodes are freed, custom
33- signals aren't. Meaning that: you will need to manually disconnect (using ``-= ``) all the custom signals you
34- connected as C# events (using ``+= ``).
39+ signals connected using ``+= `` aren't. This means you will need to manually disconnect (using ``-= ``)
40+ all the custom signals you connected as C# events (using ``+= ``).
41+
42+ An alternative to manually disconnecting using ``-= `` is to
43+ :ref: `use Connect <using_connect_and_disconnect >` rather than ``+= ``.
3544
3645Custom signals as C# events
3746---------------------------
@@ -146,3 +155,32 @@ connecting to them or emitting them). Also, note that signals created this way w
146155 AddUserSignal (" MyCustomSignal" );
147156 EmitSignal (" MyCustomSignal" );
148157 }
158+
159+ .. _using_connect_and_disconnect :
160+
161+ Using Connect and Disconnect
162+ ----------------------------
163+
164+ In general, it isn't recommended to use
165+ :ref: `Connect()<class_object_method_connect> ` and
166+ :ref: `Disconnect()<class_object_method_disconnect> `. These APIs don't provide as
167+ much type safety as the events. However, they're necessary for
168+ :ref: `connecting to signals defined by GDScript <connecting_to_signals_cross_language >`
169+ and passing :ref: `ConnectFlags<enum_Object_ConnectFlags> `.
170+
171+ In the following example, pressing the button for the first time prints
172+ ``Greetings! ``. ``OneShot `` disconnects the signal, so pressing the button again
173+ does nothing.
174+
175+ .. code-block :: csharp
176+
177+ public override void _Ready ()
178+ {
179+ Button button = GetNode <Button >(" GreetButton" );
180+ button .Connect (Button .SignalName .Pressed , Callable .From (OnButtonPressed ), (uint )GodotObject .ConnectFlags .OneShot );
181+ }
182+
183+ public void OnButtonPressed ()
184+ {
185+ GD .Print (" Greetings!" );
186+ }
0 commit comments