Godot Version
godot-4
Question
I‘m a beginner. I read an archive at runtime, and tried to replace the old node, but it didn’t work, the node still has the old value.
public static void Load()
{
FileAccess saveFile = IoUtil.Read();
SceneTree sceneTree = (SceneTree)Engine.GetMainLoop();
sceneTree.Paused = true;
var saveNodes = sceneTree.GetNodesInGroup("Save");
while (saveFile.GetPosition() < saveFile.GetLength())
{
var jsonString = saveFile.GetLine();
var json = new Json();
var parseResult = json.Parse(jsonString);
if (parseResult != Error.Ok)
{
continue;
}
var nodeData = new Dictionary<string, Variant>((Dictionary)json.Data);
var newObjectScene = GD.Load<PackedScene>(nodeData["Filename"].ToString());
var newObject = newObjectScene.Instantiate();
foreach (Node saveNode in saveNodes)
{
if (saveNode.SceneFilePath == nodeData["Filename"].ToString())
{
foreach (var (key, value) in nodeData)
{
if (key == "Filename" || key == "Parent" || key == "Path" )
{
continue;
}
newObject.Set(key, value);
}
Node parent = saveNode.GetParent();
int index = saveNode.GetIndex();
parent.RemoveChild(saveNode);
saveNode.QueueFree();
parent.AddChild(newObject);
// parent.CallDeferred(Node.MethodName.AddChild, newObject);
parent.MoveChild(newObject,index);
}
}
}
}