Saving
public void Save()
{
Item[] saveData = new Item[inv.Count];
// Item[] saveData = (inv.ToArray());
string jsonData = JsonUtility.ToJson(saveData.Length); // Showing up as null??
PlayerPrefs.SetString("InventoryData", jsonData);
PlayerPrefs.Save();
}
Loading
void Load()
{
string jsonData = PlayerPrefs.GetString("InventoryData");
Item[] loadedData = JsonUtility.FromJson<Item[]>(jsonData);
inv.Add(loadedData[0]); // Loading a null item because the saving isnt working
Debug.Log(inv[0].Name.ToString()); // print name of item in out inv
}
EDIT1:
So for my instance I would save like so
public void Save()
{
Item[] saveItems = new Item[inv.Count];
saveItems[0] = new Item();
saveItems[0].Id = inv[0].Id;
saveItems[0].Name = inv[0].Name;
saveItems[0].Description = inv[0].Description;
saveItems[1] = new Item();
saveItems[1].Id = inv[1].Id;
saveItems[1].Name = inv[1].Name;
saveItems[1].Description = inv[1].Description;
//Convert to Jason
string itemsToJson = JsonHelper.ToJson(saveItems, true);
Debug.Log(itemsToJson);
Debug.Log(saveItems[0].Name);
}
but I am unsure about 1 part of the loading
void Load()
{
string itemsToJson = "{\r\n \r\n}";
Item[] invent = JsonHelper.FromJson<Item>(itemsToJson);
Debug.Log(invent[0].Name);
}
For the string I am unsure how to write it appropriate to my code, but if that is done correctly this should work? The saving part produced no errors but because I don't understand the string part It is of course not working.
I am not familiar with the syntax of the string, other than \n which I know is new line.
EDIT2:
I have added the playerprefs lines so right now it should be working?
Saving produces no errors and debug.log is printing correctly
public void Save()
{
Item[] saveItems = new Item[inv.Count];
saveItems[0] = new Item();
saveItems[0].Id = inv[0].Id;
saveItems[0].Name = inv[0].Name;
saveItems[0].Description = inv[0].Description;
saveItems[1] = new Item();
saveItems[1].Id = inv[1].Id;
saveItems[1].Name = inv[1].Name;
saveItems[1].Description = inv[1].Description;
//Convert to Jason
string jsonData = JsonHelper.ToJson(saveItems, true);
PlayerPrefs.SetString("InventoryData", jsonData);
PlayerPrefs.Save();
Debug.Log(saveItems[0].Name);
}
Loading
The debug.log gives me null, am I still doing something incorrect?
void Load()
{
string jsonData = PlayerPrefs.GetString("InventoryData");
Item[] invent = JsonHelper.FromJson<Item>(jsonData);
Debug.Log(invent[0].Name); // Null
inv.Add(invent[0]); // Test adding the saved items to the inventory list
}
Edit 3:
Item Class
using UnityEngine;
[System.Serializable] // savable
public class Item
{
private string _name;
private string _description;
private int _id;
private int _value;
private Texture2D _icon;
private string _mesh;
private ItemType _type;
private Weapon _weaponType;
private WeaponEquipType _equipType;
private int _amount, _damage, _defence, _hitpoints;
public void Init()
{
_name = "";
_description = "";
_id = 0;
_mesh = "";
_type = ItemType.Armour;
_value = 0;
}
public void Init(string name, int value, int id, string description, string meshName,
ItemType type, Weapon weaponType, WeaponEquipType equipType, int damage)
{
_name = name;
_value = value;
_id = id;
_description = description;
_mesh = meshName;
_type = type;
_weaponType = weaponType;
_equipType = equipType;
_damage = damage;
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Description
{
get { return _description; }
set { _description = value; }
}
public int Id
{
get { return _id; }
set { _id = value;}
}
public int Value
{
get { return _value;}
set { _value = value;}
}
public Texture2D Icon
{
get { return _icon; }
set { _icon = value; }
}
public string Mesh
{
get { return _mesh; }
set { _mesh = value; }
}
public ItemType Type
{
get { return _type; }
set { _type = value; }
}
public Weapon WeaponType
{
get { return _weaponType; }
set { _weaponType = value; }
}
public WeaponEquipType EquipType
{
get { return _equipType; }
set { _equipType = value; }
}
public int Amount
{
get { return _amount; }
set { _amount = value; }
}
public int Damage
{
get { return _damage; }
set { _damage = value; }
}
public int Defence
{
get { return _defence; }
set { _defence = value;}
}
public int HitPoints
{
get { return _hitpoints; }
set { _hitpoints = value; }
}
}
[System.Serializable] // savable
public enum ItemType
{
Armour,
Weapon,
Consumable,
Craftable,
Money,
Miscellaneous
}
[System.Serializable] // savable
public enum Weapon
{
Sword,
Bow,
Shield,
Staff,
Item,
}
[System.Serializable] // savable
public enum WeaponEquipType
{
OnHand,
OffHand,
TwoHand,
Either
}
I think my problem is listed on that post as deserializing C, my variables have get and set in them, what would I enter instead?
I apologise for wasting your time, this is the last part I need to complete for an assignment (which we are allowed to get help with). I will definitely go over this in my spare time so that I understand it in its entirety.
I need to get this small task out of the way so I have time to complete a larger project I am also working on.
I appreciate the effort of continually helping me.