Save game removes objects which don't exist in the save file

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

Hi!

I’m following the godot saving games article over on the godot docs, and I have a problem with the save system that I don’t know how to fix. Basically I’m putting all objects that I want to save in a group, writing a save function for each object that returns a dictionary, finding all objects that are in the group, converting them to JSON and saving them serially in a file. When I load the game I convert each line in the save file to JSON, load the objects by type and add the new object as a child of the parent (defined in the save method).

The problem is that if I add an object to the scene when I already have a save file it will be removed. Is there a way to fix this? The code is basically the one on the docs but with some small modifications that aren’t necessary to solve this problem.

Thanks!

Not sure, if I understand your problem correctly. Creating and loading a save file works? But if you create new objects after loading it, these objects are “removed”? By what? When? Why? Or do you simply mean, they aren’t written to the save file?

The code is basically the one on the docs but with some small modifications that aren’t necessary to solve this problem.

How do you know this, if you haven’t solved the problem yet? :wink: Honestly, it just sounds like you forgot to add the newly created objects to the “Persist”-group.

njamster | 2020-03-19 14:47

Sorry if my question wasn’t clear enough. Creating and loading works fine. The problem is that the code removes all objects in the group “persist” and then loads the ones that are in the save file, to prevent duplicates. That’s where the problem is. The new objects (which are in the “persist” group) get deleted, and since they aren’t in the save file they won’t be replaced by another object in the same location. This is the problem that I’m trying to solve, but I can’t come up with a valid solution.

Freeranger Studios | 2020-03-19 16:31

Sorry, I still don’t get it. :frowning: You load from your save-file, create some additional nodes, add them to the “Persist”-group and then…? If you save again afterwards, they should be added to the save-file. But you claim they are not: “and since they aren’t in the save file”. So the only logical conclusion would be, that you haven’t saved again. But in this case, I don’t understand, why you expect them to be restored in the first place?

njamster | 2020-03-19 18:51

Thanks for trying to help, the problem was that in order to prevent duplicates when the game started (objects created from save file and the already existing objects) I removed all objects in the “persistent” group. Instead of removing all of them, I gave each object an ID, and if 2 objects had the same ID, the initial one was removed. Thanks for your help though!

Freeranger Studios | 2020-03-22 11:28