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 Storing Creature Data

Discussion in 'Scripting' started by vcinardo, Jul 12, 2023.

  1. vcinardo

    vcinardo

    Joined:
    Dec 23, 2019
    Posts:
    27
    How can I store creature data in a way that is more streamlined? What is a common way to do it? I have creature data in the following format.

    Code (JSON):
    1. {
    2.   "name": "Wolf",
    3.   "id": 0,
    4.   "age": 0,
    5.   "maxAge": 12,
    6.   "diet": 2,
    7.   "eatRate": 80.0,
    8.   "totalMeat": 100.0,
    9.   "hunger": 0.0,
    10.   "thirst": 16000.0,
    11.   "sleep": 16000.0,
    12.   "maxHunger": 16000,
    13.   "maxThirst": 16000,
    14.   "maxSleep": 16000,
    15.   "hungerRate": 0.01,
    16.   "thirstRate": 0.03,
    17.   "sleepRate": 0.03,
    18.   "hp": 12.0,
    19.   "maxHp": 12.0,
    20.   "hpRate": 0.04,
    21.   "stamina": 40.0,
    22.   "maxStamina": 40.0,
    23.   "staminaRate": 0.01,
    24.   "attackTime": 120.0,
    25.   "attackDamage": 5.0,
    26.   "moveTime": 130.0,
    27.  
    28.   "speed": 20,
    29.   "maxSpeed": 100,
    30.   "speedExperience": 0,
    31.   "maxSpeedExperience": 0,
    32.  
    33.   "strength": 20,
    34.   "maxStrength": 100,
    35.   "strengthExperience": 0,
    36.   "maxStrengthExperience": 0,
    37.  
    38.   "agiltiy": 20,
    39.   "maxAgility": 100,
    40.   "agilityExperience": 0,
    41.   "maxAgilityExperience": 0
    42. }
    I need to add many other creatures beside a wolf. Also, the ways I am retrieving this data is almost like I am trying to mimic sql by storing the names of creatures in an array, whose index corresponds with the creature id. I am able to retrieve this json with part of the path I get from that array. I am starting to get the feeling I need to use a database, since, adding new creatures is a real pain. Any recccomendations?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Use whatever method works for you. There are tons of options. ScriptableObjects, json files, databases. Whatever you choose.

    There are also many methods for accessing these items. I would suggest if you're doing single files or scriptableObjects, that Unity's Addressable system might be something you want to look into.
     
    vcinardo likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,494
    What exactly do you mean by that? What's the actual issue with using json?

    What does that even mean? sql is a query language for relational databases. I don't see any relations here, just a flat list of attributes. So what kind of "queries" do you have in mind? What's even the usecase of those creatures? What's the ballpark we talk about? hundreds? thousands? millions?

    Hmm what exactly is the pain here? When you say you "add" a creature, does that involve specifying / generating random values for those attributes? You could creat them in a text editor by simply copying a template and inser the values. You could use a spreadsheet and write a small "importer" for CSV. You could create them as scriptable objects inside Unity. Since you can simply duplicate an existing object, all you have to do is rename the file and change the values as you need them.
     
    vcinardo, Munchy2007 and Chubzdoomer like this.
  4. vcinardo

    vcinardo

    Joined:
    Dec 23, 2019
    Posts:
    27
    I think that's the main issue that has me stuck. I've been trying to do everything in my head rather than write it down. So I would say the use case is the following:

    Every creature in the game has similar stats, like a lot of older rpgs (think oblivion, the stats above are not done). I might actually separate the stats from plain creature information now that I think about it.

    Code (JSON):
    1. {
    2.   "name": "Wolf",
    3.   "id": 0,
    4.   "age": 0,
    5.   "maxAge": 12,
    6.   "diet": 2,
    7.   "eatRate": 80.0,
    8.   "totalMeat": 100.0,
    9.   "hunger": 0.0,
    10.   "thirst": 16000.0,
    11.   "sleep": 16000.0,
    12.   "maxHunger": 16000,
    13.   "maxThirst": 16000,
    14.   "maxSleep": 16000,
    15.   "hungerRate": 0.01,
    16.   "thirstRate": 0.03,
    17.   "sleepRate": 0.03,
    18.   "hp": 12.0,
    19.   "maxHp": 12.0,
    20.   "hpRate": 0.04
    21. }
    Code (JSON):
    1. {
    2.   "speed": 20,
    3.   "maxSpeed": 100,
    4.   "speedExperience": 0,
    5.   "maxSpeedExperience": 0,
    6.  
    7.  
    8.   "strength": 20,
    9.   "maxStrength": 100,
    10.   "strengthExperience": 0,
    11.   "maxStrengthExperience": 0,
    12.  
    13.  
    14.   "agiltiy": 20,
    15.   "maxAgility": 100,
    16.   "agilityExperience": 0,
    17.   "maxAgilityExperience": 0
    18. ...[and so on]
    19. }
    Different creatures have certain skills. Examples: Humanoids have dodge and block. Wolves have dodge only. Rock golem has block only. Maybe when it comes to creatures that can't do a certain skill, I can leave that skill as -1, so the AI will never choose to block.

    Code (JSON):
    1. {
    2.   "dodge": 20,
    3.   "maxDodge": 90,
    4.   "dodgeExperience": 0,
    5.   "maxDodgeExperience": 0
    6. }
    The last bit of data not included here is some sprite information used for positioning and scaling the sprite, but you get the point.

    So I guess I really could store all this base creature information in one json or csv, and pull it from disk as an array/list ordered by ID number. I do randomize some data on spawned creatures to add a bit of variation. I also plan on having 100's of concurrent creatures. So if I save game all the creatures can be saved in a different json. Does this make sense?
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Well, how do we mix and match behaviour in Unity? Components.

    So if you want to mix and match behaviour, stats, etc, you take the same component approach. I don't mean literally Unity components, though, you can do this in the pure C# realm with a base class/interface and derived types that make up a collection of stats/attributes. You want a particular stat, you run down the list for a stat of the right type and grab it.

    And you can serialise these different types into a scriptable object or other Unity assets with the SerializeReference attribute, though that requires custom inspector work to be useable.

    That's one method, of course.