I'm new to using Scriptable Objects, so this might just be a bit of confusion.
Let's say we have a Scriptable Object that defines the abilities a character might have in the game, and a character class that triggers these abilities:
public class Ability : ScriptableObject
{
public string Name = "New Ability";
public int ManaCost = 10;
public Sprite AbilitySprite;
public void Execute(ref Character character)
{
if (character.Mana < ManaCost)
{
return;
}
// Do something, like instantiating and moving the sprite
character.Mana -= ManaCost;
}
}
public class Character : Monobehaviour
{
public int Health = 100;
public int Mana = 100;
public List<Ability> AbilityList = new List<Ability>();
private void Update()
{
// Crude implementation for example purposes
if (Input.GetKeyDown(KeyCode.Alpha1)) Abilities[0].Execute(ref this);
if (Input.GetKeyDown(KeyCode.Alpha2)) Abilities[1].Execute(ref this);
}
}
Obviously we can make numerous abilities with different mana costs, and add them to each character in the scene as needed; some characters may have a "Bolt" ability, while others might have an ability that casts "Fire." The Execute()
method will then evaluate whether or not the character can use these ability based on the character's current mana.
But what if some of these abilities, all of which have similar effects, needed to also check the character's health, or perhaps a collision state, like isGrounded
? For instance, maybe a certain ability can only be executed when the character has the appropriate mana value, and is also airborne. Would it be necessary to make many different subclasses of the Ability
class to accommodate these different conditions, each with its own logic?
At first I tried changing the Execute()
method to take in a bool, like this:
public void Execute(ref Character character, bool canExecute)
{
if (!canExecute || character.Mana < ManaCost)
{
return;
}
}
The problem with this is that I have to hard code the conditions for canExecute
and compare the current ability against its Name
, and that sort of defeats the purpose of Scriptable Objects. My goal is to be able to create abilities that might need to check varying conditions while performing the same functionality, and assign them in the inspector to each character as needed.