- Home /
Casting methods, events, and/or delegates from strings / enum
The problem I would like to solve is the following. I am building a generic framework for building cards in a CCG. At the moment, I am using a scriptable object to build each card:
[CreateAssetMenu(fileName = "New CardData"]
public class CardData : ScriptableObject {
public string cardName;
public Sprite image;
public string description;
public TriggerType[] triggers;
public EffectType[] effects;
}
The first 3 parameters are self explanatory. "triggers" and "effects" are two arrays of enums of the same length which define event-effect pairs. Basically, I would like to cast the nth element of triggers to an event, subscribe a method casted from the nth element of effects to that event. This way, I can maintain a list of events (onCardDestroy, onTakeDamage...) and a list of game effects (e.g. DrawCard, GainHealth, etc.) and build cards somewhat generically (I'm open to advice on this strategy by the way).
I believe the technology I need here is "reflection", and I have made a few attempts at using it, but frankly I haven't been able to figure it out. I'm hoping one of you wonderful ace scripters can help me understand it and/or help me find an alternative :)
Concretely, inside of my Card script:
//Scriptable object
CardData data;
...
void Start(){
this.nameText.text = data.cardName;
this.descriptionText.text = data.description;
this.artwork.sprite = data.image;
for (int ii = 0; ii < data.triggers.Length; ii++){
string eventName = data.triggers [ii].ToString ();
string effectName = data.effects [ii].ToString ();
// ..... get event delegate -> e and method m......
e += m;
}
}
Here are my Triggers and Effects scripts (and the associated enums).
public enum TriggerType{
OnCardDiscard,
OnCardPlay
}
public class Triggers : MonoBehaviour {
public static Triggers instance;
// Delegates
public delegate void CardEventHandler(Card card);
public static event CardEventHandler OnCardDiscard; //Triggers when a card is discarded
void Awake () {
//Create Singleton if necessary
if (instance == null) {
instance = this;
DontDestroyOnLoad (this);
} else {
Destroy (this);
}
}
public static void CardDiscarded(Card card) {
if (OnCardDiscard != null) {
OnCardDiscard (card);
}
}
}
public enum EffectType {
PrintTestEffect
}
public class Effects : MonoBehaviour {
public static Effects instance;
void Awake () {
//Create Singleton if necessary
if (instance == null) {
instance = this;
DontDestroyOnLoad (this);
} else {
Destroy (this);
}
}
//A test effect that just prints the card's name
public static void PrintTestEffect(Card card)
{
Debug.Log (card.name);
}
}
To summarize: I want the following effect: Given the strings "OnCardDiscard" and "PrintTestEffect" I would like a game object to call a public static method called PrintTestEffect when the OnCardDiscard event is triggered. The goal is to decouple card definitions from card effects.
I am pretty new to the usage of events and delegates in Unity, so I'm happy to get any additional info on best practices, coding for performance etc. Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612152206im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity Events - Subscribe and Unsubscribe Odd Behavor 0 Answers
How can I use a delegate/event with enums and switch statement to switch powerup? 1 Answer
Delegates/Events across threads 0 Answers
Non-monobehavior editor script subscribing to delegate event and persisting on application reload 0 Answers
How do Delegates and Events work? 1 Answer