- Home /
How to script many similar entities that each have their own implementation of a given function
So here's the situation.
.
Say I need to make 300 cards. I can make a single prefab for the visuals, and a card data scriptable object for info such as name, description, etc. Easy enough.
.
Now. Say that I want to give the card object and IActivate interface has an Activate function, which will be unique in such a way that I cannot simply give the card data scriptable objects a list of effects to execute in sequence. How do I achieve this cleanly?
.
Would I make a prefab for every card, each with it's own unique script that has an activate implementation?
.
Or I could make an abstract effect class that inherits scriptable object, and each effect would be a unique SO (so 300 different kinds of SOs), which I will have each instantiate so that I can attach them to my card data SO. This is demonstrated in the example blow
public abstract class EffectBase : ScriptableObject
{
public abstract void Activate();
}
// Each card will need a unique SO like this
[CreateAssetMenu(fileName = "Effect", menuName = "Bario")]
public class BarioEffect : EffectBase
{
public override void Activate()
{
throw new System.NotImplementedException();
}
}
// Attach the unique effect SO's to their respective card data SO
[CreateAssetMenu(fileName = "New Card", menuName = "Card")]
public class CardData : ScriptableObject, IEffect
{
public string CardName;
[SerializeReference] private EffectBase Effect;
public void Activate()
{
Effect.Activate();
}
}
Your answer
