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 Hierarchy game data saving in file

Discussion in 'Prefabs' started by bngames01, Oct 4, 2022.

  1. bngames01

    bngames01

    Joined:
    Apr 26, 2021
    Posts:
    28
    Hello,

    I have three hierarchy levels.
    Is there a “best practice” to load/save game data from/into file and populate these data structure in the UI lists? Each hierarchy is on a separate canvas.

    e.g.
    Categories
    —- Sub1Categories
    ———Sub2Categories

    I.
    Store these via Scriptable objects and save them with Json and load and populate ui

    II.
    Store them some ways into separate dictionaries.
    Without any scriptable objects.

    III.
    MySQL in Unity, without remote server?

    What do you think, which is more managable if you want to add items to each hierarchy level?
     
  2. bloodthirst69

    bloodthirst69

    Joined:
    Oct 1, 2017
    Posts:
    28
    the question is very broad and you will need to add a lot of context to be able to get a good answer.
    What does your game use to persist the save data ?
    Do you reference UnityEngine.Objects in your game data ?
    Why is the UI structure relevant here ? isn't getting the data the most important step ?
    I am gonna go ahead and just assume that what you ask for is how to store data that has a "tree" structure to it.
    The easiest way imo is to have something like :

    Code (CSharp):
    1.  
    2. public class NodeRef
    3. {
    4.    public int nodeIndex;
    5.    public string nodeData;
    6.    public int[] subNodesIndicies;
    7. }
    8.  
    9. public class GameSaveData
    10. {
    11.    public NodeRef[] allNodes;
    12. }
    13.  
    with this kinda structure you avoid serializing deep branches and you simply save all nodes in a flat array and then just refer to the sub nodes by their index.
    So in your example the categories would be saved as JSON you would get something like :
    {
    allNodes :
    [
    { nodeIndex : 0 , nodeData : "Categories" , subNodesIndicies : [1] },
    { nodeIndex : 1 , nodeData : "Sub1Categories" , subNodesIndicies : [2] },
    { nodeIndex : 2 , nodeData : "Sub2Categories" , subNodesIndicies : [] }
    ]
    }
     
  3. bngames01

    bngames01

    Joined:
    Apr 26, 2021
    Posts:
    28
    Hello,

    thanks, I dig into scriptable objects it is really helpful, and are great managable from editor.