0

I have been working on a save game solution for my project and have hit a wall. After several days of research, trial/ error etc. I decided to give the stack a shot. I am attempting to convert back from a Json text file into an object, then add it to a dictionary. It keeps giving my Invalid cast exceptions, regardless as to how I iterate through the Jdata object. Keeping in mind I am using LitJson 3rd party. Here is the code thus far. The error occurs at the foreach statement.

 using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using LitJson;
    using System.IO;

    public static class SaveGame  {


    public static string savePath = Application.persistentDataPath + 
   "/Saves/";
    public static int numberOfSaves = 0;
    public static string saveFileName = PlayerCrew.playerShipName + ".json";

    public static void SavePlayerData ()
    {
        string playerSavePath = savePath + saveFileName;
        string jsonHolder;

        jsonHolder = JsonMapper.ToJson(PlayerCrew.playerCrewManifest);

        if (!File.Exists(playerSavePath))
        {
            FileStream fs = new FileStream(playerSavePath, 
    FileMode.OpenOrCreate);
            fs.Close();
            File.WriteAllText(playerSavePath, jsonHolder);          

        }
        else
        {
            File.WriteAllText(playerSavePath, jsonHolder);
        }

     }

     public static void LoadCrewManifest()
     {
        string playerSavePath = savePath + saveFileName;
        string jsonHolder;

        jsonHolder = File.ReadAllText(playerSavePath);
        JsonData jdata = JsonMapper.ToObject(jsonHolder);
        PlayerCrew.playerCrewManifest.Clear();

        foreach (KeyValuePair<string,CrewMember> item in jdata)
        {
            PlayerCrew.playerCrewManifest.Add(item.Key, item.Value);
            Debug.Log(item.Key);
        }

    }





    }
Dave cook
  • 1
  • 1

2 Answers2

0

I recommend you to use NetJson. You can Deserialize using a generic type, including Dictionary.

  • Also, you can use [PlayerPrefs](https://docs.unity3d.com/ScriptReference/PlayerPrefs.html).Set(key, myJson) since its a string. Then you can just use PlayerPrefs.Get to retrieve it – Martin Gonzalez Nov 12 '18 at 01:39
  • Retrieving the Json string is not the issue. The issue is that it will not allow me to convert the text back to an object after I have loaded back from the file.Saving works just fine. – Dave cook Nov 12 '18 at 11:40
  • Oh! so its how @derHugo said. – Martin Gonzalez Nov 13 '18 at 13:11
0

The values in jdata might come as KeyValuePair<string, string>

the simplest way would be a simple constructor for your class CrewMember e.g. something like

[Serializable]
public class CrewMember
{
    public string Name;

    public CrewMember(string name)
    {
        Name = name;
    }
}

Than you don't want the item.key since it will be the variable name (in this case Name) instead you want the item.Value.

Your json code could look like

JsonData jdata = JsonMapper.ToObject(jsonHolder);
PlayerCrew.playerCrewManifest.Clear();

foreach (KeyValuePair<string,string> item in jdata)
{
    PlayerCrew.playerCrewManifest.Add(item.Value, new CrewMember(item.Value));
    Debug.Log(item.Value);
}
derHugo
  • 83,094
  • 9
  • 75
  • 115