SailingonBroats SailingonBroats - 2 months ago 7
JSON Question

Populating an inventory UI with JSON, what's wrong with my code?

I'm using JSON and LitJSON in Unity 5 to populate an inventory UI for a game. The problem is, my code is not returning anything except for information from the item at id:0. At this point I just have two items in my JSON. My code in Unity will recognize that there are only items at id:0 and id:1, because if I put anything else in there, I get an error outside of the argument, but it's still only printing information to the console for id:0.

Here is my JSON:

[
{
"id": 0,
"title": "Stun Gun",
"value": 6,
"stats": {
"power": 100,
"defense": 4,
"vitality": 2
},
"description": "Testing the Stun Gun.",
"stackable": false,
"rarity": 2,
"slug": "stun_gun"
},

{
"id": 1,
"title": "The Great Gun",
"value": 500,
"stats": {
"power": 700,
"defense": 10,
"vitality": 10
},
"description": "The Great Gun is great.",
"stackable": true,
"rarity": 6,
"slug": "the_great_gun"
}
]


Here is my Inventory script:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class Inventory : MonoBehaviour {
GameObject inventoryPanel;
GameObject slotPanel;
ItemDatabase database;
public GameObject inventorySlot;
public GameObject inventoryItem;

int slotAmount;
public List<Item> items = new List<Item>();
public List<GameObject> slots = new List<GameObject>();

void Start ()
{
database = GetComponent<ItemDatabase>();

slotAmount = 8;
inventoryPanel = GameObject.Find ("Inventory Panel");
slotPanel = inventoryPanel.transform.FindChild ("Slot Panel").gameObject;
for (int i = 0; i < slotAmount; i++)
{
items.Add(new Item());
slots.Add(Instantiate(inventorySlot));
slots[i].transform.SetParent(slotPanel.transform);
}

AddItem(0);
AddItem(1);

Debug.Log(items[1].Slug);
}

public void AddItem (int id)
{
Item itemToAdd = database.FetchItemByID(id);
for (int i = 0; i < items.Count; i++)
{
if (items[i].ID == -1)
{
items[i] = itemToAdd;
GameObject itemObj = Instantiate(inventoryItem);
itemObj.transform.SetParent(slots[i].transform);
itemObj.transform.position = Vector2.zero;
itemObj.GetComponent<Image>().sprite = itemToAdd.Sprite;
itemObj.name = itemToAdd.Title;

break;
}
}
}
}


And here is my ItemDatabase script:

using UnityEngine;
using System.Collections;
using LitJson;
using System.Collections.Generic;

using System.IO;

public class ItemDatabase : MonoBehaviour {
private List<Item> database = new List<Item>();
private JsonData itemData;

void Start ()
{
itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Items.json"));
ConstructItemDatabase();

Debug.Log (FetchItemByID(0).Description);
}

public Item FetchItemByID (int id)
{
for (int i = 0; i < database.Count; i++)
if(database[id].ID == id)
return database[i];
return null;
}

void ConstructItemDatabase ()
{
for (int i = 0; i < itemData.Count; i++)
{
database.Add(new Item((int)itemData[i]["id"], itemData[i]["title"].ToString(), (int)itemData[i]["value"],
(int)itemData[i]["stats"]["power"], (int)itemData[i]["stats"]["defense"], (int)itemData[i]["stats"]["vitality"],
itemData[i]["description"].ToString(), (bool)itemData[i]["stackable"], (int)itemData[i]["rarity"],
itemData[i]["slug"].ToString()));
}
}
}

public class Item {
public int ID { get; set; }
public string Title { get; set; }
public int Value { get; set; }

public int Power { get; set; }
public int Defense { get; set; }
public int Vitality { get; set; }
public string Description { get; set; }
public bool Stackable { get; set; }
public int Rarity { get; set; }
public string Slug { get; set; }
public Sprite Sprite { get; set; }

public Item (int id, string title, int value, int power, int defense, int vitality, string description, bool stackable, int rarity, string slug)
{
this.ID = id;
this.Title = title;
this.Value = value;
this.Power = power;
this.Vitality = vitality;
this.Description = description;
this.Stackable = stackable;
this.Rarity = rarity;
this.Slug = slug;
this.Sprite = Resources.Load<Sprite>("Sprites/Items/" + slug);
}

public Item ()
{
this.ID = -1;
}
}


I'm getting the correct number of slots, and the correct number of filled versus empty slots (2), but both slots are filled with the "stun_gun". Any help would be much appreciated. I'm new to Unity/C#.

Answer

Shouldn't it be

public Item FetchItemByID (int id)
{
    for (int i = 0; i < database.Count; i++)
        if(database[i].ID == id)  // i rather than id
            return database[i];
    return null;
}