Armour and Power UI

Stats UI

Just two Images + two textsmeshpro (in a Horizontal group layout).
For the script, pretty simple .

using GameDevTV.Inventories;
using RPG.Stats;
using TMPro;
using UnityEngine;

namespace RPG.UI
{
    public class StatsUI : MonoBehaviour
    {
        [SerializeField] private TextMeshProUGUI powerText;
        [SerializeField] private TextMeshProUGUI armorText;
        
        private BaseStats baseStats;
        private TraitStore traitStore;
        private Equipment equipment;
        
        void Start()
        {
            GameObject player = GameObject.FindGameObjectWithTag("Player");
            baseStats = player.GetComponent<BaseStats>();
            traitStore = player.GetComponent<TraitStore>();
            equipment = player.GetComponent<Equipment>();
            traitStore.traitsChanged += RedrawStatsUI;
            equipment.equipmentUpdated += RedrawStatsUI;
            RedrawStatsUI();
        }

        private void RedrawStatsUI()
        {
            float power = baseStats.GetStat(Stat.Damage);
            powerText.text = power.ToString();
            float defense = baseStats.GetStat(Stat.Defence);
            armorText.text = defense.ToString();
        }
    }
}

The new event traitStore.traitsChanged is simply invoked in TraitStore Commit()

If you have something like an ability buff, this probably can also be also handled with a new event in the ActionStore.

2 Likes

Looking good with your code. Keep up the great work!

Thanks you. ^^^
While relecturing, I think we don’t need to cache the TraitStore + Equipement, just the BaseStats.

Looks great. You’re correct, you could simply call

player.GetComponent<TraitStore>().traitsChanged += RedrawStatsUI;
player.GetComponent<Equipment>().equipmentUpdated += RedrawStatsUI;

Privacy & Terms