Search Unity

Feedback Does this saving system have any major flaws?

Discussion in 'Editor & General Support' started by HenryFGames, Feb 21, 2023.

  1. HenryFGames

    HenryFGames

    Joined:
    Aug 6, 2022
    Posts:
    2
    I am working on creating a saving system for my game, and I think that it is going well, but I was wondering if there are any major flaws with this system. I am saving data as Dictionaries, so that I can reference a specific character from all characters, or a specific object in the scene etc. Will there be any clear issues with this?

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using System.IO; //Using statement for system processes. (such as creating files)
    6. using Newtonsoft.Json; //Using statement for newtonsoft serializer.
    7.  
    8. public class SaveControlScript : MonoBehaviour
    9. {
    10.    
    11.     [Header("Save File")]
    12.     public int selectedSaveFile; //An integer value for the save file we are saving into with the string version of the Json data.
    13.     public string[] statsJSON; //The json string for all of the saved values. (array to be used for multiple save slots)
    14.  
    15.     [Header("Saved Dictionaries")]
    16.     public Dictionary <string, Stats> savedStats; //A dictionary for the saved player stats. It has a key value and a returned value.
    17.    
    18.  
    19.     public void Save() //Function saves all stats to a json string, which it then writes to a json file.
    20.     {
    21.      
    22.         //Serialize data to Json.
    23.  
    24.         WriteSaveIntoJSON(); //Write the json string into the json file.
    25.     }
    26.  
    27.     public void LoadGame() //Function loads save data from json file, and writes that data to our current values when asked.
    28.     {
    29.         LoadSaveFromJSON(); //Load the save data from the json file.
    30.         //Deserialize data from Json.
    31.     }
    32.  
    33.     public void DeleteGame() //Function to delete a save file. Will only run if the file exists.
    34.     {
    35.         if (File.Exists (Application.persistentDataPath + @"\" + "SaveFile" + selectedSaveFile + ".json"))
    36.         {
    37.             File.Delete (Application.persistentDataPath + @"\" + "SaveFile" + selectedSaveFile + ".json");
    38.         }
    39.     }
    40.  
    41.     public void NewGame() //Function for starting a new game with base stats.
    42.     {
    43.         //Reset game data to default.
    44.     }
    45.  
    46.     public void WriteSaveIntoJSON() //Function for writing the stats into a json file. If the file exists, write it into the file, otherwise create a file, and then write it in.
    47.     {
    48.         if (System.IO.File.Exists (Application.persistentDataPath + @"\" + "SaveFile" + selectedSaveFile + ".json"))
    49.         {
    50.             System.IO.File.WriteAllText(Application.persistentDataPath + @"\" + "SaveFile" + selectedSaveFile + ".json", statsJSON[selectedSaveFile]);
    51.         }else
    52.         {
    53.             System.IO.File.Create (Application.persistentDataPath + @"\" + "SaveFile" + selectedSaveFile + ".json").Dispose(); //Dispose = Dispose of path.
    54.             System.IO.File.WriteAllText(Application.persistentDataPath + @"\" + "SaveFile" + selectedSaveFile + ".json", statsJSON[selectedSaveFile]);
    55.         }
    56.        
    57.     }
    58.  
    59.     public void LoadSaveFromJSON() //Function for loading the stats from a json file. Checks if the file exists, and if so, loads it, otherwise, it returns.
    60.     {
    61.         if (System.IO.File.Exists (Application.persistentDataPath + @"\" + "SaveFile" + selectedSaveFile + ".json"))
    62.         {
    63.             statsJSON[selectedSaveFile] = System.IO.File.ReadAllText((Application.persistentDataPath + @"\" + "SaveFile" + selectedSaveFile + ".json"));
    64.         }else
    65.         {
    66.             return;
    67.         }
    68.     }
    69. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    There is a TON of repeated code!

    This expression appears (I believe) eight (8) times!

    Something of that complexity should appear in ONE place only.

    This has nothing to do with the save system itself, but my goodness, you should NEVER repeat the same computation again and again and again.

    Otherwise, if it works it works. Here's some more notes:

    Load/Save steps:

    https://forum.unity.com/threads/save-system-questions.930366/#post-6087384

    An excellent discussion of loading/saving in Unity3D by Xarbrough:

    https://forum.unity.com/threads/save-system.1232301/#post-7872586

    Loading/Saving ScriptableObjects by a proxy identifier such as name:

    https://forum.unity.com/threads/use...lds-in-editor-and-build.1327059/#post-8394573

    When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls
    new
    to make one, it cannot make the native engine portion of the object.

    Instead you must first create the MonoBehaviour using AddComponent<T>() on a GameObject instance, or use ScriptableObject.CreateInstance<T>() to make your SO, then use the appropriate JSON "populate object" call to fill in its public fields.

    If you want to use PlayerPrefs to save your game, it's always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:

    https://gist.github.com/kurtdekker/7db0500da01c3eb2a7ac8040198ce7f6

    Do not 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