Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Using serialization to save detailed data

Discussion in 'Scripting' started by PelvisParsley, Nov 9, 2016.

  1. PelvisParsley

    PelvisParsley

    Joined:
    Aug 9, 2016
    Posts:
    89
    Hi, Im a noob, and as of yet I have been using only one variable saving. Basically, I have a static save script which has a class with variables i want to save. This is all fine and dandy. But now, I want to save additional data ( For example, saving the objects collected in a particular level) Now I cant simply do this with variables because each object collected is different for each level. So basically I want a value saved for each level. I tried using arrays but that doesnt seem to work. Im not really strong with Lists so dont know if its possible with that. Any idea how this can be achieved?
     
  2. vothka

    vothka

    Joined:
    Mar 27, 2015
    Posts:
    59
    Not sure if i get the problem correctly:

    i suggest using inheritence? so your list saves objects of the baseclass

    e.g.
    Code (CSharp):
    1. abstract class BaseItem {
    2. public string Name;
    3. public BaseItem(string _name)
    4. Name = _name;
    5. }
    6.  
    7. class Level1Item : BaseItem {
    8. public Level1Item(string _name) : base(_name)
    9. //do your level 1 specific stuff here
    10. }
    11.  
    12. //your list in your static class now is of type
    13.  
    14. List<BaseItem> someName;
    15.  
    16.  
    17.  
    you can typecast these BaseItem Objects to your Level1Item if you need a certain attribute that is only available in the Level1Item class
     
    PelvisParsley likes this.
  3. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    Can you be more specific with what you are trying to save?

    some objects (basically all Unity.Objects) can't be serialized, at least directly, for them you'll want to save a proxy (an id, for example, that represents a prefab, scriptableobject, texture, audiofile, basically any asset file). so when you load it back you use the proxy to return the actual asset at runtime.

    for complex data groupings (for instance a custom loadout on a instanced gameobject) I use a specialized ScriptableObject that tracks all the custom data and on command preformats the data into the preffered serialization format before sending
     
  4. PelvisParsley

    PelvisParsley

    Joined:
    Aug 9, 2016
    Posts:
    89
    No i m not trying to save a literal object, but ints/bools etc. What I meant by objects was like coins or gold collected. I want to save the coin collected value specific to each level.
     
  5. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    So, exactly what are you trying to save? Objects but not literal objects is kinda vague... Can you post a sample of your static class and save function you have now so we have a better idea what exactly you are trying to do?
     
  6. Mordus

    Mordus

    Joined:
    Jun 18, 2015
    Posts:
    174
    If i'm understanding him right he just wants to save a set of variables. I.e: Save the # of gold coins that was collected during a level, secrets found, items picked up, scores etc. Not saving/restoring gameobjects.
     
  7. PelvisParsley

    PelvisParsley

    Joined:
    Aug 9, 2016
    Posts:
    89
    Yes thats what i want to achieve. Only variables, no gameobjects. But i wanted to group variables together so that coins collected in a level is saved with that level number.
     
  8. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    In that case you could use the Generic Data manager from my signature: https://github.com/deMD/Unity-Data-Manager/blob/master/UnityDataManager/DataManager.cs

    Sample usage:
    Code (CSharp):
    1. [Serializable]
    2. public class SaveableData
    3. {
    4.     // Saveable fields. You could also use public fields, but that goes against normal C# conventions.
    5.     [SerializeField] private int score;
    6.  
    7.     public SaveableData(int score)
    8.     {
    9.         this.score = score;
    10.     }
    11.  
    12.     public int Score { get { return score; } }
    13. }
    14.  
    15. public class Level : MonoBehaviour
    16. {
    17.     [SerializeField] private string levelName;
    18.     [SerializeField] private int levelScore;
    19.  
    20.     private void SaveLevel()
    21.     {
    22.         var data = new SaveableData(levelScore);
    23.         DataManager.SaveData<SaveableData>(data, levelName + "_save");
    24.     }
    25.  
    26.     private void LoadLevel()
    27.     {
    28.         var data = DataManager.LoadData<SaveableData>(levelName + "_save");
    29.      
    30.         if(data != null)
    31.         {
    32.             levelScore = data.Score;
    33.         }
    34.     }
    35. }
     
    PelvisParsley likes this.