0

Loosing my mind over it, but still cant find what's wrong. I have 2 List variables (items, playerEquipment.equipment), trying to save both of them and load after, but it gives me strange results. For items its always good, saves and loads all the time, but for playerEquipment.equipment behaviour is different - if i save and then immidiatly load without exiting play mode, i get right result from load function, but if i save, stop play mode, start play mode, and then load - i get list of "null" is result.

Here is my save code

public void SavePlayer()
    {
        SaveData data = new SaveData();
        data.level = level;
        data.nick = nick;
        data.experience = experience;
        data.experienceToNextLevel = experienceToNextLevel;
        data.maxHP = maxHP;
        data.minAttack = minAttack;
        data.maxAttack = maxAttack;
        data.coins = coins;
        data.items = items;
        data.equipment = playerEquipment.equipment;
        for (int i = 0; i < data.equipment.Count; i++)
            Debug.Log("Saved type " + data.equipment[i].type + " with id " + data.equipment[i].id+" and type "+ data.equipment[i].type);
        //Save data from PlayerInfo to a file named players
        DataSaver.saveData(data, "players");
    }

Here is my load code

public void LoadPlayer()
    {
        SaveData data = DataSaver.loadData<SaveData>("players");
        if (data == null)
        {
            Debug.Log("ERROR: data not loaded");
            return;
        }
        level = data.level;
        nick = data.nick;
        experience = data.experience;
        experienceToNextLevel = data.experienceToNextLevel;
        maxHP = data.maxHP;
        currentHP = data.maxHP;
        minAttack = data.minAttack;
        maxAttack = data.maxAttack;
        coins = data.coins;
        items = data.items;
        playerEquipment.equipment = data.equipment;
    }

SaveData is

[Serializable]
class SaveData
{
    //all data types and names that go to save
    public int level;
    public string nick;
    public int experience;
    public int experienceToNextLevel;
    public int maxHP;
    public float minAttack;
    public float maxAttack;
    public int coins;
    //public List<String> itemsString;
    //public List<String> equipmentString;


    public List<Item> items;
    public List<Item> equipment;
}

Save code i grabbed from this answer and didnt change it

UPD: Item class is

public enum ItemType { HELMET, SHOULDERS, WEAPON_MAIN, WEAPON_SECOND, BODY, ARMOR, HANDS, PANTS, BOOTS }

[CreateAssetMenu(menuName = "item")]
[Serializable]
public class Item : ScriptableObject
{
    public int id;
    public Sprite sprite;
    public string itemName;
    public ItemType type;
    
}
dgleming
  • 21
  • 1
  • 1
  • 4
  • Paste the definition of `Item` class. – aybe Jan 22 '23 at 03:20
  • Updated code with Item class – dgleming Jan 22 '23 at 03:30
  • Hello, please look at how to post a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) With the party of your code you posted no one can run it to reproduce what you are describing making it harder to give you an answer. – ephb Jan 22 '23 at 09:45

1 Answers1

0

You can not save and load a list of ScriptableObjects like that. JsonUtility.ToJson saves these references as InstanceIDs which are nor persistent. That is why it works when you do not stop play mode.

Since you already implemented an ID you could save a list of IDs instead and add a method which finds them by that references and adds adds them back to your playerEquipment.equipment

Edit: This means your statement of it working for the item list is likely false, you should check and edit your answer accordingly.

ephb
  • 594
  • 1
  • 15
  • Hi and thank you for your answer! I look forward to implement saving of List and then loading it, but what would be best way to load items? I imagine something like for(int i = 0; i < List.count; i++){ for(int i = 0; i < allequipmentlist.count; i++){ check if id's are equal}} but that nested list can be bad if ill have like 1000 unique equipmnets and 50 items in inventory - it will check for 50k iterations – dgleming Jan 22 '23 at 13:48
  • For the amount of items you describe I think the time it takes to complete is negligible. Especially since you are doing it only once when loading. Alternatively you could store your items in a dictionary with their ID as the key. – ephb Jan 23 '23 at 08:37