0

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.

OmBiEaTeR
  • 53
  • 9
  • 1
    See **2. MULTIPLE DATA(ARRAY JSON)** from the answer in the duplicated question. – Programmer Jun 14 '17 at 05:57
  • so in my instance I would save like so – OmBiEaTeR Jun 14 '17 at 06:29
  • not enough characters for my code oops ill edit question – OmBiEaTeR Jun 14 '17 at 06:30
  • 1
    Do not modify your question and remove the original code. That will confuse everyone. Add **EDIT** in your question followed by your new code. – Programmer Jun 14 '17 at 06:39
  • 1
    As for your new code, ignore `\r\n`. Those are used to test the json. `itemsToJson` value should come from the loaded json data. You are supposed to save and load the json with `PlayerPrefs`. The only thing that should change from your original code are the use of `JsonHelper.ToJson` and `JsonHelper.FromJson`. Everything else should remain the-same. – Programmer Jun 14 '17 at 06:41
  • I Think the script is now using playerprefs and Jsonhelper but not loading, could you check to see if I am doing something incorrectly? – OmBiEaTeR Jun 14 '17 at 07:06
  • It looks fine now. Please read **4.TROUBLESHOOTING JsonUtility** from the duplicated answer. That will save both of us time. If there is problem then post your `Item` class exactly how it is. – Programmer Jun 14 '17 at 07:23
  • It seems that it not working is related to me using `get` and `set` inside the variables, I am unsure what I would replace them with. – OmBiEaTeR Jun 14 '17 at 08:04
  • Remove the 'get' and 'set'. That's it. – Programmer Jun 14 '17 at 08:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146615/discussion-between-ombieater-and-programmer). – OmBiEaTeR Jun 14 '17 at 08:17
  • oops I made it into chat because I thought I only had 15 characters left to type with, Removing only "get" and set" `public string Description { { return _description; } { _description = value; } }` Gives me an error: 'Item.Description': property or indexer must have at least one accessor I get an error after removing get and set, and the brackets that I left there – OmBiEaTeR Jun 14 '17 at 08:24
  • Hi, finally got time to run your code. The current problem now is the variable you declared such as `private string _name;`, `private string _description;` etc. They are all private. *You must either make them `public` or add `[SerializeField]` on top of each variable* in your `Item` class. I hope that's clear and your problems are now solved. – Programmer Jun 14 '17 at 14:41
  • I ended up figuring out that part out myself, I should probably stop and take things more slowly I kept overlooking so many things. Anyway thanks a lot for taking the time to go through my code. Everything works perfectly now. – OmBiEaTeR Jun 15 '17 at 08:56
  • You are welcome! – Programmer Jun 15 '17 at 08:57

0 Answers0