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/2020
public enum ItemType
{
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.
public abstract class Item : ScriptableObject {
public string itemName;
public int value;
public Sprite sprite;
protected ItemType itemType;
//updated code adds variable below - must be set in child class constructor
public 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 void Use()
{
//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]
public class ItemInstance
{
// Reference to scriptable object "template".
public Item item; //should be a child class item
public ItemInstance(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/2020
public enum GemType { Ruby, Diamond, Sapphire, Emerald }
[System.Serializable]
public class Gem : Item {
public GemType gemType;
public Gem()
{
itemType = ItemType.Gem;
instanceType = gemType.ToString(); //updated - new code
}
public override void Use()
{
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/inventory
public enum PotionType { Energy, Wisdom, Truth, Health }
public class Potion : Item {
public PotionType potionType;
public Potion()
{
itemType = ItemType.Potion;
instanceType = potionType.ToString(); //set item value of potionType enum as string
}
public override void Use()
{
Debug.Log("Using Potion " + this.potionType);
switch (this.potionType)
{
case PotionType.Health:
GameData.instanceRef.BoostHealth(this.value);
break;
case PotionType.Energy:
// GameData.instanceRef.BoostEnergy(this.value);
break;
case PotionType.Wisdom:
case PotionType.Truth:
//GameData.instanceRef.BoostExperience(this.value);
break;
}
GameData.instanceRef.BoostHealth(this.value);
}
} //end class Potion