@RanMan @Brian_Trotter
ok guys… thank you! It took me some failed scripts with errors and your patience to understand that whatever variables fields I declare in the ActionItem script CARRIES over to say HealthPotion.cs, and not to really put anything on there except what I want to access during the override…
For those of you who are also confused and not following how the ActionItem script works…
As long as you house bools for health, mana, damage, whatever in the ActionItem script, and find whatever components may be needed (Health, BaseStats, etc.) on the ActionItem script, we will have access to it on the ActionItem subclass (health potion, mana potion, damage increase, etc.) without needing to build a reference to it… for example, lets look at what I added to my ActionItem script to prep it for making a potions subclass.
ActionItem.cs
using System;
using RPG.Attributes;
using UnityEngine;
namespace GameDevTV.Inventories
{
/// <summary>
/// An inventory item that can be placed in the action bar and "Used".
/// </summary>
/// <remarks>
/// This class should be used as a base. Subclasses must implement the `Use`
/// method.
/// </remarks>
[CreateAssetMenu(menuName = ("GameDevTV/GameDevTV.UI.InventorySystem/Action Item"))]
public class ActionItem : InventoryItem
{
// CONFIG DATA
[Tooltip("Does an instance of this item get consumed every time it's used.")]
[SerializeField] bool consumable;
[SerializeField] bool health;
[SerializeField] bool mana;
[SerializeField] float amountToReplenish;
Health player;
// PUBLIC
/// <summary>
/// Trigger the use of this item. Override to provide functionality.
/// </summary>
/// <param name="user">The character that is using this action.</param>
public virtual void Use(GameObject user)
{
Debug.Log("Using action: " + this);
}
public bool IsConsumable()
{
return consumable;
}
public float AmountToReplenish()
{
return amountToReplenish;
}
public Health GetPlayerHealth()
{
return GameObject.FindGameObjectWithTag("Player").GetComponent<Health>();
}
}
}
You can see I’ve added bools for whether or not I’m making a potion for health or mana and a float for how much I want to replenish when the subclass is actually used in the action bar. You can see I also want to be able to call a reference to the players Health script, so I can call a method I have set up on the Health script called Heal(), so that script needs to be in the variable declarations as well.
Now, let’s make a script for a health potion and call it HealthPotion. Because everything you might want on your health potion is already in the base class ActionItem, all the bools and floats you include in there will show up in this when you right click in the assets panel to make a new potion in Unity, EVEN THOUGH they are nowhere to be found in this script, save the reference to the base class in the class declaration… note it doesn’t say Monobehaviour, it says ActionItem… really short script… here we go
using UnityEngine;
namespace GameDevTV.Inventories
{
[CreateAssetMenu(menuName = ("GameDevTV/GameDevTV.UI.InventorySystem/Action Item/Health Potion"))]
public class HealthPotion : ActionItem
{
public override void Use(GameObject user)
{
GetPlayerHealth().Heal(AmountToReplenish());
}
}
}
You can see I’m calling the GetPlayerHealth() that I prepped in the ActionItem script, and it does not need a reference built to access it since we inherited it, any method you put in action item will be usable in your sub class without needing to build a reference to it, it just knows you mean to call it on ActionItem…
now when I right click in unity to make a health potion, you will see it in the menu in the action item because of how the CreateAssetMenu name has the slash Action Item/HealthPotion.
and in the inspector you can see that all the prep I did in ActionItem is there ready for me to access…
and finally, the override from HealthPotion.cs calls the GetPlayerHealth() on the ActionItem, which gets me the player’s health script, and then it calls Heal() with whatever AmountToReplenish() returns (which doesn’t need a reference, it just knows you mean AmountToReplenish() from the ActionItem script) to pass as the argument that method requires…
public void Heal(float amountToHeal)
{
print("i'm supposed to heal " + healthPoints + " for " + amountToHeal);
healthPoints = Mathf.Min(baseStatsScript.GetStat(Stat.Health), healthPoints + amountToHeal);
print("healed, now my healthPoints = " + healthPoints);
}
as you can see I have some print statements in there for debugging which can be removed…
and when it’s all said and done, you won’t have to bother @RanMan and @Brian_Trotter anymore… you’ll have an action item that heals your player all day long… or adds mana… or whatever… I’m gonna try a damage potion for a nice challenge.