Why isnt IModifierProvider called many times?

In the StatsEquipment script we call statsEquipableItems GetAdditiveModifiers and GetPercentageModifiers. But when we call to Basestats GetStat, we go through all the IModifierProviders in the game and sum them all up. Wouldnt this mean that StatsEquipableItems IModifierProvider gets called twice? Is it because its not a gameobject in the world that we must call to it manually? Ofc if all items in your inventory got summed up it would be a mess.

There are several things happening at once behind the scenes with StatsEquipment and StatsEquipableItem.

First, in BaseStats, when we call GetStat(), BaseStats gathers all IModifierProviders on the GameObject and calls GetAdditiveModifiers. At this point in the course, the only two components that should be coming up as IModifierProvider is the StatsEquipment and the Fighter.

Each of these is returning an IEnumerable<float>, but in this case, it’s easier to visualize what’s happening if you think of each of them returning a float[]

We’ll start with Fighter… for GetAdditiveModifier, it yield returns the damage on the WeaponConfig. This means that it effectively sends back a float[] with a length of 1 where the 0th element is damage.

The StatsEquipment, on the other hand, goes through each of the StatsInventoryItems that are equipped (through their IModifierProvider interface) and gets their modifier for that particular stat. Suppose that six slots have StatsInventoryItems with Stat.Health modifiers, then it would effectively be returning a float[] with a length of 6.

In reality, what is returned in these cases is more like a stack of cards, with each card being a float value. BaseStats.GetAdditiveModifiers() looks at each card and adds it to the total which is our total AdditiveModifier.

The same process happens with the PercentageModifiers

So yes, StatsEquipment actually gets called twice for each Stat request, once for the Additive, and once for the Percentage. It gets those modifiers by looking through the equipped items.

You might ask why the equipped items aren’t themselves caught up in BaseStats.GetAdditiveModifiers(). This is because the method is only looking for the IModifierProvider components (MonoBehaviours) attached to the same GameObject. The items in Equipment are just references to ScriptableObjects, which GetComponent cannot see.

thnx!

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

Privacy & Terms