- Home /
How to store references to many unique abilities?
I'm creating a highly modular ability system for my game. In it, both the player and various creatures, NPCs, and even potentially inanimate objects can use abilities. They're unlocked in various ways, both based on character creation and during the game. Each playthrough will see the player using entirely different abilities.
So I have all of the essential shared logic in an abstract class Ability. Then I have abilities implementing it: AbilityA, AbilityB,... AbilityZZZ.
My question is how my game's entities can reference these abilities. This would be easy if they could be ScriptableObjects, but requiring unique code on each means they can't exist as SOs (at least not easily*). I need any entity in the game to be able to find a specific ability and hold a reference to it so that they can access the logic. I can't for the life of me figure out a way to make this happen without having X-dozen gameobjects each holding a single ability script. Obviously that's far from optimal. Any thoughts or ideas on where to go with this?
*the only way this could work is if Ability derived from ScriptableObject, and then I generated an individual SO for every single ability and references to the SOs in a manager which other objects could reference. I feel like there's got to be a better solution than that, also.
I'm not entirely sure if i understood the scenario you're in, could you post one example of an Ability. @Rathlord
The Ability parent script contains branching logic to deter$$anonymous$$e if the target passed in is valid, if the entity has the resources required for the ability, etc. It's an abstract.
Child abilities take the target that's gone through the logic in the parent class an do unique things to it. The most basic possible example is passing damage. That script might look like:
public class DirectAttack : Ability
{
protected override void DoAction(AbilitiedObject target)
{
target.TakeDamage(1, AbilityType.Blunt);
}
}
It tells the target to take 1 Blunt damage. These child classes could also be much more involved, possibly taking multiple targets, applying status effects, or pretty much anything else.
@Rathlord I will just throw some ideas around, hope something is useful XD feel free to correct me if they are not related to problem, maybe i got it wrong.
If the problem each gameobject needing a Ability script, you could ins$$anonymous$$d create an Interface, like Caster, and everyone that implements this interface has access to UseAbility(Ability) ?
Another idea is associated each ability to an ID and save it on a Dictionary, then anyone would be able to retrieve the desired ability by passing the ID.
There is the option of creating a delegate too, and store a method with the desired behaviour.
Your answer