Search Unity

Asset Store: Auto-Magic Save System

Discussion in 'Works In Progress - Archive' started by DexRobinson, Mar 24, 2014.

  1. DexRobinson

    DexRobinson

    Joined:
    Jul 26, 2011
    Posts:
    594
    How many times have you made a game only to get to the saving part of it and had to write out line after line using PlayerPref.Set whatever. Then when there is other variables you add in later you have to go back and write more save and load lines. There is now a SIMPLE and EASY way to save all your variables.

    I give you...Auto-Magic! With the Auto-Magic save system you now NEVER have to worry about writing a save system again. Just write all your variables into a static class that is provided for you and then when you need to save or load you just have to make one call to the save or load function. It's that simple. No more manually writing out lines of code again.


    [THE OLD WAY]
    Example 1: John has 10 variables he wants to save for his game. Here is a sample of what John's code would look like using the standard way...
    Code (csharp):
    1.  
    2. public class Saving : MonoBehaviour
    3. {
    4.     void Save()
    5.     {
    6.         PlayerPrefs.SetInt("variable1", 1);
    7.         PlayerPrefs.SetInt("variable2", 2);
    8.         PlayerPrefs.SetInt("variable3", 3);
    9.         PlayerPrefs.SetInt("variable4", 4);
    10.         PlayerPrefs.SetInt("variable5", 5);
    11.         PlayerPrefs.SetInt("variable6", 6);
    12.         PlayerPrefs.SetInt("variable7", 7);
    13.         PlayerPrefs.SetInt("variable8", 8);
    14.         PlayerPrefs.SetInt("variable9", 9);
    15.     }
    16.  
    17.     void Load()
    18.     {
    19.         int var1 = PlayerPrefs.GetInt("variable1");
    20.         int var2 = PlayerPrefs.GetInt("variable2");
    21.         int var3 = PlayerPrefs.GetInt("variable3");
    22.         int var4 = PlayerPrefs.GetInt("variable4");
    23.         int var5 = PlayerPrefs.GetInt("variable5");
    24.         int var6 = PlayerPrefs.GetInt("variable6");
    25.         int var7 = PlayerPrefs.GetInt("variable7");
    26.         int var8 = PlayerPrefs.GetInt("variable8");
    27.         int var9 = PlayerPrefs.GetInt("variable9");
    28.     }
    29. }
    30.  
    [THE AUTO-MAGIC WAY]
    Same Example as John's using Auto-Magic
    Code (csharp):
    1.  
    2. public static class Variables
    3. {
    4.     public static float variable1 = 0;
    5.     public static float variable2 = 0;
    6.     public static float variable3 = 0;
    7.     public static float variable4 = 0;
    8. }
    9.  
    Code (csharp):
    1.  
    2. public class Saving : MonoBehaviour
    3. {
    4.     void Save()
    5.     {
    6.         SavedVariables.SaveVariables();
    7.     }
    8.  
    9.     void Load()
    10.     {
    11.         SavedVariables.Load();
    12.     }
    13. }
    14.  
    As you can see using Auto-Magic you can greatly increase the speed in which you want to save and load variables. Auto-Magic also allows you to save under multiple locations for games that may have multiple save slots using the same functions as before

    [SAVING MULTIPLE FILES]​

    Code (csharp):
    1.  
    2. public class Saving : MonoBehaviour
    3. {
    4.     int index = 1;
    5.  
    6.     void Save()
    7.     {
    8.         SavedVariables.SaveVariables(index);
    9.     }
    10.  
    11.     void Load()
    12.     {
    13.         SavedVariables.Load(index);
    14.     }
    15. }
    16.  
    $Photo2.jpg


    Auto-Magic also comes with a built in Editor Window that allows you to view all your saved variables. No more guess what a saved variables is or having to write out tedious log code to output what you saved. With the built in editor you can also manipulate data to change or tweak saved data.


    $Photo1.jpg


    $Photo3.jpg



    [LIMITATIONS]​

    While the Auto-Magic is pure awesome saving power, it does have a few set backs. It uses Unity's built in Player Prefs system so XML or any other kind of saving is not going to work unless you write the code yourself to get it too work. Also there is a set amount of types that you can save, which are the following...
    • float
    • double
    • int
    • bool
    • string
    • Vector2
    • Vector3
    • List<int>
    • List<float>
    • List<double>
    • List<string>
    • List<bool>

    Saving has never been easier to do with Auto-Magic. Weather you are new to unity or have been developing for years, Auto-Magic will make a great addition to your library for each project you do.

    If you have any questions please feel free to email me @ drobinson@taptoongames.com


    TL;DR = Make static variables, make 1 call to save and load variables, never write another save script EVER!
     
  2. BokuDev

    BokuDev

    Joined:
    Jan 2, 2014
    Posts:
    81
    This looks really good! I am making a 3D collect-a-thon co-op platformer, and it definitely needs to have saving and loading!
     
  3. DexRobinson

    DexRobinson

    Joined:
    Jul 26, 2011
    Posts:
    594
    As most games do lol. For you game do you have each person on the same machine or is it networked?
     
  4. DexRobinson

    DexRobinson

    Joined:
    Jul 26, 2011
    Posts:
    594
    I have been adding new variable types into Auto-Magic. I now have List<Vector3> as a new type to celebrate adding this I decided to make a cool little example...

    To show how easy it is to use this system, I made a Minecraft example. In it I built a cube of 20x20x20 then I clicked on some cubes to remove them, saved the scene and then loaded it after restarting the editor and presto in under 100 lines of code I had a whole Minecraft world. It's hard to tell that I actually loaded the scene since the images are the same lol.

    First I build the level...
    $mine1.jpg


    Next I destroy some cubes...
    $mine2.jpg


    Finally I load the level back to it's original form...
    $mine2.jpg

    And finally he is all the code you need to save all this information(I had to build a lot of code to get the actual world to be generated)

    Cube Code
    Code (csharp):
    1.  
    2. // is the cube alive?
    3.     public bool isDead;
    4.  
    5.     // used when loading the scene
    6.     public void CheckIfDead() {
    7.         if (isDead)
    8.         {
    9.             gameObject.SetActive(false);
    10.         }
    11.     }
    12.  
    13.     // destory cube
    14.     void OnMouseDown()
    15.     {
    16.         if (!isDead)
    17.         {
    18.             isDead = true;
    19.             gameObject.SetActive(false);
    20.         }
    21.     }
    22.  
    World Code
    Code (csharp):
    1.  
    2. // just a simple cube with the cube.cs script
    3.     public GameObject cube;
    4.  
    5.     // holds all the cube.cs objects
    6.     private List<Cube> cubes = new List<Cube>();
    7.     // local list of positions...not needed but makes for faster code I think...
    8.     private List<Vector3> savedPositions = new List<Vector3>();
    9.     // used to make sure we don't create two worlds...
    10.     private bool worldCreated;
    11.  
    12.     void OnGUI()
    13.     {
    14.         if (GUILayout.Button("Create"))
    15.         {
    16.             if(!worldCreated)
    17.                 CreateWorld();
    18.         }
    19.  
    20.         if (GUILayout.Button("Save"))
    21.         {
    22.             SavePositions();
    23.         }
    24.  
    25.         if (GUILayout.Button("Load"))
    26.         {
    27.             LoadPositions();
    28.         }
    29.     }
    30.  
    31.     void CreateWorld()
    32.     {
    33.         worldCreated = true;
    34.         for(int x = 0; x < 20; x++)
    35.         {
    36.             for (int y = 0; y < 20; y++)
    37.             {
    38.                 for (int z = 0; z < 20; z++)
    39.                 {
    40.                     GameObject initCube = Instantiate(cube, new Vector3(x, y, z), Quaternion.identity) as GameObject;
    41.                     savedPositions.Add(new Vector3(x, y, z));
    42.                     cubes.Add(initCube.GetComponent<Cube>());
    43.                 }
    44.             }
    45.         }
    46.     }
    47.  
    48.     void LoadPositions()
    49.     {
    50.         if (!worldCreated)
    51.         {
    52.             worldCreated = true;
    53.  
    54.             // call to load...this could be done in Start() but I put it here instead...
    55.             SavedVariables.Load();
    56.             // copy the saved list into the local list
    57.             savedPositions = Variables.v3List;
    58.  
    59.             // spawn the cubes and assign them the the local lists...
    60.             for (int i = 0; i < savedPositions.Count; i++)
    61.             {
    62.                 GameObject initCube = Instantiate(cube, savedPositions[i], Quaternion.identity) as GameObject;
    63.                 cubes.Add(initCube.GetComponent<Cube>());
    64.  
    65.                 initCube.GetComponent<Cube>().isDead = Variables.isCubeAlive[i];
    66.                 initCube.GetComponent<Cube>().CheckIfDead();
    67.             }
    68.         }
    69.     }
    70.  
    71.     void SavePositions()
    72.     {
    73.         // copy the local list to the saved lists...
    74.         Variables.v3List = savedPositions;
    75.         List<bool> localList = new List<bool>();
    76.         // run through and make a local copy of all the cubes still alive...
    77.         for (int i = 0; i < savedPositions.Count; i++)
    78.         {
    79.             localList.Add(cubes[i].isDead);
    80.         }
    81.  
    82.         Variables.isCubeAlive = localList;
    83.  
    84.         // save...
    85.         SavedVariables.SaveVariables();
    86.     }
    87.  
     
    Last edited: Mar 26, 2014
  5. BokuDev

    BokuDev

    Joined:
    Jan 2, 2014
    Posts:
    81
    4 player local co-op in split-screen, and I'm looking into help for networking/online co-op, at this moment. I am interested in buying this as well.
     
  6. DexRobinson

    DexRobinson

    Joined:
    Jul 26, 2011
    Posts:
    594
    Awesome, sad to say it's still being reviewed by the Asset Store team. It will be $15. As far as your game, it's pretty simple to setup for local co-op.

    Code (csharp):
    1.  
    2. public static class Variables {
    3.     public static float player1Experience = 0;
    4.     public static float player2Experience = 0;
    5.     public static float player3Experience = 0;
    6.     public static float player4Experience = 0;
    7.  
    8.     public static float player1Level = 1;
    9.     public static float player2Level = 1;
    10.     public static float player3Level = 1;
    11.     public static float player4Level = 1;
    12.  
    13. }
    14.  
    Then you would just make your call to increase experience or gold, or whatever you want to save. Using...

    Code (csharp):
    1.  
    2. SavedVariables.SaveVariables();
    3.  
    You can even save under multiple files just by adding an index number inside of SaveVariables(). Once it's out and if you get it let me know how it is working for you.
     
  7. DexRobinson

    DexRobinson

    Joined:
    Jul 26, 2011
    Posts:
    594
    This is now released in the asset store.

    Link to asset store: Auto Magic