Search Unity

Saving and Loading, need some advice.

Discussion in 'Scripting' started by Loki18000, Oct 3, 2019.

  1. Loki18000

    Loki18000

    Joined:
    Apr 1, 2015
    Posts:
    75
    Hello!

    I'm approaching the end of Development and I've hit the Saving and Loading portion. My most feared section!

    I need a little advice on how to get this to work.

    The player is able to instantiate Prefabs into the game world. They are then able to alter the prefab. Such as Materials and Inactive and Deactivate children and so on. Saving these objects seems to be complex.

    I understand the general gist of how this would work but I'm a little lost on how to implement it.

    When the player clicks Save or the game Auto Saves the script will

    Get Every Game Object that needs to be saved. Maybe the objects that need to be saved will have a script on it or a tag. The script will search for these.

    Or maybe when a Prefab is added into the game world they are added to a List so the Objects are easy to find without having to use GameObject.Find.

    It will then get the id of the prefab as an int or string and its Transform in a float. X Y Z. When the game is Loaded the script will Instantiate each prefab using the id and setting the transform. It will instantiate each object using a DataBase script with a list of every Prefab.

    Seems easy enough!

    BUT how would I get information for each Object, what transform belongs to what GameObject instantiated. How would I go about getting information on the GameObjects children such as Active or Deactivated or their materials and what information belongs to what GameObject's child. I know the script will need to save the Materials as a string to then find the material using a DataBase mention above. But how would the script know what information belongs with what GameObject?

    It seems complected. I'm currently attempting to use the Asset Easy Save but I've run into some issues and nobody seems to be answering my questions. So in the mean time I thought I'd give creating my own Saving and Loading a go.

    Thanks for any advice.
     
    Last edited: Oct 3, 2019
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @Loki18000

    "BUT how would I get information for each Object..."

    Seems like you already have most of the stuff figured out. I'm still learning how to do saving/loading properly too. When loading do the reverse of saving. Process your "props" for example... get the first, check its ID, find matching prop from your prop "database" then clone it and place it with save data position.

    "How would I go about getting information on the GameObjects children such as Active or Deactivated or their materials and what information belongs to what GameObject's child."

    "But how would the script know what information belongs with what GameObject?"


    I think you are just overthinking it. One unit of save data can contain save data for one prop/world item... and it has a unique ID. Just add more fields to your save data classes. Fields that are lists of save data classes for children for example. Then save data also has a string field for material, bool field for on/off state and so on, whatever you need to remember. When looping through all the item data that got loaded,first get the ID, then get the matching prefab, then just give the found prefab (or more like its script) the data, and let the prop/turret/whatever do its own setup with given data.
     
    Loki18000 likes this.
  3. Loki18000

    Loki18000

    Joined:
    Apr 1, 2015
    Posts:
    75

    That is very useful. I think I'll have to build my own Saving and Loading script to get what I need as the Asset I'm using doesn't seem to be up to the task.

    I'll see what I can come up with using your Advice.

    Thank you.
     
    eses likes this.