1

So in our visual novel game using the ink/inkle API for unity, I'm trying to implement a save and load system, but I am currently having trouble with loading the saved state the way I intend to.

Based on the API's documentation, to save the state of your story within your game, you call:

string savedJson = _inkStory.state.ToJson();

then to load it:

_inkStory.state.LoadJson(savedJson);

Here is the how I save the current state of the story using PlayerPrefs, which is working just fine.

public void saveGame()
{

    DialogueSystem dialogueSystem = GetComponent<DialogueSystem>();
    dialogueSystem.savedJson = dialogueSystem.dialogue.state.ToJson();
    PlayerPrefs.SetString("saveState", dialogueSystem.savedJson);

    Debug.Log(dialogueSystem.savedJson);
}

and here is how I'm trying to load the saved state on Start(), which doesn't load the saved state.

private void Start()
{
    dialogue = new Story(dialogueJSON.text);
    currentActiveBG = BGs[0]; 
    historyScript = GetComponent<History>();
    counterScript = GetComponent<counter>();
    
    if (PlayerPrefs.HasKey("saveState")) { // for loading the saved state

        dialogue = new Story(dialogueJSON.text);
        savedJson = PlayerPrefs.GetString("saveState");
        dialogue.state.LoadJson(savedJson);
        loadLine(); //this function contains story.Continue, which loads the next line
        Debug.Log(savedJson);
    }

    else {
        loadGame("Pre_Prep1"); // default starting line

    }

}

How can I make this work? And is there a better way of implementing this save/load feature?

winner_joiner
  • 12,173
  • 4
  • 36
  • 61
Patorikku
  • 61
  • 8
  • Please share some more information. What do `dialogueSystem.dialogue.state.ToJson();`, `Debug.Log(dialogueSystem.savedJson);` and `PlayerPrefs.GetString("saveState");` log? – Display name Jan 09 '23 at 10:12

1 Answers1

-1

I'm not sure which game you're going to make but, I use the following saving system in my project and it's working perfectly and you can also use it.

using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Saving System
public class UserData{
 #region Save Data Var's 
 // file Path
 private static readonly string FILE_PATH = Application.dataPath + 
 "/SaveFiles";
 public  int carToLoad;
 public  int levelToLoad;
 public  int unlockLevels;
 #endregion

 #region Methods


 // Save
 public static void SaveData(string contents){ // for Car Selection 
     File.WriteAllText(FILE_PATH + "/UserData.txt",contents);
 }

 // Load
 public static UserData LoadData(){
    string _data = File.ReadAllText(FILE_PATH + "/UserData.txt");
    return JSONToUserData(_data);
 }

 // Init 
 public static void UserData_init(){
    CheckFilesPreviousData();
    CheckDirectory();
 }

 // Selection Level Data 
 public static void SaveData(UserData LevelData) { // for Level 
    UserData tempObj = LoadData();
    tempObj.levelToLoad = LevelData.levelToLoad;
    SaveData(UserDataToJSON(tempObj));
 }

 // UnLock Level Data
 public static void SaveUnLockedLevelData(int LevelValue){
    UserData tempObj = LoadData();
    tempObj.unlockLevels = LevelValue;
    File.WriteAllText(FILE_PATH + "/UnLockLevels.text", 
 UserDataToJSON(tempObj));
}

 // Load UnLock Level Data
 public static UserData LoadUnLockLevelData(){
    string _data= File.ReadAllText(FILE_PATH+"/UnLockLevels.text");
    UserData tempObj = JSONToUserData(_data);
    return tempObj;
 }
 #endregion

#region Helper Methods
// !! important
 public static UserData JSONToUserData(string path) => 
  JsonUtility.FromJson<UserData>(path);// JSON to Obj
public static string  UserDataToJSON(UserData obj) => JsonUtility.ToJson(obj);// 
 Obj to JSON 

// Making Directory if not present
private static void CheckDirectory(){
    if (!Directory.Exists(FILE_PATH)){
        Directory.CreateDirectory(FILE_PATH);
    }
}

// Reset Data If User Play 1st Time
private static void CheckFilesPreviousData(){
    // Checking File is not Null
    UserData tempObj = LoadUnLockLevelData();
    if(tempObj == null){
        const int FIRSTLEVEL = 1;
        SaveUnLockedLevelData(FIRSTLEVEL);
        Debug.Log("Donee");
    }
    
}

#endregion

}

zain ul din
  • 1
  • 1
  • 2
  • 23
  • Writing data to an unencrypted file is a very bad practice. I haven't seen that done since The Lord of the Rings: The Battle for Middle-earth in 2004 where even a normie could hack the game. You should use PlayerPrefs or an online database to store player data. – Display name Jan 09 '23 at 10:09