Search Unity

Problems with having C# script communicate with JSON database

Discussion in '2D' started by Spoopy_, Sep 11, 2019 at 11:15 AM.

  1. Spoopy_

    Spoopy_

    Joined:
    May 22, 2019
    Posts:
    1
    As the title states, im having problems with having a c# script communicate with a JSON database. Its been extremely frustrating for me as ive communicated with the database in the same way before without it working. Ive also run my JSON through jsonlint and its completely valid.

    upload_2019-9-11_19-53-5.png

    Code (JavaScript):
    1. [
    2.   {
    3.     "id": 0,
    4.     "title": "Test",
    5.     "value": 6,
    6.     "stats": {
    7.       "damage": 1,
    8.       "defence": 1,
    9.       "vitality": 1
    10.     },
    11.     "description": "test",
    12.     "stackable": true,
    13.     "stacksize": 10,
    14.     "rarity": 10,
    15.     "craftable": true,
    16.     "mats":{
    17.       "id_1":3, "amount_1":2,
    18.       "id_2":1, "amount_2":1,
    19.       "id_3":2, "amount_3":1,
    20.       "id_4":-1, "amount_4":0
    21.     },
    22.     "slug": "test",
    23.     "type": "test"
    24.  
    25.   },
    26.  
    27.   {
    28.     "id": 1,
    29.     "title": "Test2",
    30.     "value": 10,
    31.     "stats": {
    32.       "damage": 2,
    33.       "defence": 2,
    34.       "vitality": 3
    35.    
    36.     },
    37.     "description": "test2",
    38.     "stackable": false,
    39.     "stacksize": 1,
    40.     "rarity": 1,
    41.     "craftable": false,
    42.     "mats":{
    43.       "id_1":-1, "amount_1":0,
    44.       "id_2":-1, "amount_2":0,
    45.       "id_3":-1, "amount_3":0,
    46.       "id_4":-1, "amount_4":0
    47.     },
    48.     "slug": "green",
    49.     "type": "test"
    50.   },
    51.  
    52.   {
    53.     "id": 2,
    54.     "title": "Test3",
    55.     "value": 10,
    56.     "stats": {
    57.       "damage": 2,
    58.       "defence": 2,
    59.       "vitality": 3
    60.     },
    61.     "description": "test2",
    62.     "stackable": false,
    63.     "stacksize": 1,
    64.     "rarity": 1,
    65.     "craftable": false,
    66.     "mats":{
    67.       "id_1":-1, "amount_1":0,
    68.       "id_2":-1, "amount_2":0,
    69.       "id_3":-1, "amount_3":0,
    70.       "id_4":-1, "amount_4":0
    71.     },
    72.     "slug": "blue",
    73.     "type": "test"
    74.   },
    75.  
    76.   {
    77.     "id": 3,
    78.     "title": "Brass",
    79.     "value": 1,
    80.     "stats": {
    81.       "damage": 0,
    82.       "defence": 0,
    83.       "vitality": 0
    84.     },
    85.     "description": "An alloy of copper and zinc. Rather popular nowadays.",
    86.     "stackable": true,
    87.     "stacksize": 20,
    88.     "rarity": 1,
    89.     "craftable": false,
    90.     "mats":{
    91.       "id_1":-1, "amount_1":0,
    92.       "id_2":-1, "amount_2":0,
    93.       "id_3":-1, "amount_3":0,
    94.       "id_4":-1, "amount_4":0
    95.     },
    96.     "slug": "ingot_brass",
    97.     "type": "Material"
    98.   }
    99. ]
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using LitJson;
    5. using System.IO;
    6. public class ItemDatabase : MonoBehaviour
    7. {
    8.     private List<Item> database = new List<Item>();
    9.     private JsonData itemData;
    10.  
    11.     void Start()
    12.     {
    13.         itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Items.json"));
    14.         ConstructItemDatabase();
    15.  
    16.     }
    17.  
    18.     public Item FetchItemByID(int id)
    19.     {
    20.         for(int i = 0; i< database.Count; i++)
    21.         {
    22.             if(database[i].ID == id) return database[i];
    23.         }
    24.         return null;
    25.     }
    26.     void ConstructItemDatabase()
    27.     {
    28.         for(int i = 0; i < itemData.Count; i++)
    29.         {
    30.             Mats materials = new Mats(
    31.                 (int)itemData[i]["mats"]["int_1"],
    32.                 (int)itemData[i]["mats"]["amount_1"],
    33.                 (int)itemData[i]["mats"]["int_2"],
    34.                 (int)itemData[i]["mats"]["amount_2"],
    35.                 (int)itemData[i]["mats"]["int_3"],
    36.                 (int)itemData[i]["mats"]["amount_3"],
    37.                 (int)itemData[i]["mats"]["int_4"],
    38.                 (int)itemData[i]["mats"]["amount_4"]);
    39.  
    40.             print(itemData[i]["id"] + " " +
    41.                 itemData[i]["title"].ToString() + " " +
    42.                 (int)itemData[i]["value"] + " " +
    43.                 (int)itemData[i]["stats"]["damage"] + " " +
    44.                 (int)itemData[i]["stats"]["defence"] + " " +
    45.                 (int)itemData[i]["stats"]["vitality"] + " " +
    46.                 itemData[i]["description"].ToString() + " " +
    47.                 (bool)itemData[i]["stackable"] + " " +
    48.                 (int)itemData[i]["stacksize"] + " " +
    49.                 (int)itemData[i]["rarity"] + " " +
    50.                 itemData[i]["slug"].ToString() + " " +
    51.                 itemData[i]["type"].ToString() + " " +
    52.                 (bool)itemData[i]["craftable"]);
    53.  
    54.             database.Add(new Item(
    55.                 (int)itemData[i]["id"],
    56.                 itemData[i]["title"].ToString(),
    57.                 (int)itemData[i]["value"],
    58.                 (int)itemData[i]["stats"]["damage"],
    59.                 (int)itemData[i]["stats"]["defence"],
    60.                 (int)itemData[i]["stats"]["vitality"],
    61.                 itemData[i]["description"].ToString(),
    62.                 (bool)itemData[i]["stackable"],
    63.                 (int)itemData[i]["stacksize"],
    64.                 (int)itemData[i]["rarity"],
    65.                 itemData[i]["slug"].ToString(),
    66.                 itemData[i]["type"].ToString(),
    67.                 (bool)itemData[i]["craftable"]
    68.                 ));
    69.         }
    70.     }
    71. }
    72.  
    73. public class Mats
    74. {
    75.     public int Item_1 { get; set; }
    76.     public int Amount_1 { get; set; }
    77.     public int Item_2 { get; set; }
    78.     public int Amount_2 { get; set; }
    79.     public int Item_3 { get; set; }
    80.     public int Amount_3 { get; set; }
    81.     public int Item_4 { get; set; }
    82.     public int Amount_4 { get; set; }
    83.     public Mats(int item_1, int int_1, int item_2, int int_2, int item_3, int int_3, int item_4, int int_4)
    84.     {
    85.         this.Item_1 = item_1;
    86.         this.Amount_1 = int_1;
    87.         this.Item_2 = item_2;
    88.         this.Amount_2 = int_2;
    89.         this.Item_3 = item_3;
    90.         this.Amount_3 = int_3;
    91.         this.Item_4 = item_4;
    92.         this.Amount_4 = int_4;
    93.     }
    94. }
    95.  
    96. public class Item
    97. {
    98.     public int ID { get; set; }
    99.     public string Title { get; set; }
    100.     public int Value { get; set; }
    101.     public int Damage { get; set; }
    102.     public int Defence { get; set; }
    103.     public int Vitality { get; set; }
    104.     public string Description { get; set; }
    105.     public bool Stackable { get; set; }
    106.     public int StackSize { get; set; }
    107.     public int Rarity { get; set; }
    108.     public string Slug { get; set; }
    109.     public Sprite Sprite { get; set; }
    110.     public string Type { get; set; }
    111.     public bool Craftable { get; set; }
    112.     public Mats Materials { get; set; }
    113.  
    114.  
    115.     public Item(int id, string title, int value, int damage, int defence, int vitality, string description, bool stackable, int stacksize, int rarity, string slug, string type, bool craftable)
    116.     {
    117.         this.ID = id;
    118.         this.Title = title;
    119.         this.Value = value;
    120.         this.Damage = damage;
    121.         this.Defence = defence;
    122.         this.Vitality = vitality;
    123.         this.Description = description;
    124.         this.Stackable = stackable;
    125.         if (this.Stackable == true)
    126.         {
    127.             this.StackSize = stacksize;
    128.         }
    129.         this.Rarity = rarity;
    130.         this.Slug = slug;
    131.         this.Sprite = Resources.Load<Sprite>("Graphics/Items/" + slug);
    132.         this.Type = type;
    133.         this.Craftable = craftable;
    134.        // this.Materials = materials;
    135.         //if(craftable == true)
    136.         //{
    137.         //    if(id_1 != -1)
    138.         //    {
    139.         //        this.ID_1 = id_1;
    140.         //        this.Amount_1 = amount_1;
    141.         //    }
    142.         //    if(id_2 != -1)
    143.         //    {
    144.         //        this.ID_2 = id_2;
    145.         //        this.Amount_2 = amount_2;
    146.         //    }
    147.         //    if(id_3 != -1)
    148.         //    {
    149.         //        this.ID_3 = id_3;
    150.         //        this.Amount_3 = amount_3;
    151.         //    }
    152.         //    if(id_4 != -1)
    153.         //    {
    154.         //        this.ID_4 = id_4;
    155.         //        this.Amount_4 = amount_4;
    156.         //    }
    157.         //}
    158.     }
    159.  
    160.     public Item()
    161.     {
    162.         this.ID = -1;
    163.     }
    164. }
     

    Attached Files:

    Last edited: Sep 11, 2019 at 11:22 AM