Search Unity

Best way to save and load all objects and their parameters in a scene.

Discussion in 'Scripting' started by unityplease, Jun 25, 2015.

  1. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    32
    I thought I'd create a thread because my unity answers question doesn't appear to be going anywhere:

    http://answers.unity3d.com/questions/961322/best-way-to-save-and-load-all-objects-and-their-pa.html

    Essentially,

    I have made a very complex game with lots of characters walking around each instantiated with their own individually randomly chosen materials and attributes.

    I'd like to save their script properties, attribute states, mechanim animation state, their AI RAIN state, their locations, etc so I can load them back. But for sometime that seems so essential to game design there is nothing available only tutorials on how to save individual serialized simple types.

    I tried creating a serializable data class with gameobject lists inside it. But gameobjects won't serialize.

    The game relies on a lot of progress with creating new characters and leveling them up. But all this is lost if the game is stopped and restarted. Please help because it seems like this must be something everyone needs.

    A lot of old solution pages suggested unity serializer but the old webpages of Mike Talbot are now offline and people mentioned that his code is full of errors.
     
  2. malkere

    malkere

    Joined:
    Dec 6, 2013
    Posts:
    1,212
  3. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
  4. unityplease

    unityplease

    Joined:
    Aug 31, 2014
    Posts:
    32
    I have watched that tutorial serveral times, have completed that tutorial and I am fully aware of playerprefs.

    1. Player prefs does not store gameobjects but rather strings, and although, other data types can be serialized and saved as strings via encoding/decoding this is not the best way to save data. Rather saving data as a binary file is more approprate. Despite this the point that gameobjects cannot be serialized means that this does not solve the problem

    2. The tutorial describes this second method of saving but again only the serialization of simple types in a container class, and then their reasignment. This does not work for gameobjects/ instantiated gameobjects and their attached components so it comes no way near to solving the problem.

    I am still looking for a method of saving the scene 'state' at runtime and then loading it again after the game has been restarted.
     
  5. malkere

    malkere

    Joined:
    Dec 6, 2013
    Posts:
    1,212
    a gameobject is just a reference to a type of object and its variables. there are very few cases where a gameobject itself would actually -need- to be stored... for example a procedurally generated mesh? Even then though its just a list of vertex positions and a triangle index which could be dumped into strings and other things data mediums if needed.

    It sounds a bit troublesome, but assuming each of your NPCs has a unique ID you can use that as a locator and save all the needed information separately. preferably grouping as much as possible into a string for enums, states, materials etc which probably don't have a horribly huge batch of choices to chose from. everything is just variables, very few cases where you can't recreate everything from a list of all the tiny variables. Not as appealing as a simple "save" button that just dumps everything without question into a file, but most programs only save specific changes to the normal pattern.

    as for your "state" how are you diferentiating this during runtime? why can this not be stored and input at play?