Why not use Ability to implement IAction?

Hello,
In the video, Sam says that we cant use Ability to implement IAction because an Ability might be shared between different characters. But wont all characters have their own instance of the Ability from their own inventory? Why would it be shared?

Not automatically, no. ScriptableObjects are shared resources. That means that every component with a reference to the ScriptableObject (and an Ability is a ScriptableObject), works on the same copy of the ScriptableObject. That’s why we pass around the AbilityData rather than just setting variables within the Ability.

You can Instantiate the Ability when it is added Inventory (and subsequently when Restored in ActionStore or Inventory), but this won’t work for stackable items, as they won’t be recognized as the same Inventory item by the stacking system. Best practice is to treat all InventoryItems like they are shared resources, and therefore cannot carry individual state.

1 Like

Got it. So the Scriptable Objects are only created once and every time they are referenced either in another scriptable object like here -

or by a GameObject like Player or Enemy, only a reference to them is created? A new memory allocation is not done?

That is correct. It’s not always immediately apparent in C#, as opposed to a language like C++, but object references are exactly that, references. No memory allocations are made unless the class is instantiated or created with the new keyword. For Value types like integer, or structs, a memory allocation is always made.

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

Privacy & Terms