Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Sycobob · Feb 20, 2013 at 04:03 PM · eventsdelegateachievements

Choosing Events/Delegates to Register for by Parameters Passed

I'm trying to determine the best model for dynamically choosing events to register for. Here's my situation: I have a UserData class that keeps track of overall play stats, and a Challenge class that keeps track of challenges for the user to complete (~3 will be active at a time) and contains fields for text, a delegate that gets passed a lambda expression for testing completion, and a CheckComplete method.

Currently, I'm looking to have UserData fire an event whenever something is updated (with a different event for each stat). Then, upon instantiation, Challenges hook up their CheckComplete method (which uses the lambda expression to check for completion) to the relevant event(s) in UserData.

As far as I'm aware at this point, events can't be passed as arguments, so I'm probably going to need to use multicast delegates.

My ultimate goal here is to only check to see if a Challenge is completed when a relevant stat changes, as opposed to, say, checking all the active challenges for completion whenever any stat changes.

I'm sure this will make some of you weep, but here's a small (non-working) example of what I'm trying to do:

 public static class UserData
 {
     public event Action ClubbedSealsUpdated;
     public event Action LifetimeScoreUpdated;

     ...
 }

 public class Challenge
 {
     public event Action ChallengeCompleted;

     public string text;
     private Func<bool> condition;

     public Challenge ( string displayText, Func<bool> completionCondition,
                        event[] eventsToRegisterFor )
     {
         text = displayText;
         condition = completionCondition;

         foreach ( event e in eventsToRegisterFor )
         {
             e += this.CheckComplete();
         }
     }

     public void CheckComplete ()
     {
         if ( condition )
         {
             UserData.ChallengePoints += 10;
             ChallengeCompleted(this);

             ...
         }
     }
 }

 public static class Challenges
 {
     public static List<Challenge> activeChallenges;
     public static List<Challenge> allChallenges = new List<Challenge>() {

                    new Challenge( "Club 10 baby seals",
                                   () => UserData.ClubbedSeals >= 10,
                                   UserData.ClubbedSealsUpdated ),

                    new Challenge( "Accumulate 10,000 total points",
                                   () => UserData.LifetimeScore >= 10000,
                                   UserData.LifetimeScoreUpdated )
                    }
 }

Does this seem like a reasonable way to accomplish my task? If it is, how do pass which events to register for as parameters in the Challenge constructor? Or is there something better I haven't considered?

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

9 People are following this question.

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

Related Questions

When to use 'delegate', 'event' or 'Action' ? 1 Answer

Singleton class or delegate? 1 Answer

Dynamic Anonymous Delegates/Lambda On UntyEvent 1 Answer

What happens with the receiver when the same event is trigger from multiple instances at the same time? 0 Answers

Interaction script 2 Answers


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