0

In my game on Unity I have save/load mechanism. Here how I save the game:

// class with the all data, which need to be saved
[Serializable]
public class Saves : ScriptableObject
{
    public string dateTime;
    public int latestSaveSlot;
    public int actNumber;
    public string sceneName;
    public int currentActiveSlot;
}

// method in another class, which save data
public void ButtonSave()
{
    latestSaveSlot = currentActiveSlot;

    saves.dateTime = System.DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss");
    saves.latestSaveSlot = latestSaveSlot;
    saves.actNumber = gameManagerScript.currentActNumber;
    saves.sceneName = SceneManager.GetActiveScene().name;

    var serializedSave = JsonUtility.ToJson(saves);
    var saveFileName = Application.persistentDataPath + "/Save_" + currentActiveSlot + ".save";
    File.WriteAllText(saveFileName, serializedSave);
}

And now I need to load it. I have 6 save slots. I need something like this:

currentActNumber = saves.actNumber + currentActiveSlot;

.. to load data from the slot I need, but without editing value of the variable.

6 save slots: enter image description here

enter image description here

Amazing User
  • 3,473
  • 10
  • 36
  • 75
  • 3
    what do you mean by *without editing value of the variable*? – Timothy Groote Oct 30 '17 at 11:56
  • your example also appears incomplete. from what i gather in your source code, i would expect an array or collection of `Saves` objects (is that what your `saves` variable is?) – Timothy Groote Oct 30 '17 at 11:58
  • @TimothyGroote In my previous version of this code I use PlayerPrefs to set: `PlayerPrefs.SetInt("act number" + currentActiveSlot, gameManagerScript.currentActNumber);` and get `currentActNumber = PlayerPrefs.GetInt("act number" + currentActiveSlot);` value of the variable. But I can't do it in my new code like this `currentActNumber = saves.actNumber + currentActiveSlot;` because it will change value of the valiable – Amazing User Oct 30 '17 at 12:00
  • @TimothyGroote `saves` is instance of `Saves` class and I don't need an array because I have a little number of variables – Amazing User Oct 30 '17 at 12:03
  • It looks to me like you are having serious trouble wrapping your head around object oriented programming and thinking (please do not take this as an insult, i's not.) wouldn't it make more sense to have a collection of `savestate` objects that you can serialise from the current state on "save" and deserialise into the current state of the game on "load"? – Timothy Groote Oct 30 '17 at 12:08
  • How can you load something without changing a value? That is the point of loading data...to overwrite the variables that have the default values on game start. – TheSkimek Oct 30 '17 at 12:14
  • @TimothyGroote Not sure I understand what do you mean. It worked fine with `PlayerPrefs` but by some reason instead of it now need to save data to the file. But now when I load it, I can't say to the program, from which save slot I need to load data – Amazing User Oct 30 '17 at 12:14
  • @dima check my answer. – Timothy Groote Oct 30 '17 at 12:15
  • @dima the method i would propose instead of this, is to serialize all the saves into a single file, then deserialize that instead. you could then just switch "save states" in memory, without having to read them from files every time. – Timothy Groote Oct 30 '17 at 12:17
  • I see you've put in an impressive amount of work on that game you're working on. looks good! – Timothy Groote Oct 30 '17 at 12:34
  • @TimothyGroote Thanks – Amazing User Oct 30 '17 at 12:42

1 Answers1

1

based on what you are saying, your load method should look something like this :

public void LoadGame(int slot)
{
    string saveFileName = Application.persistentDataPath + "/Save_" + slot+ ".save";
    string saveFilecontent = File.ReadAllText(saveFileName);
    //please note, i have no idea if this works, because i don't know whay your
    //JsonUtility does. this is an educated guess.
    string deSerializedSave =  JsonUtility.FromJson<Saves>(saveFilecontent);

    saves = deSerializedSave;

    //you can now switch scene, set variables etc. form your "saves" object.

}
Timothy Groote
  • 8,614
  • 26
  • 52
  • Thanks, I tried this code when load the game. But it gives me an error: "Cannot deserialize JSON to new instances of type 'Saves.'". Added image – Amazing User Oct 30 '17 at 12:42
  • 1
    JsonUtility.FromJson [documentation](https://docs.unity3d.com/ScriptReference/JsonUtility.FromJson.html) says that "Only plain classes and structures are supported; classes derived from UnityEngine.Object (such as MonoBehaviour or ScriptableObject) are not." – Mikko Koivisto Oct 30 '17 at 13:08
  • @MikkoKoivisto thanks, I fixed that. And replaced `var` to `string` and `Saves`, the error is gone – Amazing User Oct 30 '17 at 13:17