Hi, very new and stupid programmer here. I’m attempting to make a simple RPG following this series link text and i wish to add WeaponTypes and ArmorTypes to my items, so i can equip them into Equipment slots further down the line, but i’m having trouble understanding how to read info and convert it.
Here’s a copy of what i thought would work :
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>();
JsonData itemData;
void Start()
{
//Item item = new Item(0, "Basic Short Sword", 5); create your own.
itemData = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + "/StreamingAssets/Items.json"));
ConstructItemDatabase();
Debug.Log(FetchItemByID(1).Description);
}
public Item FetchItemByID(int id)
{
for(int i=0; i < database.Count; i++) // Searches through the database for an item matching the ID you give it.
{
if(database*.ID == id)*
{
return database*;*
}
}
return null;
}
public Item FetchItemByName(string name)
{
for(int i = 0; i < database.Count; i++)
{
if(database*.Title == name)*
{
return database*;*
}
}
return null;
}
void ConstructItemDatabase()
{
// Having problems here for loading Enums
for (int i = 0; i < itemData.Count; i++)
{
database.Add(new Item((int)itemData[“id”], itemData[“title”].ToString(), itemData[“itemType”].ToString, itemData[“weaponType”]/* Nope /, itemData[“armorType”], (int)itemData[“value”], (int)itemData[“stats”][“damage”], (int)itemData[“stats”][“armor”], (int)itemData[“stats”][“block”], (int)itemData[“stats”][“dodge”], (int)itemData[“stats”][“speed”], itemData[“description”].ToString(), (bool)itemData[“isEquippable”], (bool)itemData[“stackable”], (bool)itemData_[“consumable”], (string)itemData[“slug”]));
}
}*_
}
public class Item
{
public int ID { get; set; }
public string Title { get; set; }
public int Value { get; set; }
public int Damage { get; set; }
public int Armor { get; set; }
public int Block { get; set; }
public int Dodge { get; set; }
public int Speed { get; set; }
public string Description { get; set; }
public bool IsEquippable { get; set; }
public bool Stackable { get; set; }
public bool Consumable { get; set; }
public string Slug { get; set; }
public enum ItemType
{
Weapon,
Armor,
Consumable,
Misc,
Material,
Quest
}
public ItemType itemType { get; set; }
public enum WeaponType
{
Dagger,
ShortSword,
LongSword,
BroadSword,
Axe,
HeavyAxe,
Staff,
Spear,
Shield
}
public WeaponType weaponType { get; set; }
public enum ArmorType
{
Head,
Torso,
Legs,
Feet,
MainHand,
OffHand,
Ring,
Neck,
Trinket
}
public ArmorType armorType { get; set; }
public Sprite Sprite { get; set; }
public Item(int id, string title, string itmType, WeaponType wpnType, ArmorType armrType, int value, int damage, int armor, int block, int dodge, int speed, string desc, bool equippable, bool stackable, bool consumable, string slug)
{
//If i make Item(int id, string title, ItemType itmType… into string itmType i need to convert it to enum somehow?
//ItemType parsed_enum = System.Enum.Parse(typeof(ItemType), your_string); wtf do i do with this?
this.ID = id;
this.Title = title;
// ItemType itemtype = System.Enum.Parse(typeof(ItemType), itemType); nope…
this.weaponType = wpnType;
this.Value = value;
this.Damage = damage;
this.Armor = armor;
this.Block = block;
this.Dodge = dodge;
this.Speed = speed;
this.Description = desc;
this.IsEquippable = equippable;
this.Stackable = stackable;
this.Consumable = consumable;
this.Slug = slug;
this.Sprite = Resources.Load(“Sprites/Items/” + slug);
}
public Item()
{
this.ID = -1;
}
}