Search Unity

How to save the player progress in the game?

Discussion in 'Scripting' started by Giannigiardinelli, Nov 16, 2016.

  1. Giannigiardinelli

    Giannigiardinelli

    Joined:
    May 8, 2014
    Posts:
    189
    Hello,

    After several searches on the backup of the game, I could see the number of questions asked on the forums to understand the functionality or the videos. After several days of research I have more hair on my head.

    I find that there is a big shortage on the Asset Store about saving a project.

    I need to be helped at GameObject level.

    How do I save my gameobject "Disable" that found in the Hierarchy of "Canvas" "UI"?

    I would like to know how to activate the gameObject and the backup, for which the player loads the game it finds the gameObject Activate



    thank you in advance
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,727
    The process is generally called serialization and deserialization. Once the desired parts of the game have been serialized (into a string), then you write it to a storage spot, perhaps with PlayerPrefs or perhaps writing it to a file.

    It is a fairly non-trivial part of game engineering, but it can be approached one tiny part at a time. Try saving a single binary value and restoring it when the player reloads. From there you will get a feel for what you may ultimately be facing.

    Properly managed, it can be a breeze, but the key is learning HOW to properly manage it for the context you need it in.
     
  3. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    The noob way is to use PlayerPrefs. However they are slow, clumsy to organize and very easy for inexperienced cheaters to change. When I say "inexperienced cheaters", I'm referring to people who're looking for a quick text file to change and don't know much about game hacking. More advanced cheaters will use memory hacks (such as Cheat Engine), and/or will be able to decode the serizalized file.

    The better way is to use serialization. I use this script to serialize a dictionary:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4. using System.Runtime.Serialization.Formatters.Binary;
    5. using System.Runtime.Serialization;
    6. using System;
    7. using System.Collections.Generic;
    8.  
    9. //this is SaveInfoNewer.cs
    10. //this script saves and loads all the info we want
    11. public class SaveInfoNewer : MonoBehaviour
    12. {
    13.     public SlotLoader SlotLoader;
    14.     //data is what is finally saved
    15.     public Dictionary<string, int> data;
    16.  
    17.     void Awake()
    18.     {
    19.         //LoadUpgradeData();
    20.         LoadData();
    21.         //WARNING! data.Clear() deletes EVERYTHING
    22.         //data.Clear();
    23.         //SaveData();
    24.     }
    25.  
    26.     public void LoadData()
    27.     {
    28.         //this loads the data
    29.         data = SaveInfoNewer.DeserializeData<Dictionary<string, int>>("PleaseWork.save");
    30.     }
    31.  
    32.     public void SaveData()
    33.     {
    34.         //this saves the data
    35.         SaveInfoNewer.SerializeData(data, "PleaseWork.save");
    36.     }
    37.     public static void SerializeData<T>(T data, string path)
    38.     {
    39.         //this is just magic to save data.
    40.         //if you're interested read up on serialization
    41.         FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
    42.         BinaryFormatter formatter = new BinaryFormatter();
    43.         try
    44.         {
    45.             formatter.Serialize(fs, data);
    46.             Debug.Log("Data written to " + path + " @ " + DateTime.Now.ToShortTimeString());
    47.         }
    48.         catch (SerializationException e)
    49.         {
    50.             Debug.LogError(e.Message);
    51.         }
    52.         finally
    53.         {
    54.             fs.Close();
    55.         }
    56.     }
    57.  
    58.     public static T DeserializeData<T>(string path)
    59.     {
    60.         //this is the magic that deserializes the data so we can load it
    61.         T data = default(T);
    62.  
    63.         if (File.Exists(path))
    64.         {
    65.             FileStream fs = new FileStream(path, FileMode.Open);
    66.             try
    67.             {
    68.                 BinaryFormatter formatter = new BinaryFormatter();
    69.                 data = (T)formatter.Deserialize(fs);
    70.                 Debug.Log("Data read from " + path);
    71.             }
    72.             catch (SerializationException e)
    73.             {
    74.                 Debug.LogError(e.Message);
    75.             }
    76.             finally
    77.             {
    78.                 fs.Close();
    79.             }
    80.         }
    81.         return data;
    82.     }
    83. }
    84.  
    It might not be perfect for your game and it's certainly not the most secure system, but it is fully functional and it's a good starting point.

    With this script, to save data, you'll do this:
    Code (CSharp):
    1. public SaveInfoNewer SaveInfo;
    2.  
    3. void SaveAKey()
    4. {
    5.     SaveInfo.data["YourKeyName"] = 1234;
    6. }
    To pull data, use this:
    Code (CSharp):
    1. public SaveInfoNewer SaveInfo;
    2.  
    3. void LoadAKey()
    4. {
    5.     YourVariable = SaveInfo.data["YourKeyName"];
    6. }
    That should help save you some time :)
     
    Last edited: Jun 15, 2017
  4. Giannigiardinelli

    Giannigiardinelli

    Joined:
    May 8, 2014
    Posts:
    189
    Thank you for your help! I will see what I can do with this script! If I have difficulties to start the script at the level of my gameObject I would come to appeal to you.
     
  5. riyad77

    riyad77

    Joined:
    Sep 4, 2017
    Posts:
    1
  6. DLX_Studios

    DLX_Studios

    Joined:
    Apr 8, 2019
    Posts:
    4
    So can we use this to save a full scene???, i'm making a game where it only has two scenes in it, one is the menu the other is the game itself so i need to save the game scene.

    kind regards, Ben.
     
  7. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Generally you don't save game objects states directly. You save common game state like player position, health, monsters killed, items collected. And then you restore whole level based on that data.
     
  8. DLX_Studios

    DLX_Studios

    Joined:
    Apr 8, 2019
    Posts:
    4
    But how to save the entire seen to load after a while?
     
  9. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
  10. DLX_Studios

    DLX_Studios

    Joined:
    Apr 8, 2019
    Posts:
    4
    is there a free one?
     
  11. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,748
    Idk, maybe. It's a fairly complex task to write whole scene serializer in Unity, I think the probability of finding good free one is low.
     
  12. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You're very likely thinking about this in the wrong way if you think you need to save the entire scene. Figure out the minimum data you need to save in order to recreate that scene, and write code which both stores that information and loads that information to recreate the scene.

    For example, you have an NPC monster to store. Well you don't need to store the object structure and every variable. You likely just need to store an identifier for its prefab, its position and rotation, and a few other key bits of information. You write some code that serializes that information to whatever format you choose, and you run that code against all the NPC monsters. You take a similar approach for every other object in your scene which you need to recreate to restore the scene.
     
    Kurt-Dekker and Ryiah like this.
  13. Itachibaka

    Itachibaka

    Joined:
    Apr 18, 2022
    Posts:
    1
    I am just Confused ok
     
  14. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,481
    It's confusing why you're necroing a thread that's 6 years old to say this. This purpose of the forum is to help people with problems, it's not a chat room. ;)
     
    alex11paulescu and Kurt-Dekker like this.