Consumable Stats
Example Script
using Esper.Inventool;
using Esper.Inventool.Items;
using Esper.Inventool.Stats;
using System.Collections;
using UnityEngine;
public class ConsumableEventExample : MonoBehaviour
{
/// <summary>
/// The player's stats.
/// </summary>
private CharacterStats characterStats;
private void Awake()
{
// In this example, this component was added to the player object,
// so we can get the character's stats with GetComponent
characterStats = GetComponent<CharacterStats>();
// Register the event on Awake
Inventool.Inventory.onItemUsed.AddListener(OnConsumableUsed);
}
private void OnDestroy()
{
// Unregister the event when this object is destroyed
Inventool.Inventory.onItemUsed.RemoveListener(OnConsumableUsed);
}
/// <summary>
/// Handles consumable used.
/// </summary>
/// <param name="itemStack">The item stack used.</param>
public void OnConsumableUsed(ItemStack itemStack)
{
// Safety null checks
if (itemStack == null || !itemStack.item)
return;
// Ensure this is the correct item type
if (itemStack.item.itemType.displayName != "Consumable")
return;
// Get the item dataset
var dataset = itemStack.item.dataset as MyItemDataset;
// Loop through each item stat
foreach (var itemStat in itemStack.profile.stats)
{
// Check if the player has that specific stat
var characterStat = characterStats.GetStat(itemStat.Identity);
if (characterStat != null)
{
// If so, apply the consumable
switch (dataset.consumableType)
{
case MyItemDataset.ConsumableType.Restoration:
if (!dataset.isOverTime)
{
// Restore immediately
characterStat += itemStat;
}
else
{
// Restore over time
StartCoroutine(RestorationHelper(characterStat, itemStat, dataset.consumableLength));
}
break;
case MyItemDataset.ConsumableType.Buff:
// Apply the buff (with auto removal)
StartCoroutine(BuffHelper(characterStat, itemStat, dataset.consumableLength));
break;
}
}
}
}
/// <summary>
/// A coroutine that assists with over-time stat restoration (each tick delayed by a second).
/// </summary>
/// <param name="characterStat">The charater stat to restore.</param>
/// <param name="itemStat">The stat to use as the restoration source.</param>
/// <param name="length">The duration of the consumable's effect.</param>
/// <returns>An enumerator that performs over-time stat restoration when used as a coroutine.</returns>
private IEnumerator RestorationHelper(Stat characterStat, Stat itemStat, float length)
{
// Calculate ticks based on length (1s = 1 tick, 1.5s = 2 ticks)
var ticks = Mathf.CeilToInt(length);
// Calculate value added per tick
var overTimeValue = itemStat.value / ticks;
// Tick loop
for (int i = 0; i < ticks; i++)
{
// Restore the stat
characterStat += overTimeValue;
// Yield for a second before the next tick
yield return new WaitForSeconds(1f);
}
}
/// <summary>
/// A coroutine that assists with buffs. The buffs are removed automatically.
/// </summary>
/// <param name="characterStat">The charater stat to apply the buff to.</param>
/// <param name="itemStat">The stat to use as a buff.</param>
/// <param name="length">The duration of buff.</param>
/// <returns>An enumerator that performs over-time stat restoration when used as a coroutine.</returns>
private IEnumerator BuffHelper(Stat characterStat, Stat itemStat, float length)
{
// Apply the buff
characterStat.Combine(itemStat);
// Yields for the duration length
yield return new WaitForSeconds(length);
// Remove the buff
characterStat.Uncombine(itemStat);
}
}Important Notes
Dataset Used
Last updated