Search Unity

How to load a scriptable object?

Discussion in 'Scripting' started by LeRan, Jan 11, 2016.

  1. LeRan

    LeRan

    Joined:
    Nov 24, 2015
    Posts:
    116
    Hello forum,

    I'm having difficulties to load properly a scriptable objet that I'm using for data storage. Everytime I try to load a game, it tells me: "InventairePerso must be instantiated using the ScriptableObject.CreateInstance method instead of new InventairePerso."

    How can I do it right?

    The load function is very simple :
    Code (CSharp):
    1.             XmlSerializer leSerialXML = new XmlSerializer(typeof(Jeu));
    2.             FileStream file = File.Open(cheminDonnéesSauvegarde+"jeuxSauvés.sav", FileMode.Open);
    3.             Jeu.JeuCourant=(Jeu)leSerialXML.Deserialize(file); //the warning message happens right here.
    4.             file.Close();
    The whole data structure is quite basic too (I'm only testing how it works now):
    Code (CSharp):
    1. [System.Serializable]
    2. public class InventairePerso : ScriptableObject
    3. {
    4.     public int or;
    5.     public string test;
    6.     //a bogus inventory with a couple fields to see if it works
    7. }
    8.  
    9. [System.Serializable]
    10. public class Jeu {
    11.     public static Jeu JeuCourant = new Jeu(); //the whole data structure of the game in one static object
    12.     public string nomDuJeu; //a string because why not
    13.     public InventairePerso inventairePerso; //said bogus inventory as a subclass
    14.  
    15.     public Jeu() {
    16.         inventairePerso = ScriptableObject.CreateInstance<InventairePerso>();
    17.         //properly instantiated the first time, no warning displayed at start
    18.         inventairePerso.name = "case inventaire défaut";
    19.         inventairePerso.or = 12;
    20.         inventairePerso.test = "inventaire vide";
    21.         nomDuJeu = "nom du jeu vide";
    22.     }
    23. }
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,468
  3. LeRan

    LeRan

    Joined:
    Nov 24, 2015
    Posts:
    116
    Thanks for the answer KelsoMRK, but could you explain me how to do it from a saved file? My concern is to write the whole game status to disk to load it in another run, and binary files do not seem fit for that - I couldn't write scriptable objects. I thought XML would be good because the structure of the object was correctly saved, but I can not instantiate it when I load the data... What's a better solution?
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,468
    You can certainly save data to XML - or any other file format you want. ScriptableObjects aren't going to get you there by themselves though; and a lot of stuff in the UnityEngine namespace won't serialize straight-away. It sounds like you want to use your own data classes with custom inspectors that can read/write to files on the disk. That's fairly involved. For XML, I'd start with looking up System.Xml.Serialization on MSDN. Also note that 5.3 ships with some JSON serialization functionality baked in - so that might get you where you're going faster.
     
  5. LeRan

    LeRan

    Joined:
    Nov 24, 2015
    Posts:
    116
    I'm sorry, I admit that I quite new to Unity (and C# alltogether), but I don't really understand... The scriptable object can be perfectly serialized into the XML file : all fields, classes and lists are there with the right tags, all classes and child classes fit nicely into each other (I complexified a bit the structure since last time)... so, the file is the exact depiction of the game world when it's saved.

    The only problem occurs during deserialization : I understand that scriptable objects must be instantiated and not created anew, and I don't know how to tell the deserialization method to do that...
     
  6. LeRan

    LeRan

    Joined:
    Nov 24, 2015
    Posts:
    116
unityunity