How do I save multiple pieces of data, without copying/pasting my code and renaming slightly?

Okay. I got my save/load system working. My issue is, is there a better way for me to be doing this?

Lets say I’m saving a bunch of data. All of this data is tied to FACTION_A.

Right now, being as lost as I am, I’m simply copying pasting all of the code used to save FACTION_A’s data, and slightly renaming it to match FACTION_B. Its a lot of copy pasting and I intend to have more factions.

is there perhaps a technique, something along the lines of “For every faction… save XYZ” ?

Is this something a “dictionary” or “list” can help me with? I’ve never used either one.

I think we’d need to see the system you have in place currently to really understand what you’re thinking of.

1 Like

^this, JSP can be a good start to data/program structures though. could probably have a int and a byte array in a class, then just serialise, deserialise depending on the int? but without seeing anything, stab in the dark.

First place to start is to create a Faction class that stores all the information relevant to a single faction. Have you done this step yet? It sounds like maybe you have a “FactionA” class and are thinking about making a “FactionB” class. That would be the wrong approach. Just create a general “Faction” class and give it properties such as “FactionName” and whatever else you need.

After that, any code that processes a faction should be able to accept any Faction object you give it. Then it is easy to handle multiple factions in your game.

1 Like

Thank you all for your help. My crummy looking code is a bit of an embarrassment but.

public void SaveGame()
    {
        Debug.Log(">> I'M GOING TO SAVE SOME DATA!" + "\n");

        FriendLvl = XPScript.Level;
        FriendCurrentXP = ((int)XPScript.XP);
        FriendMaxXP = ((int)XPScript.MaxXP);


        myFile.Add("Friendlvl", FriendLvl);
        myFile.Add("FriendCurrentXP", FriendCurrentXP);
        myFile.Add("FriendMaxXP", FriendMaxXP);

        myFile.Save();
    }

This works all fine and dandy when I’m saving/loading a single “friend” but in the spirit of “wanting to do it properly” I came here.

Someone suggested I try to implement the data into a serializable scriptable object but that made me even more lost.

Not so much scriptable object (at least for savings) but rather serialize it with JSON.

Look into JSON, work through some tutorials… basically its just a way to turn data into a string, and back into the original data, and that’s the essence of savegames.

While we’re at it, here’s a few more of my mad ramblings about loading/saving:

Load/Save steps:

https://discussions.unity.com/t/799896/4

Don’t use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide