- Home /
Adding a behavior to a ScriptableObject
So, here is the situation I'm in. I have different small parts, similar to cards in a TCG, that have different stats and effects. This is an idea situation for a ScriptableObject, since I can create an asset for the cards and enter in the data for things like health, name, etc., but not for effects. I can't have a single ScriptableObject for everything since I need them to have different methods for various triggers.
There's a few ways I can think of to do this, but none of them seem particularly ideal. One is to have reference to a MonoBehavior, and having the "card" objects hold a reference to both a ScriptableObject and a MonoBehavior, but that almost entirely defeats the purpose of using a ScriptableObject, since I might as well just hard-code the data into that MonoBehavior.
Another way is to have every single possible effect in the ScriptableObject .cs file, and have an event selector or message for the ScriptableObject to know what method to call when its triggers are met. This is incredibly messy and difficult to maintain.
Is there any other way to add modifiable methods to a ScriptableObject? For example, having a method named "OnAttack" for two different cards, and set one to a method that deals damage, and another to one that will instead heal itself?
I would like to keep using ScriptableObjects if possible, as they are much quicker to create and edit, and I don't have to worry about updating many instances and prefabs if I need to change a specific card's stats.
I am trying to do a similar thing. I am working on an RPG that has a number of Races(That may have some skills) and a number of Professions(That may have some skills). I created a SO called Race and then created a few Races from the assets menu(Setting their individual stats). I did the same for the professions. I had hoped that adding the skills to each Race/Class would be as easy as adding the following to both Race and Profession:
public ScriptableObject[] skills;
then writing some skills(hide, sneak, cast, s$$anonymous$$l, etc) that derive from ScriptableObject, and attaching the appropriate ones to each Race and profession. But apparently, that isn't an option. I've been digging through google searches to find the right solution but I feel I'm at a dead end. Any suggestions would be greatly appreciated.
I may be missing something but isn't this just asking for polymorphism?
abstract public class Card: ScriptableObject
{
abstract public void OnAttack(...);
}
public class DamagerCard: Card
{
override public void OnAttack(...)
{
}
}
public class HealerCard: Card
{
override public void OnAttack(...)
{
}
}