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. Dismiss Notice

Save and Load system Save

Discussion in 'Getting Started' started by DENz_LORD, Jul 27, 2023.

  1. DENz_LORD

    DENz_LORD

    Joined:
    Feb 22, 2022
    Posts:
    2
    Good afternoon, dear colleagues!
    Faced with this problem, so we are creating a first-person adventure with a small team. The game is almost done, but recently the lead programmer left us and then I discovered that there is simply no system for saving and loading the game.
    I am engaged in level design and plot content, I know programming only basic. Please tell me some simple preservation system, like for example in the atomic heart. That is, in fact, it is necessary that the progress is saved when interacting with the subject, and loading from saving occurs by pressing a button in the menu.
    Please help me! Well, or give a link to some video on YouTube
     
  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    431
    You don't give any specifics about what you want to store. So we don't know what you want to put into the file structure. Most time you just save data in the file, you can save it as plain text or some structure like XML or JSON. If you want structure you need serialization and deserialization. There is no point to store everything on the scene, so you must give more details.
    Code (CSharp):
    1. using System.IO;
    2.  
    3. public class Campaign
    4. {
    5.     public void SaveGame(string data)
    6.     {
    7.         File.WriteAllText("MyGame.txt", data);
    8.     }
    9.  
    10.     public string LoadGame()
    11.     {
    12.         if (File.Exists("MyGame.txt"))
    13.         {
    14.             return File.ReadAllText("MyGame.txt");
    15.         }
    16.         else
    17.         {
    18.             return "No saved data.";
    19.         }
    20.     }
    21. }
     
    Last edited: Jul 27, 2023
  3. DENz_LORD

    DENz_LORD

    Joined:
    Feb 22, 2022
    Posts:
    2
    I need to save the player's current position in the scene, active and inactive items in the player's inventory, activated and deactivated triggers, the amount of health and also closed open doors in the scene. There are also objects in the scene such as keys and notes that the player managed and did not have time to look at.. Something like this
     
  4. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    431
    Most games have GameManager or SceneManager. They are created in a singleton pattern with a static instance of self inside, thanks to that they can be called from any script. So the scripts like PlayerController, Inventory, or Triggers can report the status. That status is what you know how to translate from file to object and back.

    The next topic that will be handy is using some kind of structure. One of the popular formats is JSON, and Unity has JSONUtility library that helps you to serialize data (from text to object) and deserialize data (from object to text). And loading and saving text is simple like the above example I had posted earlier.

    JSONUtility requires a class that has [System.Serializable] before itself.

    I hope these 3 steps help you in your future search.
     
  5. TheNullReference

    TheNullReference

    Joined:
    Nov 30, 2018
    Posts:
    222
    You need to Serialize / DeSerialize the data for each point of data you want saved and loaded. Unfortunately unity types and monobehaviours aren't easily Serializable so you essentially have to create a new class for each class you want to serialize.

    Code (csharp):
    1. using UnityEngine;
    2. using System.IO;
    3. using System.Runtime.Serialization.Formatters.Binary;
    4.  
    5. public class Player : MonoBehaviour
    6. {
    7.     private void OnEnable()
    8.     {
    9.         LoadPlayer();
    10.     }
    11.  
    12.     private void OnDisable()
    13.     {
    14.         SavePlayer();
    15.     }
    16.  
    17.     void SavePlayer()
    18.     {
    19.         BinaryFormatter formatter = new BinaryFormatter();
    20.         string path = Application.persistentDataPath + "/player.save";
    21.         FileStream stream = new FileStream(path, FileMode.Create);
    22.  
    23.         PlayerData data = new PlayerData(transform.position);
    24.  
    25.         formatter.Serialize(stream, data);
    26.         stream.Close();
    27.     }
    28.  
    29.     void LoadPlayer()
    30.     {
    31.         string path = Application.persistentDataPath + "/player.save";
    32.         if (File.Exists(path))
    33.         {
    34.             BinaryFormatter formatter = new BinaryFormatter();
    35.             FileStream stream = new FileStream(path, FileMode.Open);
    36.  
    37.             PlayerData data = formatter.Deserialize(stream) as PlayerData;
    38.             stream.Close();
    39.  
    40.             transform.position = new Vector3(data.position[0], data.position[1], data.position[2]);
    41.         }
    42.         else
    43.         {
    44.             Debug.LogError("Save file not found in " + path);
    45.         }
    46.     }
    47. }
    48.  
    49. [Serializable]
    50. public class PlayerData
    51. {
    52.     public float[] position;
    53.  
    54.     public PlayerData(Vector3 position)
    55.     {
    56.         this.position = new float[3];
    57.         this.position[0] = position.x;
    58.         this.position[1] = position.y;
    59.         this.position[2] = position.z;
    60.     }
    61. }
    62.  
    you could use txt, json, yaml, xml etc to store the data but then it's easy for a player to edit to give themselves unfair advantage. Binary Formatter is still possible to compromise but not human readable.