Check if Class is specific enum and change object to child?
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?
Thank you for posting all your code so we have some of the necessary context. The last little bit could use an edit. What you want to know is not clear to me (although I have some guesses). If you could spend a little more time beefing up the question part of your question, you might get more answers.
Giving a few examples of what you want (from the player's perspective) can help. An useful example includes the following:
The situation before the player does something.
What the player does.
What the situation should be after the player does something.
Also, if what you are really doing is trying to start a design discussion, you might want to consider the forums, ins$$anonymous$$d.
Answer by MaxGuernseyIII · Sep 29, 2017 at 01:51 AM
I think what you are asking about is how do you make an item work the way you want it to work. Like, how do you make a potion give someone some health and then go away (for instance).
I think you've taken some good steps and I think there are some more steps you can take.
In short, what you want is called "polymorphism" that is, you have a heterogeneous population of items which each have the potential to do different things.
You've implemented polymorphism using enum type codes. At least you've done this in the case of potions and, presumably, you plan to do this with the more durable items as well.
There are issues with this kind of polymorphism and an alternative exists in the form of inheritance.
This leads us into a deep conversation that, frankly, probably won't even fit in unity answers. As I said in my comment, you could create a forum thread. You can also look up some stuff. I'll give you some search phrases in a second but, first, I want to direct your attention to a super-simple, super-valuable technique for design around problems like this.
It's called "Commonality-Variability Analysis" (CVA) and, like the other topics I'm about to suggest, you can look it up in detail. Unlike those topics, I can also describe it in a kind of cursory sense, right here.
CVA works like this:
Start with your domain (your game).
Brainstorm as many of your requirements as you can (e.g., "when I use an hp potion, I get healed by its amount") - don't worry about organizing them, yet...just get them written down somewhere.
Go through all your requirements and look for requirements that represent things of a like kind (e.g., hp potions go away when used, mp potions go away when used, and daggers go away when thrown) and organize the requirements into those groups.
For each group, analyze what it is, exactly, that they all have in common (e.g., everything that can be pulled out of a chest or dropped has "item-ness").
Use those descriptions as the headings for each group.
When you are done, you have an organized list where each heading is the semantic name of a commonality and each item underneath a heading is a variation of that commonality. This can (but doesn't have to) translate directly into a class hierarchy:
Each commonality can correlate with a base class that has all the stuff that is the same for every variant, especially the interface elements which are required (e.g., all items have the ability to use, and some need to be able to remove themselves from inventory, so there needs to be an interface that supports that).
Each variability can correspond to an inheritor of the commonality's base class, defining only what makes that variant special. (e.g., consumables do something, then destroy themselves).
Once you have that, I believe it will be apparent to you how to make the things you mentioned in your bulleted-list at the end of the first version of your question.