The backbone of this Inventory are several classes that inherit from SciptableObject.
Item Class - Abstract Class, ItemInstance Class
The Item Class is an abstract class, this means that no Item objects can be created directly of the Item type. Instead, we must write concrete classes that inherit from the Item Class, such as Gem, Potion.
Methods: abstract, virtual, override:
The Item class introduces new types of methods, where we want to insure we can call the method Use( ), on all object instances ( of child classes )
Updated: April 11, 2020
class Item, ItemInstance
using System.Collections;using System.Collections.Generic;using UnityEngine;//Modified from//https://github.com/Toqozz/blog/blob/master/inventory//https://answers.unity.com/questions/1415831/inheritance-from-a-scriptableobject.html//updated 4/10/2020publicenumItemType{ Gem, Potion, Key}[System.Serializable]//abstract classes can not be used to make //actual object instances, only child classes of //abstract classes can actually be created.publicabstractclassItem : ScriptableObject {public string itemName;publicint value;publicSprite sprite;protectedItemType itemType; //updated code adds variable below - must be set in child class constructorpublic string instanceType; // enum type set in child classes - ie: Gem.Sapphire/// <summary>/// virtual means that this class can be overridden in a child-class, but it is not required/// in which case no code would be executed since this default version of the method /// happens to have no code./// </summary>public virtual voidUse() {//no code in this case, so nothing will be executed }///other option which requires Use to be overridden in child classes/// /*public abstract void Use() { //no code in this case, so nothing will be executed } */}[System.Serializable]publicclassItemInstance{// Reference to scriptable object "template".publicItem item; //should be a child class itempublicItemInstance(Item item,int value ) {this.item= item;item.value= value; }}
Class Gem
using System.Collections;using System.Collections.Generic;using UnityEngine;//modified from////https://github.com/Toqozz/blog/blob/master/inventory//updated 4/10/2020publicenumGemType { Ruby, Diamond, Sapphire, Emerald }[System.Serializable]publicclassGem : Item {publicGemType gemType;publicGem() { itemType =ItemType.Gem; instanceType =gemType.ToString(); //updated - new code }public override voidUse() {Debug.Log("Using Gem "+this.gemType);//TODO what does a gem do? }} //end class Gem
Class Potion
using System.Collections;using System.Collections.Generic;using UnityEngine;//modified from////https://github.com/Toqozz/blog/blob/master/inventorypublicenumPotionType { Energy, Wisdom, Truth, Health }publicclassPotion : Item {publicPotionType potionType;publicPotion() { itemType =ItemType.Potion; instanceType =potionType.ToString(); //set item value of potionType enum as string }public override voidUse() {Debug.Log("Using Potion "+this.potionType);switch (this.potionType) {casePotionType.Health:GameData.instanceRef.BoostHealth(this.value);break;casePotionType.Energy:// GameData.instanceRef.BoostEnergy(this.value);break;casePotionType.Wisdom:casePotionType.Truth://GameData.instanceRef.BoostExperience(this.value);break; }GameData.instanceRef.BoostHealth(this.value); }} //end class Potion