Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by SwatHound, Mar 19, 2021.

  1. SwatHound

    SwatHound

    Joined:
    Jun 7, 2018
    Posts:
    14
    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.
     
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I think we'd need to see the system you have in place currently to really understand what you're thinking of.
     
    pantang likes this.
  3. pantang

    pantang

    Joined:
    Sep 1, 2016
    Posts:
    219
    ^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.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,888
    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.
     
    Suddoha likes this.
  5. SwatHound

    SwatHound

    Joined:
    Jun 7, 2018
    Posts:
    14
    Thank you all for your help. My crummy looking code is a bit of an embarrassment but.

    Code (CSharp):
    1. public void SaveGame()
    2.     {
    3.         Debug.Log(">> I'M GOING TO SAVE SOME DATA!" + "\n");
    4.  
    5.         FriendLvl = XPScript.Level;
    6.         FriendCurrentXP = ((int)XPScript.XP);
    7.         FriendMaxXP = ((int)XPScript.MaxXP);
    8.  
    9.  
    10.         myFile.Add("Friendlvl", FriendLvl);
    11.         myFile.Add("FriendCurrentXP", FriendCurrentXP);
    12.         myFile.Add("FriendMaxXP", FriendMaxXP);
    13.  
    14.         myFile.Save();
    15.     }

    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.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    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://forum.unity.com/threads/save-system-questions.930366/#post-6087384

    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