Save system: How can I make sure that parent nodes don't lose their references to their children?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Whom

I’m following this tutorial: Saving games — Godot Engine (3.5) documentation in English

Let’s say I have a node called Enemy with a variable “health”. I save the node’s file name, parent and the health variable etc. just like in the tutorial.

However, I run into problems after I load the save file. The original node is replaced by a new node that has the health value I saved (again, just like in the tutorial). The problem is that the parent still references the old node that does no longer exist: onready var enemy = $Enemy so I can’t access the new Enemy node from parent. When I try to call a function of the Enemy node, I get this error: Attempt to call function ‘[function]’ in base ‘previously freed instance’ on a null instance.

What is the simplest way to fix this?

:bust_in_silhouette: Reply From: Whom

I ended up solving this by not creating new nodes when I load a save file. Instead, I save node path and then when loading the save, I find the original node by its path and set its variables. I don’t know yet if this is the best solution but it’s good enough for now. This is mainly for scenes that will always have the same node tree structure.