Search Unity

Looking for some advice on formatting data for saving

Discussion in 'Scripting' started by Afropenguinn, Mar 3, 2019.

  1. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    I'm making a management game and that keeps track of every product the player makes, as well as products made by AI competitors. Each entry is fairly small, so it's not an insane amount of data, not enough to warrant a true database I'd think. I'm more so just looking for some advice, since I'm sure most of you have some experience with save systems. Some thoughts I had:

    Right now I have data directly referencing other data by class, for instance a product can have a genre, and that genre is a reference to a genre class which contains information on that genre. So in pseudo-code, something like:
    Code (CSharp):
    1. Genre Definition:
    2. string name
    3. string description
    4. float popularity
    5.  
    6. Product Definition:
    7. string name
    8. string description
    9. Genre genre
    10. int unitsSold
    11. float profit
    12. float cost
    But I was wondering if, for the purposes of saving, it would make more sense to give each product and genre a guid, and then they could reference each other that way. So instead it would be:

    Code (CSharp):
    1. Genre Definition:
    2. string guid
    3. string name
    4. string description
    5. float popularity
    6.  
    7. Product Definition:
    8. string guid
    9. string name
    10. string description
    11. string genreGuid
    12. Genre genre = GetGenreByGuid(genreGuid)
    13. int unitsSold
    14. float profit
    15. float cost
    Would that be a better option than serializing the whole class with a direct reference?
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Put players, products etc., data into serializable lists. Then simply use JsonUtility to save load data.

    For reference you can use simply index int, to the list. You don't need GUID.
     
  3. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    Any reason why you'd recommend JsonUtility over binary serialization?
     
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Because is dead simple to implement and easy to debug.
     
  5. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    I'd still would go with some UID in your data, even if it is json, if you search your json for something and would have double content, you would lose the relation.
     
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    UID does not guarantee in general uniqueness, even is in general highly unlikely to get duplicate.
    If wanting having unique ID, incremental index is more than enough, for most cases.

    Since serializing lists, save and load will read / write data in same order. So UID is irrelevant for that purpose.
    Unless you do some modding items. But even then, you can always use enumerator instead string UID, for Identifying items for example. See minecraft blocks for example. They have assigned integer index, not string UID.

    Also, since UID is additional string, which means, would need be dealt at runtime, which imposes extra garbage collection. That beside saving and loading.
     
  7. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    Well, it also depends on the data. For some data I probably will use a guid, and others I will use the index, specifically because I want to allow data modding (no new code, just additional data). For instance, say a user adds a new product type to the game called "Graphics Engine" and a new employee skill called "Graphics Programming". They want the quality of the product to be based on the employee's skill in "Graphics Programming", so they need some way to reference that skill in the data for "Graphics Engine". Obviously we don't want to depend on mod order, so the id must be assigned by the user. You certainly still could use an int, but that's not very user friendly for modders, and opens itself up to conflicts if other modders use the same id. using a string in that case is nice, since modders could use a slug like "MODNAME_guid".

    Though for something like products, using the index is perfect. Since products can't be added in by modding, it is data created only during gameplay, so it will always be in the same order and it's easy to keep track of what the next index should be.

    Correct me if I'm wrong, of course, but that's the best way I could think of to handle it.
     
  8. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    I can not say if this is best way, since I don't know. It is project specific. But surely should work well, as per what you describe.
     
  9. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    I wonder how you guys would update a json file with a new one if you don't have any UID. Of course, if you have a player json which just holds stuff, that is not going to change, you can always just iterate through. But if you like have a player which gets is data form a server or new version and you want to update a name from an item for example, you not gonna get around UIDs, which will be unique if you set them unique. For example a check of ID and the UTC code. Just my thoughts how I handled a game with lots of questions being modified in a backend and then pushed to the user with the need to update, add or delete json data on the fly.
     
  10. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,778
    Simply override.

    If you don't need server flexibility, simply using enumerator, with assigning index to item, is more what you need.
    For example,
    Code (CSharp):
    1. enum Items
    2. {
    3. pen = 0,
    4. hat = 10,
    5. boots = 11,
    6. sword = 100,
    7. pistol = 200,
    8. colt = 201
    9. ... etc
    10. }
    etc.
    On the server, you got corresponding IDs to that.
    But mind again, this approach does not assume, OP requires server, nor flexibility of modding.
    Just keeping simple.

    Otherwise, yes you may need some stringified UID, or ensuring unique index, if above approach is not sufficient.