Search Unity

Official Progress Saving System

Discussion in 'Open Projects' started by Feral_Pug, Sep 30, 2020.

  1. Feral_Pug

    Feral_Pug

    Joined:
    Sep 29, 2019
    Posts:
    49
    Hey,

    Starting a thread about the saving and loading system.

    I guess things I would think to think about for this would be:

    1. What aspects of the game are persistent

    2. at which times is the player able to save.

    3. Code basis for implementation

    I have some experience writing scripts that control and use binary readers and writers that save wanted data to files.

    Thoughts?
     
  2. DaSerialGenius

    DaSerialGenius

    Joined:
    Jun 27, 2017
    Posts:
    17
    Would'nt this come much later when we have most of the systems in place so that we know what to save what to leave to the systems in place?
     
    luisquid likes this.
  3. Feral_Pug

    Feral_Pug

    Joined:
    Sep 29, 2019
    Posts:
    49
    Yeah you are right. I think I was just excited and thought I saw this on one of the cards...
     
  4. Neonage

    Neonage

    Joined:
    May 22, 2020
    Posts:
    287
    I would want to write that package later too.
    Simple as PlayerPrefs, but generic, feature rich and with visual inspector :)
     
  5. Feral_Pug

    Feral_Pug

    Joined:
    Sep 29, 2019
    Posts:
    49
    Yeah, It also doesn't hurt to think about this now as it might require lots of components to be adjusted later on depending on how this works. I try to avoid using player prefs because they are easily editable by users, which could cause game breaking bugs. Much safer to save necessary data into binaries so that most users wouldn't be able to edit.
     
  6. cirocontinisio

    cirocontinisio

    Joined:
    Jun 20, 2016
    Posts:
    884
    I always imagine this as a bit of a stretch goal, since I don't know yet how long will the game be. Do we need a progress saving system?

    But it would be fun to make. No harm in starting to talk about it!

    (PS: I took the liberty to edit the title so it doesn't get confused with the Game Settings)
     
  7. MUGIK

    MUGIK

    Joined:
    Jul 2, 2015
    Posts:
    481
    I have recently made one on github. Here is a link: https://github.com/STARasGAMES/Save-System

    But for this game it's an overkill.

    I could easily develop a lightweight version featuring:
    • Singleton pattern (it's for beginners as I understand)
    • C# events (or UnityEvents?)
    • JSON serialization (cuz it's simpler than Binary and easy to understand)
    • Custom inspector editors for saveable components
     
  8. kcastagnini

    kcastagnini

    Joined:
    Dec 14, 2019
    Posts:
    61
    This is what I use in my prototypes/small games.
    It's a really simple class which takes an object and serializes it (as @Feral_Pug suggested) to the persistent data path.

    serializer.PNG

    A trivial example of a save file would be:
    saveExample.PNG

    The Serializer would be used by a hypothetical PlayerDataManager to load player's data when the game boots for the first time and to save data on the disk on OnApplicationQuit or OnApplicationPause.

    A trivial example would be:
    saveManager.PNG
     
    Last edited: Oct 1, 2020
  9. MUGIK

    MUGIK

    Joined:
    Jul 2, 2015
    Posts:
    481
    Why did you make it a generic class?
    You can just make a static generic method like this:
    Code (CSharp):
    1. public static class SaveUtility
    2. {
    3.     public static void Save<T>(string fileName, T value) where T : new()
    4.     {
    5.         // code
    6.     }
    7.  
    8.     public static T Load<T>(string fileName) where T : new()
    9.     {
    10.         // code
    11.     }
    12. }
     
    kcastagnini likes this.
  10. kcastagnini

    kcastagnini

    Joined:
    Dec 14, 2019
    Posts:
    61
    Yes you are right.
     
  11. dkaloger

    dkaloger

    Joined:
    Jul 7, 2019
    Posts:
    39
  12. Feral_Pug

    Feral_Pug

    Joined:
    Sep 29, 2019
    Posts:
    49
    Yes, the solution I had in mind was similar to what @kcastagnini laid out. I typically would make classes that are responsible for creating writers and readers and initialize the saving or loading function. And then all classes that have data that need to either be saved or loaded inherit from a class that has functionality for saving and loading. The classes that create the readers and writers then go through an array or list of gameobjects that belong to the parent class of saveable objects and runs calls to the method on them that has the instructions for how to save that class. I have examples of this but nitty gritty is different for every game depending on what you are saving.