Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Best way to build a flexible Save/Load system (using addressables???)

Discussion in 'Scripting' started by RogueStargun, Sep 25, 2021.

  1. RogueStargun

    RogueStargun

    Joined:
    Aug 5, 2018
    Posts:
    296
    I've been running into a particular issue with my game, and it's quite a bit unclear what the best way to go about dealing with this is.

    I already have a save system that is capable of loading very basic string/numerical data like health, position, etc.

    What I'm trying to understand better is the proper way to deal with saving and loading more complex data. For example in my game there are the following:

    • The player can carry multiple guns which are essentially different game objects
    • when the game starts, I want to be able to load different player game objects, followed by different gun gameobjects (presumably at runtime?)
    • There can be N number of enemies which can also have different guns and items which I also want to be able to save and load.
    At the highest level, I want to be able to save and load data AND prefabs at the same granularity one would get in an RPG, essentially.

    For loading NPC gameobjects, and things like guns, it sounds like its no longer recommended to use Resources.Load, and instead use either asset bundles or addressables?

    I was thinking at the simplest level, I could initially start out with an "inventory database" prefab that holds a reference to every prefab that could possibly be put in the inventory. This prefab can then be attached to a manager monobehaviour, and on loading a saved game, inventory objects can be spawned by matching a string in the save file with an item in the database. This creates a sort of memory issue where every single inventory item is loaded from memory regardless of being used or not (this is a 3D VR game btw).

    Later, I can swap the backend for this database prefab for addressable assets if the need arises (???)...
     
    Last edited: Sep 25, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    I would say more complex data would need a load/save solution tailored to precisely what it needs in order to be recreated on demand from scratch from wherever you are in the game state.

    Keep in mind the more you load/save, the more code and test cases you must maintain and debug, so don't go too overboard until you're positive you need something saved.

    Here's my general notes for Load/Save steps:

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

    Don't 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

    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.