Hello,
im working on an inventory system since a few weeks but ive got a problem.
Im working with a static Array as my “inventory”, adding,deleting etc works but now i want to make my items useable.
Item mainclass
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemMAIN : ScriptableObject {
public int ItemID;
public string itemName;
public string itemDesc;
private int itemAmount;
public Sprite itemSprite;
public enum ItemType{Consumable,Equipment};
public ItemType itemtype;
public ItemType getItemType(){
return itemtype;
}
}
Item subclasses
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ConsumableItem : ItemMAIN {
public enum PotionType{HP,MANA};
public PotionType potionType;
public float regAmount;
public void heal(){
if (potionType == PotionType.HP) {
Playerstats.hp = Playerstats.hp * regAmount;
} else if (potionType == PotionType.MANA) {
Playerstats.mp = Playerstats.mp * regAmount;
} else {
return;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EQItem : ItemMAIN {
public enum EQType{WEAPON,RING,NECKLACE,BLESSING};
public EQType eqType;
public float attackBonus;
}
Class to create item based on a int(itemID)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemDatabase {
static private List<ItemMAIN> _items;
static private bool _isDatabaseLoaded = false;
static private void ValidateDatabase(){
if (_items == null) {
_items = new List<ItemMAIN> ();
}
if (!_isDatabaseLoaded) {
LoadDatabase ();
}
}
static public void LoadDatabase(){
if (_isDatabaseLoaded) {
return;
}
_isDatabaseLoaded = true;
LoadDatabaseForce ();
}
static public void LoadDatabaseForce(){
ValidateDatabase ();
ItemMAIN[] resources = Resources.LoadAll<ItemMAIN> (@"Items");
foreach (ItemMAIN item in resources) {
if (!_items.Contains (item)) {
_items.Add (item);
}
}
}
static public void ClearDatabase(){
_isDatabaseLoaded = false;
_items.Clear ();
}
static public ItemMAIN GetItem(int id){
ValidateDatabase ();
foreach (ItemMAIN item in _items) {
if (item.ItemID == id) {
return ScriptableObject.Instantiate(item) as ItemMAIN;
}
}
return null;
}
}
Inventory array:
public static ItemMAIN playerInventory;
the lenght of the array is fixed later in the code.
Adding/deleting works fine,a Object of the type EQItem or ConsumableItem will be added to the inventoryArray
anyway i want to make it useable.
public void UseItem(int itemID){
ItemMAIN item = ItemDatabase.GetItem (itemID);
}
Atm thats my method to use the item, i gonna create a new item based on the itemID. Lets say i wanna use a HealthPotion, i wanna do something like that :
→ check if the item is Consumable or EQ
→ check if the enum PotionType is HP or Mana
→ use void health
Does anyone can tell me how to “check” those things?