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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Database of Creatures

Discussion in 'Scripting' started by DeeJayVee, Dec 17, 2018.

  1. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    I've been wondering what a good way to go about having a database of creatures similar to Pokemon.

    I thought about using IO and reading from a text file and having the text file written out like this;

    CreatureName;Type1;Type2;ExpCurveType;

    but from what I understand reading from a text file isn't as simple as finding certain lines, so I got stumped on how to reference those lines.

    I'm not looking for a solution per say, more something I can read and learn from on a particular topic that fits.

    How would you go about adding/creating/referencing say 150 creatures and their types, exp curves, things like that?

    Is reading from a textfile a good idea for this?
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    Not run-time. If you don't need searching, then ScriptableObjects are great for persistent asset-datastore. If you need searching I think some nosql solution is the best. They are usually performant for read only.
    Now, you can edit your game data in spreadsheets and then export them into csv file, but I think it's the best if you convert it at build time to something more native to Unity (SO or DB or something).
     
    DeeJayVee likes this.
  3. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    Oh actually, yes I've seen Scriptable Objects used for items and things, I'd forgotten what they were called. That may be the way to go. I'm going to test, thanks man
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    For 150 objects you can just load the whole thing from your text file into memory at runtime. Use a dictionary or list to store the data. All 150 creatures will take up less memory then a single texture.

    If you are really lazy (which is a good characteristic to have as a programmer) you can take advantage of Unity's serialization with JsonUtility to read and write the text file for you in the first place.
     
    DeeJayVee likes this.
  5. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    I'm pretty unfamiliar with JsonUtility, so I'll read up on it.

    I do like the idea of a textfile, and a loadup at runtime. I'm currently messing around on visual studio doing Scriptable Objects but I'm not sure which I prefer. That's ok though, I'm just practicing, it isn't for anything in particular, just something I found interesting. So I can keep playing with both ideas no problem. Thanks man
     
  6. DeeJayVee

    DeeJayVee

    Joined:
    Jan 2, 2015
    Posts:
    121
    I was able to make a Database and Unpacker with System.IO and a Text File. At least, i was able to separate the data into a nice list. I'm unsure if this is really a way someone would do it or how I will use the data in a List like this, but it does work.

    I'll post what I've done and if any of you lads wanna rip it apart, by all means, lemme know what I could of done better or differently.

    The Text File is laid out like this

    Code (CSharp):
    1. CreatureID;CreatureName;Type1;Type2;ExpCurveType;
    2. CreatureID;CreatureName;Type1;Type2;ExpCurveType;


    all separated by semi colons it is hardcoded to have 5 attributes, i'm not sure if I can change that or how to at this point.

    Two Scripts: A serialized creature Attribute script and the Unpacker.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6. public class Creature{
    7.  
    8.    public int creatureID;
    9.  
    10.    public string creatureName;
    11.  
    12.    public string type1;
    13.  
    14.    public string type2;
    15.  
    16.    public  int expCurveType;
    17.  
    18.  
    19.  
    20.     public Creature(string ID, string name, string t1, string t2, string expCurve)
    21.     {
    22.         this.creatureID = int.Parse(ID);
    23.         this.creatureName = name;
    24.         this.type1 = t1;
    25.         this.type2 = t2;
    26.         this.expCurveType = int.Parse(expCurve);
    27.     }
    28. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5.  
    6.  
    7. public class CDBUnpacker : MonoBehaviour {
    8.  
    9.     string assetPath, fullDB;
    10.     string[] lines;
    11.  
    12.  
    13.     [SerializeField]
    14.     List<Creature> creatureDatabase = new List<Creature>();
    15.  
    16.  
    17.  
    18.  
    19.  
    20.     // Use this for initialization
    21.     void Start ()
    22.     {
    23.         assetPath = "Assets/CreatureDatabase.txt";
    24.         LoadDatabase();
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update () {
    29.      
    30.     }
    31.  
    32.  
    33.  
    34.  
    35.     void LoadDatabase()
    36.     {
    37.         StreamReader reader = new StreamReader(assetPath);
    38.         fullDB = reader.ReadToEnd();
    39.         lines = fullDB.Split(';');
    40.         int count = 0;
    41.         string[] tempMethodHold = new string[5];
    42.  
    43.    
    44.  
    45.      
    46.         foreach( string s in lines)
    47.         {
    48.        
    49.             if(s != null)
    50.             {
    51.                 tempMethodHold[count] = s;
    52.                 Debug.Log(tempMethodHold[count]);
    53.                 count++;
    54.                 if (count == 5)
    55.                 {
    56.                     AddCreatureToList(tempMethodHold[0], tempMethodHold[1], tempMethodHold[2], tempMethodHold[3], tempMethodHold[4]);
    57.                     count = 0;
    58.                 }
    59.             }
    60.         }
    61.  
    62.      
    63.  
    64.     }
    65.  
    66.     void AddCreatureToList(string a, string b, string c, string d, string e)
    67.     {
    68.  
    69.         Creature creature = new Creature(a, b, c, d, e);
    70.         creatureDatabase.Add(creature);
    71.      
    72.  
    73.     }
    74. }
    I'm happy for now, that was a lot of fun.
     
    kingfelix_unity likes this.