Handling Signal Subscription When Re-adding Nodes to the Scene in Godot 4.3 with C#

Godot Version

4.3

Question

I’m facing a recurring issue in my Godot 4.3 project using C#. Often, I remove nodes from the scene tree (using RemoveChild(myNode)) with the intention of re-adding them later, such as for an NPC. However, I’ve encountered a problem with signal subscriptions and the _Ready() method.

In the _ExitTree() method, I unsubscribe from signals, but _Ready() only gets called once when the node is first added to the tree. If I move the signal subscriptions to the _EnterTree() method, the problem is that at first, certain subnodes (e.g., an AnimationTree node within the NPC scene) are not available yet, as they are initialized in the _Ready() method. This causes issues when subscribing to signals from those subnodes.

How do you typically handle this situation?

Here’s a simplified example of my current code:

public override void _Ready()
{
    base._Ready();

    InteractArea = GetNode<InteractArea>("InteractArea");

    // _Ready is not called again when node is re-added
    // but this lines are not possible within an _EnterTree() for e.g.
    InteractArea.BodyEntered += OnBodyEntered;
    InteractArea.BodyExited += OnBodyExited;
}

public override void _ExitTree()
{
    base._ExitTree();
    InteractArea.BodyEntered -= OnBodyEntered;
    InteractArea.BodyExited -= OnBodyExited;
}

I would appreciate any insights or best practices to ensure smooth signal handling when nodes are removed and re-added to the scene.

you can await for signal ready.

		await ToSignal(TargetNode, SignalName.Ready);

but remember is called only once in other nodes too, you can check if is ready first.
IsNodeReady();

you can try also
on exit tree RequestReady(); will make IsNodeReady(); false and next time when enter tree he will call _Ready()