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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Database errors

Discussion in 'Scripting' started by NintendoMaster00, May 23, 2016.

  1. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    I am following a tutorial by AwfulMedia and I am getting 4 errors for one simple part of the script.

    Here is the code:
    Code (CSharp):
    1. void ConstructItemDatabase()
    2.     {
    3.         for (int i = 0; i < itemData.Count; i++)
    4.         {
    5.             database.Add(new Item(
    6.                 (int)itemData[i]["id"],
    7.                 itemData[i]["title"].ToString,
    8.                 (int)itemData[i]["value"],
    9.                 (int)itemData[i]["stats"]["power"],
    10.                 (int)itemData[i]["stats"]["defence"],
    11.                 (int)itemData[i]["stats"]["vitality"],
    12.                 itemData[i]["description"].ToString,
    13.                 (bool)itemData[i]["stackable"]));
    14.         }
    15.     }
    and here are the errors I am getting:
    error CS1502: The best overloaded method match for `Item.Item(int, string, int, int, int, int, string, bool)' has some invalid arguments

    error CS1503: Argument `#2' cannot convert `method group' expression to type `string'

    error CS1502: The best overloaded method match for `System.Collections.Generic.List<Item>.Add(Item)' has some invalid arguments

    error CS1503: Argument `#1' cannot convert `object' expression to type `Item'

    Any help would be really appreciated!
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    I think you are missing brackets after ToString on line 7, it should be
    Code (CSharp):
    1. itemData[i]["title"].ToString(),
    and the same on line 12
    Code (CSharp):
    1. itemData[i]["description"].ToString(),
     
    BrandyStarbrite likes this.
  3. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    That was it,
    Thanks!
     
    BrandyStarbrite and Munchy2007 like this.
  4. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    I am now having another error :p
    Code (CSharp):
    1. KeyNotFoundException: The given key was not present in the dictionary.
    2. System.Collections.Generic.Dictionary`2[System.Int32,System.Int32[]].get_Item (Int32 key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150)
    3. LitJson.JsonReader.Read ()
    4. Rethrow as JsonException: Invalid token '58' in input string
    5. LitJson.JsonReader.Read ()
    6. LitJson.JsonMapper.ReadValue (LitJson.WrapperFactory factory, LitJson.JsonReader reader)
    7. LitJson.JsonMapper.ReadValue (LitJson.WrapperFactory factory, LitJson.JsonReader reader)
    8. LitJson.JsonMapper.ReadValue (LitJson.WrapperFactory factory, LitJson.JsonReader reader)
    9. LitJson.JsonMapper.ReadValue (LitJson.WrapperFactory factory, LitJson.JsonReader reader)
    10. LitJson.JsonMapper.ToWrapper (LitJson.WrapperFactory factory, System.String json)
    11. LitJson.JsonMapper.ToObject (System.String json)
    12. ItemDatabase.Start () (at Assets/Scripts/GUIScripts/ItemDatabase.cs:14)
    Here is my entire code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using LitJson;
    4. using System.Collections.Generic;
    5. using System.IO;
    6.  
    7. public class ItemDatabase : MonoBehaviour {
    8.    
    9.     private List<Item> database = new List<Item>();
    10.     private JsonData itemData;
    11.  
    12.     void Start()
    13.     {
    14.         itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Items.json"));
    15.         ConstructItemDatabase();
    16.  
    17.         Debug.Log(FetchItemByID(0/*ItemId number here*/).Description);//This is used for testing purposes atm
    18.     }
    19.  
    20.     public Item FetchItemByID(int id)
    21.     {
    22.         for (int i = 0; i < database.Count; i ++)
    23.             if (database[i].ID == id)
    24.                 return database[i];  
    25.         return null;
    26.     }
    27.  
    28.     void ConstructItemDatabase()
    29.     {
    30.         for (int i = 0; i < itemData.Count; i++)
    31.         {
    32.             database.Add(new Item(
    33.                 (int)itemData[i]["id"],
    34.                 itemData[i]["title"].ToString(),
    35.                 (int)itemData[i]["value"],
    36.                 (int)itemData[i]["stats"]["power"],
    37.                 (int)itemData[i]["stats"]["defence"],
    38.                 (int)itemData[i]["stats"]["vitality"],
    39.                 itemData[i]["description"].ToString(),
    40.                 (bool)itemData[i]["stackable"]));
    41.         }
    42.     }
    43. }
    44.  
    45. public class Item
    46. {//List you different stats here, remember to change the type and name.
    47.     public int ID{ get; set; }
    48.     public string Title { get; set; }
    49.     public int Value { get; set; }
    50.     public int Power { get; set; }
    51.     public int Defence { get; set; }
    52.     public int Vitality { get; set; }
    53.     public string Description { get; set; }
    54.     public bool Stackable { get; set; }
    55.  
    56.     public Item(int id, string title, int value, int power, int defence, int vitality, string description, bool stackable )
    57.     {
    58.         this.ID = id;
    59.         this.Title = title;
    60.         this.Value = value;
    61.         this.Power = power;
    62.         this.Defence = defence;
    63.         this.Vitality = vitality;
    64.         this.Description = description;
    65.         this.Stackable = stackable;
    66.     }
    67.  
    68.     public Item()
    69.     {
    70.         this.ID = -1;
    71.     }
    72. }

    Code (CSharp):
    1. [
    2.     {
    3.         "id": 0,
    4.         "title": "Pot",
    5.         "value": 6,
    6.         "stats": [
    7.             "power": 1,
    8.             "defence":1,
    9.             "vitality":1
    10.             ],
    11.         "description": "A normal pot, nothing special.",
    12.         "stackable": false
    13.  
    14.     }
    15. ]
     
  5. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    I started on that tutorial but.. I simply abandoned it. If you are going to be using an online database, the JSON database will be useless.
    It just seems messy and overly complicated to me.
     
  6. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    Did you find you did not like the JSON or that it did not work?

    I find I like JSON quite a bit but that is just my personal preference
     
  7. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    It didn't work. I've got an online database, so having a local item database is redundant. Also, I couldn't get Visual Studios to format the JSON which really hampered my desire to attempt using JSON
     
  8. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    what are you using instead SQL?
     
  9. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    Yeah, with a database manager in between the database and the client to handle any commands and prevent SQL injections or intrusions.
     
    NintendoMaster00 likes this.
  10. NintendoMaster00

    NintendoMaster00

    Joined:
    Jan 31, 2015
    Posts:
    86
    [/QUOTE]
    Oh I figured out the error. For anyone else who got this error the error was in the json data with the array, if you take out the array every thing works fine.

    Good Idea I may use this method instead :)
     
  11. Cainebog

    Cainebog

    Joined:
    Jul 15, 2015
    Posts:
    8