From 52414e8581b0820358abc1c8a036846346dbacf0 Mon Sep 17 00:00:00 2001 From: 31 <31eee384@gmail.com> Date: Sat, 27 Jan 2024 14:04:21 -0800 Subject: [PATCH] Add C# signal automatic disconnection info --- .../scripting/c_sharp/c_sharp_signals.rst | 177 +++++++++++++++++- 1 file changed, 168 insertions(+), 9 deletions(-) diff --git a/tutorials/scripting/c_sharp/c_sharp_signals.rst b/tutorials/scripting/c_sharp/c_sharp_signals.rst index 8138fe1fa50..50a890f7c6c 100644 --- a/tutorials/scripting/c_sharp/c_sharp_signals.rst +++ b/tutorials/scripting/c_sharp/c_sharp_signals.rst @@ -15,6 +15,10 @@ In some cases it's necessary to use the older :ref:`Disconnect()` APIs. See :ref:`using_connect_and_disconnect` for more details. +If you encounter a ``System.ObjectDisposedException`` while handling a signal, +you might be missing a signal disconnection. See +:ref:`disconnecting_automatically_when_the_receiver_is_freed` for more details. + Signals as C# events -------------------- @@ -33,15 +37,6 @@ In addition, you can always access signal names associated with a node type thro await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame); -.. warning:: - - While all engine signals connected as events are automatically disconnected when nodes are freed, custom - signals connected using ``+=`` aren't. This means you will need to manually disconnect (using ``-=``) - all the custom signals you connected as C# events (using ``+=``). - - An alternative to manually disconnecting using ``-=`` is to - :ref:`use Connect ` rather than ``+=``. - Custom signals as C# events --------------------------- @@ -184,3 +179,167 @@ does nothing. { GD.Print("Greetings!"); } + +.. _disconnecting_automatically_when_the_receiver_is_freed: + +Disconnecting automatically when the receiver is freed +------------------------------------------------------ + +Normally, when any ``GodotObject`` is freed (such as any ``Node``), Godot +automatically disconnects all connections associated with that object. This +happens for both signal emitters and signal receivers. + +For example, a node with this code will print "Hello!" when the button is +pressed, then free itself. Freeing the node disconnects the signal, so pressing +the button again doesn't do anything: + +.. code-block:: csharp + + public override void _Ready() + { + Button myButton = GetNode