Multiple scenes save data file

So I have a test project, where I have a Crate A in one scene, and a Crate B and C in the other. Whenever I save to the .json file, the data for the objects that are not present in the current scene get overwritten to zero.

So I would like that, when I save, no matter the scene, the data parameters of the saved objects don’t get overwritten to zero, cause they’re not in the same scene. If anyone has any insight how to achieve that, please go ahead… I would be more than thankful. Thanks.

If I move Crate A in scene 1… then Switch to scene 2 , how do you know where to load Crate A when I go back to Scene 1. Usually if you load a Scene you destroy all the gameObjects. So you must be saving the Crate A data somewhere?

Whereever your storing this data, if your Saving in Scene 1 you need to add the saved off B/C crate data to your Scene 1 save /load Same Goes for Scene 2. Whereever the stored data is for crate A, you need to add it to the Scene 2 load/save

I am saving the data to computer, to LocalLow folder in C: (I’m using Windows 10), there is a .json file in there, and that .json file keeps track of all the objects, from all the scenes, it’s just… it overwrites the data of those that are not present in the scene to zero, when it’s writing the new data of the objects from the other scene… It seems it’s hard for the people to grasp what’s going on here, and that’s why I’ve put all the scripts, and even the project, so you can check it out on your own. I apologize if I’m not understanding you, and you actually gave a solution… but, hit me up again :).

Well, first off if you’re using the same save utility in multiple scenes, you’d be best off with a centralized database for the data that needs to be saved. I’ll be responding using that premise, rather than the premise of doing this on a scene-by-scene basis, but the solution I suggest can be adapted to suit pretty much any situation.

As an example, you can have a Dictionary in a static class (or singleton on a DontDestroyOnLoad object / additive scene that’s never unloaded). This dictionary database would likely keep track of objects by ID (either string identifiers like “Box001” or random numerical IDs), or possibly a combination of scene ID and object ID if you want to make scene distinctions, and would track the data to be saved for the objects with those IDs in the VALUE column. This data would likely be in the form of a class you make for storing information, like SaveData in your posted example.

Dictionary<string, SaveData> database = new Dictionary<string, SaveData>();

In many solutions I’ve seen, the process of selecting which objects to save and which information to save on those objects is accomplished using a “Save Identifier” script/component, attached only on the objects in each scene that you want to keep track of the data for. You’d be able to set the IDs right there in the component once its attached to the object, and often be able to select some kind of dropdown (enum) value, or series of boolean values, for the kinds of data that need to be saved for it. Position and Rotation seem to be the ones you’re focused on in your setup up to now, so you’d have an enum option for “transform data”. When the object with that identifier is loaded in the scene, as part of its Awake process it would reach out to your database and register itself in a list of “objects to keep track of in the scene”.

Back on the database controller, you would likely load all of the data in the whole database all at once when the game is loaded / new game is started / program is launched- whenever is appropriate for your game. It would convert the data back into a usable form and place it into the Dictionary. If you need a solution for serializing Dictionaries to make them compatible with your JSON utility, there’s a good one that’s been active in the past few days in this forum, near the top of the topics list. This would serialize out to JSON and back perfectly fine, I think, and could store the database directly.

Even if you don’t use that though, you can just convert the database into a list of custom class objects that contain the database key as one of its fields along with the rest of the data.

[System.Serializable]
public class SaveDataContainer
{
    public string id;
    public SaveData saveData;

    public SaveDataContainer(string id, SaveData saveData)
    {
        this.id = id;
        this.saveData = saveData;
    }
}

When a scene starts, the database controller can be made to LoadAllFromDatabase (or something), which would contact all of the objects in its “objects to keep track of” list and then tell them to update their information based on what’s been stored (if anything). When SaveAllToDatabase is called, it can call out to the same list and get updates on all of their information, storing it and overwriting existing data for that object in the database. When SaveDatabaseToFile is called, It would then convert the database data into JSON and write it out to the disk, and when LoadDatabaseFromFile is called, it would load the JSON data and convert it back into a dictionary.

Wow, lot of info, I’ll try to look into the stuff you’ve said. But basically, I need to take a look into this as well?

https://unity3d.com/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries

Cause I’ve never to this point used lists or arrays or dictionaries… hence why I couldn’t get the right idea in the first place…

I don’t use the same save utility that you do, and I have no idea how it’s designed. In mine, I can use Tags to check IDs for objects placed into the save file, but that doesn’t seem to be an option for this JSON utility. It seems to me like each of your scenes are independently overwriting the same save file- the WHOLE file, not just the lines that matter to them, and this is leaving you with no saved data for objects that weren’t in the last saved scene.

As far as I know, the only way to be sure to avoid doing this without using a centralized database in your project is, when you go to save, LOAD the save file first, then place it into a list of SaveData objects (call it an “existing data list” of save data). Then, create a second list of SaveData objects for the data in the current scene that you want to save (call it a “new data list”), and then combine the two lists and overwrite any of the entries in the first list with the same IDs with the entries from the second list. Then, when the lists are combined, you can save it out to a file again.

Also, please don’t send messages to people with additional information/questions. First, it puts all of the pressure on a single person to provide an answer, when this is supposed to (ideally) be a collaborative effort. I might be busy with my own project and only able to help at specific times of the day, etc. Second, both the question and the answer need to be publicly available for others who come along at a later date and find the thread- it could help them with the same kind of problem.

The second point might be giving the average user too much credit though, given that they’d have to use a “search” function…

Yup, thanx.