Saving game progress with JSON - LitJson Error
Hi guys, this is my first post here on the forum, sorry if I did something wrong ^^
Whenever I try to convert a list to json I get a error: "Max allowed object depth depth while trying to export from type UnityEngine.Vector3", and I have no idea how to solve this, I wish someone could help me and If you think there is a better way to do the game save please let me know.
Here is my code: (I'm using Litjson)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
public class ItemDatabase : MonoBehaviour {
public List<Item> database = new List<Item> ();
private JsonData itemData;
private JsonData itemDataSave;
void Start()
{
ConstructItemDatabase ();
}
void SaveGame()
{
**itemDataSave = JsonMapper.ToJson (database);**
File.WriteAllText (Application.dataPath + "/StreamingAssets/Items.json", itemDataSave.ToString());
}
public void ConstructItemDatabase()
{
itemData = JsonMapper.ToObject (File.ReadAllText (Application.dataPath + "/StreamingAssets/Items.json"));
database.Clear ();
for (int i = 0; i < itemData.Count; i++)
{
database.Add (new Item ((int)itemData [i] ["id"], itemData [i] ["tipo"].ToString (),
itemData [i] ["title"].ToString (), (int)itemData [i] ["nivel"], (int)itemData [i] ["value"],
itemData [i] ["descricao"].ToString (), (int)itemData [i] ["quant"], (int)itemData [i] ["comestivel"]));
}
}
}
public class Item
{
public int ID { get; set; }
public string Tipo { get; set; }
public string Title { get; set; }
public int Nivel { get; set; }
public int Value { get; set; }
public Sprite Sprite { get; set; }
public string Descricao { get; set; }
public int Quant { get; set; }
public int Comestivel { get; set; }
public Item(int id, string tipo, string title,int nivel, int value, string descricao, int quant, int comestivel)
{
this.ID = id;
this.Tipo = tipo;
this.Title = title;
this.Nivel = nivel;
this.Value = value;
this.Descricao = descricao;
this.Quant = quant;
this.Comestivel = comestivel;
this.Sprite = Resources.Load<Sprite> ("Sprites/Items/" + title);
}
public Item()
{
this.ID = -1;
}
}
Comment