Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cruseyd · Feb 08, 2018 at 08:04 AM · reflectioneventscastdelegates

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges