简体   繁体   中英

Unity problems with loading save

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;
    
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM