Search Unity

Serialization

Discussion in 'Scripting' started by synok, Sep 29, 2019.

  1. synok

    synok

    Joined:
    Jun 7, 2011
    Posts:
    126
    Hey everyone,

    I am working on something now where I want to implement a dialog system. Also checked into saving and loading game. But heres the thing. What should I include when serializing dialogs, or how should I handle it?

    I.e. The player talks to several NPCs in a town. Saves the game. The next day the player loads it up again and the game needs to keep track of not only inventory items, maybe temples finished / unfinished, which NPC have been talked to, how far they got into the conversation, etc.

    Is there ways to bring the serialization part to a minimum in this regard? And what is the most effective way for you?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Usually for dialog you have a dialogue tree. And each node in the tree can have an id assigned to it (a simple integer would work). So to save your NPC information you'd just have the id for the NPC (maybe a guid?) and the id for where in the dialogue tree you are. Talking all of 20 bytes of information (16 for a guid, and 4 for an integer), and that could be smaller if you used a more compact id for the NPC. For example we made a simple 'ShortUID' which is just 8 bytes:
    https://github.com/lordofduct/spacepuppy-unity-framework/blob/master/SpacepuppyBase/ShortUid.cs

    And of course, its editor:
    https://github.com/lordofduct/space...tor/Base/Inspectors/ShortUidPropertyDrawer.cs
     
  3. synok

    synok

    Joined:
    Jun 7, 2011
    Posts:
    126
    Thank you! I will check into it. Wouldnt just some form of integer serialization system work? For example, if dialog equals 1, say this. If it is 2, then say this instead. And just including those?
    But if you have plenty of NPCs I guess that will end up in a lot of serialization when saving the game.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    You could just have a single integer, sure. But usually for organization purposes it's nice to associate the dialogue tree with the NPC. That way when writing your dialogue trees you don't have to concern yourself with all the other dialogue trees that exist.
     
  5. Synpse

    Synpse

    Joined:
    Mar 19, 2018
    Posts:
    20
    You should keep track of everything and store everything you think is important, you should then pass all that data into a non-monobehavior class and it has to be marked as [System.Serializable].
    About the minimizing your "serialization footprint" (I just made that up), you can try to store, per example, all your inventory items in one single string, or in an array or list.
    In my opinion this will be a much cleaner thing to do but will obviously be a little more difficult.

    Another thing you can do is (and this is just my opinion and is obviously debatable):
    Use scriptable objects to easily organize, access and store your data, then take you data from all scriptable objects into a single file and then save that.

    You may also save the scriptable objects with a Json Utility but I'm not sure that is a good practice.

    As you can see you have tons of options. Just pick what you think is right for you.
     
    Last edited: Sep 30, 2019