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

Question The best way to save data

Discussion in 'Scripting' started by NyamaNyama, Sep 16, 2023.

  1. NyamaNyama

    NyamaNyama

    Joined:
    Jul 10, 2023
    Posts:
    8
    I recently started studying unity and I had a problem with saving game data, such as player position, inventory, etc. YouTube is full of videos where people use scriptable object to save the game. However, I have heard that scriptable objects are not really designed for this, and besides, my scriptable objects do not save data unless I click on them in the inspector. Therefore, I would like to know what is best to use for saving in the game.
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Scriptable objects are not at all intended for saving data between runtime sessions. While changes to their data persist between editor playmode sessions, they do not persist during built sessions.

    Save games always involve writing (serialising runtime data) data to disk, and reading that at a later point in time.

    You can use scriptable objects to encapsulate save data, making it easy to reference, alongside easily editable. But said data will always need to be written to disk in some form.

    Also important to note that you can't serialise out complete Unity objects, or references to them. You need to work with simpler forms of data supported by whatever serialisation library you use.
     
    Bunny83, NyamaNyama and Kurt-Dekker like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Load/Save steps:

    https://forum.unity.com/threads/save-system-questions.930366/#post-6087384

    An excellent discussion of loading/saving in Unity3D by Xarbrough:

    https://forum.unity.com/threads/save-system.1232301/#post-7872586

    Loading/Saving ScriptableObjects by a proxy identifier such as name:

    https://forum.unity.com/threads/use...lds-in-editor-and-build.1327059/#post-8394573

    When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls
    new
    to make one, it cannot make the native engine portion of the object.

    Instead you must first create the MonoBehaviour using AddComponent<T>() on a GameObject instance, or use ScriptableObject.CreateInstance<T>() to make your SO, then use the appropriate JSON "populate object" call to fill in its public fields.

    If you want to use PlayerPrefs to save your game, it's always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:

    https://gist.github.com/kurtdekker/7db0500da01c3eb2a7ac8040198ce7f6

    Do not use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

    https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide
     
    NyamaNyama likes this.
  4. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    521
    I'll suggest that you start by writing and reading a simple test and work from there. I use a class that is intended to write and read game settings. It can read/write from the device but also from an API running on a server in the cloud. Get "hello world" working and then apply that logic to your actual settings.
     
    NyamaNyama and Kurt-Dekker like this.
  5. NyamaNyama

    NyamaNyama

    Joined:
    Jul 10, 2023
    Posts:
    8
    Thank you all for deciding to answer my question. I will try to implement saving and loading my game using BinaryWriter and BinaryReader. I hope this is a good solution and I will succeed.
     
  6. SeerSucker69

    SeerSucker69

    Joined:
    Mar 6, 2021
    Posts:
    65
    Check out "Easy Save" in the Unity assets store - it will save you a lot of hours of work.
     
    NyamaNyama likes this.