Caching CooldownStore and Mana (again)

Hey there,

I wanted to reply to the already existing question about caching CooldownStore and Mana (Caching CD and Mana) but it is closed.
So I re-open it here.
I understand all that have been said there… anyway, I still feel like @psyconius.
I feel like I want to cache these components in order to be able to use them.
Would caching them in the AbilityData be a valid approach?
I mean something like this:

    public class AbilityData
    {
        GameObject user;
        IEnumerable<GameObject> targets;
        Vector3 targetPoint;
        Mana mana;
        CooldownStore cooldownStore;

        public AbilityData(GameObject user)
        {
            this.user = user;
            mana = user.GetComponent<Mana>();
            cooldownStore = user.GetComponent<CooldownStore>();
        }

        public CooldownStore CooldownStore { get => cooldownStore; }

        public Mana Mana { get => mana; }
...

And then, when used:

        public override void Use(GameObject user)
        {
            abilityData = new AbilityData(user);

            if (abilityData.CooldownStore == null || abilityData.CooldownStore.GetRemainingTime(this) == 0)
            {
                if (abilityData.Mana != null && abilityData.Mana.CanCast(manaCost))
                {
                    targetingStrategy.StartTargeting(abilityData, () => TargetAcquired(abilityData));
                }
            }
        }
...

Any downside?
Thanks!

NB: of course, it means that enemies will have a cooldown store component and a mana component too. But this feel ok for me…

Absolutely, just not in Ability itself. Not sure from a profiler perspective if that would save a great deal of clock, but it certainly wouldn’t be worse than a GetComponent at each stage.

Mine do. If the enemies are going to have abilities, they need an AbilityStore, and they need the same helper components that the player would need.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms