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?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO; //Using statement for system processes. (such as creating files)
using Newtonsoft.Json; //Using statement for newtonsoft serializer.
public class SaveControlScript : MonoBehaviour
{
[Header("Save File")]
public int selectedSaveFile; //An integer value for the save file we are saving into with the string version of the Json data.
public string[] statsJSON; //The json string for all of the saved values. (array to be used for multiple save slots)
[Header("Saved Dictionaries")]
public Dictionary <string, Stats> savedStats; //A dictionary for the saved player stats. It has a key value and a returned value.
public void Save() //Function saves all stats to a json string, which it then writes to a json file.
{
//Serialize data to Json.
WriteSaveIntoJSON(); //Write the json string into the json file.
}
public void LoadGame() //Function loads save data from json file, and writes that data to our current values when asked.
{
LoadSaveFromJSON(); //Load the save data from the json file.
//Deserialize data from Json.
}
public void DeleteGame() //Function to delete a save file. Will only run if the file exists.
{
if (File.Exists (Application.persistentDataPath + @"\" + "SaveFile" + selectedSaveFile + ".json"))
{
File.Delete (Application.persistentDataPath + @"\" + "SaveFile" + selectedSaveFile + ".json");
}
}
public void NewGame() //Function for starting a new game with base stats.
{
//Reset game data to default.
}
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.
{
if (System.IO.File.Exists (Application.persistentDataPath + @"\" + "SaveFile" + selectedSaveFile + ".json"))
{
System.IO.File.WriteAllText(Application.persistentDataPath + @"\" + "SaveFile" + selectedSaveFile + ".json", statsJSON[selectedSaveFile]);
}else
{
System.IO.File.Create (Application.persistentDataPath + @"\" + "SaveFile" + selectedSaveFile + ".json").Dispose(); //Dispose = Dispose of path.
System.IO.File.WriteAllText(Application.persistentDataPath + @"\" + "SaveFile" + selectedSaveFile + ".json", statsJSON[selectedSaveFile]);
}
}
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.
{
if (System.IO.File.Exists (Application.persistentDataPath + @"\" + "SaveFile" + selectedSaveFile + ".json"))
{
statsJSON[selectedSaveFile] = System.IO.File.ReadAllText((Application.persistentDataPath + @"\" + "SaveFile" + selectedSaveFile + ".json"));
}else
{
return;
}
}
}