Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Recreating objects from binary savefile

Discussion in 'Scripting' started by NoizedropBP, Sep 8, 2017.

  1. NoizedropBP

    NoizedropBP

    Joined:
    Mar 13, 2015
    Posts:
    5
    Hey!

    I tried to solve this problem for a while now and can't find a proper solution and get more and more confused about the whole theory behind it.
    I've read hundreds of different articles and forum topics about it but couldn't find a solution.
    Maybe i tackle this problem in the complete wrong way..I'm open for any suggestions!

    So my problem is the following. I've created a binary formatting save systems which works fine.
    It saves a playerData File that can be properly loaded and is persitent through scenes.

    The PlayerData holds a List of CreatureData (i don't like the comparison but yeah think about pokemon)
    when entering a battle i have to recreate the Creatures from the saved Data (all saved Data is serialized)

    I didn't expect to get problems with the re-Instantiation of the Gameobjects.

    A battle participant was planned to consist out of a
    -prefab (A Mesh with Animations)
    -CreatureTemplate (Base Informations for all creatures of that type) as scriptable object
    -CreatureData (Current individual values for this specific creature) This is what is saved in the playerData file

    i need this structure because every creature will have the same template but will develop individually throughout it's levels.

    I planned to use Resources.Load with a string id from the creatureData to load the referenced scriptable object asset(which again holds the prefab))
    But how ever i try this i always get back a null.

    So is there already a flaw in that logic? how should i try to recreate a gameobject from the data i can serialize?

    I also tested the same function call with a simple cube prefab which worked...
    Which leads me to the guess, that i can't load custom ScriptableObject .asset files created with this method
    http://wiki.unity3d.com/index.php?title=CreateScriptableObjectAsset

    This is the exact code passage
    GameObject newObject = Instantiate(Resources.Load(curPath,typeof (Object))) as GameObject;

    and yes the files are located in Resources/Creatures..so i guess i got something wrong logically.

    Hope i kind of made my problem clear enough.
    Thanks in advance to everyone who takes this time to read and answer to this!
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    Are you making sure you are setting that string value?

    My current project has a similar setup, and works great... once all the values are set correctly.
     
  3. NoizedropBP

    NoizedropBP

    Joined:
    Mar 13, 2015
    Posts:
    5
    Thank you that already gives me some hope that i'm not totally wrong track here...might be something with the type casting.

    Here' a bit more code:

    Variable Declaration
    Code (CSharp):
    1. private const string templatePath = "CreatureTemplates/";
    before method call
    Code (CSharp):
    1. string curPath = templatePath+Player.instance.data.Team[i].ID;

    which returns
    Creatures/Larva for example
    and should direct to my file located in
    Assets/Resources/Creatures/Larva.asset

    I guess the problem is somwhat in the typeof. currently im simply looking for an object adn try to cast it into a gameobject so can can use a getcomponent to get on my CreatureData...i tried to look for creaturedata in the beginning but couldnt bring that to work either... and received "cannot convert 'unityEngine.Object" to "CreatureData" via built-in conversion....so i guess this means that my custom asset is considered an Object.
    i tried tons of stuff but i'm totally stuck here
     
    Last edited: Sep 9, 2017
  4. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,260
    Ah yes you are using a ScriptableObject. I'm using the prefabs directly, so its a little different.

    To get it to load, I found a line of code from this forum post.
    Code (CSharp):
    1. var cData = (CreatureData)Resources.Load(curPath, typeof(CreatureData));
    Not sure if you need to create an instance of it after that to get the data from it, but it should work.
     
  5. NoizedropBP

    NoizedropBP

    Joined:
    Mar 13, 2015
    Posts:
    5
    Thank you Chris for your help.

    I tried your code snippet but i get the exact same error (can't cast from Unity.Object to CreatureData)
    it happens at the typeof Passage.

    I'm still absolutly lost and it get's worse and worse each day.
    A friend of mine suggested to restructure it a bit and simply save a reference to the scriptable object.
    I tried and failed miserably.

    when i try to save a reference to my scriptable object i get a serialization error no matter how i structure it.
    • when the scriptableobject isn't serialized i get a (serialization missing error) so i guess every object that is somehow referenced in my saveFile also has to be serializable
    • when i serialize the scriptableObject to i get a huge error message that doesn't tell me anything
      (serialization exception type unityEngine.ScriptableObject in assembly UnityEngine is not marked as serializable)
      Brickwall for me. I have no Idea why i can't serialize it...
    So i'm back to the idea of using a string or int to find my scriptableobject reference at runtime... here is a small graph of what im trying to achieve.



    https://drive.google.com/file/d/0B1WTi87HjxTcMno0dWpnUThVbFE/view?usp=sharing
     
  6. NoizedropBP

    NoizedropBP

    Joined:
    Mar 13, 2015
    Posts:
    5
    Solved!

    I just received the right hint for something like this! In case anybody else might need this.

    If you have a scriptableObject as an Asset Instance you have to use AssetDatabase.LoadAtPath and everything works like a charm. maybe i will stumble upon some further problems with this but for now atleast it seems to be working :)