Home>

I make a save system in the game:

using System.Collections.Generic;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine;
public class SaveDataItems: SaveDataRaw
{
    public int [] ints= new int [0];
    public void PrepareForSave ()
    {
        string data= JsonUtility.ToJson (ints);
    }
    public void Load ()
    {
        ints= JsonUtility.FromJson <
int [] >
(data);
    }
}
[Serializable]
public class SaveDataRaw
{
    public string data;
    public void PrepareForSave () {}
    public void Load () {}
}
public class SaveManager: MonoBehaviour
{
    public static void Save (string fileName, SaveDataRaw data)
    {
        BinaryFormatter bf= new BinaryFormatter ();
        FileStream file= File.Create (Application.persistentDataPath
          + "/"+fileName+".dat");
        data.PrepareForSave ();
        bf.Serialize (file, data);
        file.Close ();
    }
    public static SaveDataRaw Load (string fileName)
    {
        if (File.Exists (Application.persistentDataPath + "/" + fileName + ".dat"))
        {
            BinaryFormatter bf= new BinaryFormatter ();
            FileStream file=
            File.Open (Application.persistentDataPath + "/" + fileName + ".dat", FileMode.Open);
            SaveDataRaw data= (SaveDataRaw) bf.Deserialize (file);
            file.Close ();
            return data;
        }
        else return new SaveDataRaw ();
    }
}

But if I try to load a list of items

SaveDataItems save= (SaveDataItems) SaveManager.Load ("Items");
save.Load ();

Then an error appears at startup, tell me what you can do?

  • Answer # 1
    public static SaveDataRaw Load (string fileName)
    

    The method returnsSaveDataRaw, and you are trying to cast him inSaveDataItems...

    (SaveDataItems) SaveManager.Load ("Items");
    

    That is, you are trying to cast the parent's typeSaveDataRaw datato the heir.

    Such a caste is impossible. You can only the heir to the parent, or the heir, previously raised to the parent, lower back to himself.

    This is how you can

    SaveDataItems item= new SaveDataItems ();
    SaveDataRaw cast= (SaveDataRaw) item;
    

    And this is how you can

    SaveDataItems item= new SaveDataItems ();
    SaveDataRaw upcast= (SaveDataRaw) item;
    SaveDataItems downcast= (SaveDataItems) upcast;
    

    But this is impossible

    SaveDataRaw item= new SaveDataRaw ();
    SaveDataItems cast= (SaveDataItems) item; //InvalidCastException
    

    That is, it is not the type of the object link that is important here, but the type of the object itself under the link, which is created usingnew...

    I searched and found that you can make your own caste

    YuraSuper20482021-12-27 05:09:11
  • Answer # 2
    public static SaveDataRaw Load (string fileName)
    

    The method returnsSaveDataRaw, and you are trying to cast him inSaveDataItems...

    (SaveDataItems) SaveManager.Load ("Items");
    

    That is, you are trying to cast the parent's typeSaveDataRaw datato the heir.

    Such a caste is impossible. You can only the heir to the parent, or the heir, previously raised to the parent, lower back to himself.

    This is how you can

    SaveDataItems item= new SaveDataItems ();
    SaveDataRaw cast= (SaveDataRaw) item;
    

    And this is how you can

    SaveDataItems item= new SaveDataItems ();
    SaveDataRaw upcast= (SaveDataRaw) item;
    SaveDataItems downcast= (SaveDataItems) upcast;
    

    But this is impossible

    SaveDataRaw item= new SaveDataRaw ();
    SaveDataItems cast= (SaveDataItems) item; //InvalidCastException
    

    That is, it is not the type of the object link that is important here, but the type of the object itself under the link, which is created usingnew...

    I searched and found that you can make your own caste

    YuraSuper20482021-12-27 05:09:11