• 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 /
  • Help Room /
avatar image
Question by Ziron999 · Sep 15, 2015 at 08:58 PM · eventeventsdelegatedelegates

Question regarding delegates and events

I have been recently learning about delegates & events. I would like to know performance wise if there is another way to do this. Or should I just write events for every single command like so:

 public delegate void IndividualUnitAttackingDelegate(GlobalAISensor aiSensor);
 public static event IndividualUnitAttackingDelegate MoveEnemyUnitToAttack1;
 public static event IndividualUnitAttackingDelegate MoveEnemyUnitToAttack2;
 public static event IndividualUnitAttackingDelegate MoveEnemyUnitToAttack3;

 public delegate void IndividualDefendingUnitDelegate(Vector3 destination);
 public static event IndividualDefendingUnitDelegate MoveEnemyUnitToDefend1;
 public static event IndividualDefendingUnitDelegate MoveEnemyUnitToDefend2;
 public static event IndividualDefendingUnitDelegate MoveEnemyUnitToDefend3;

here is how it's being used in this main event script if for whatever reason you need that information:

 void DetermineTargetDestination()
     {
         if (Enemy1DefendPlanet)
             MoveEnemyUnitToDefend1(defendPosition1);
         if (Enemy2DefendPlanet)
             MoveEnemyUnitToDefend2(defendPosition2);
         if (Enemy3DefendPlanet)
             MoveEnemyUnitToDefend3(defendPosition3);
         if (!Enemy1DefendPlanet && aiSensor1 != null && MoveEnemyUnitToAttack1 != null)
             MoveEnemyUnitToAttack1(aiSensor1);
         if (!Enemy2DefendPlanet && aiSensor2 != null && MoveEnemyUnitToAttack2 != null)
             MoveEnemyUnitToAttack2(aiSensor2);
         if (!Enemy3DefendPlanet && aiSensor3 != null && MoveEnemyUnitToAttack3 != null)
             MoveEnemyUnitToAttack3(aiSensor3);
         Enemy1DefendPlanet = false;
         Enemy2DefendPlanet = false;
         Enemy3DefendPlanet = false;
     }
Comment

People who like this

0 Show 2
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
avatar image Ziron999 · Sep 15, 2015 at 08:59 PM 0
Share

as a note this is regarding over a 1000 active units with commands being passed in and out that is why I am currently using delegates...

avatar image Suddoha · Sep 16, 2015 at 12:46 AM 0
Share

May I ask why you use 3 different events of each type? Do they have different subscribers?

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by YoungDeveloper · Sep 15, 2015 at 09:02 PM

Why not pass the command as secondary param?

Comment

People who like this

0 Show 3 · Share
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
avatar image Ziron999 · Sep 15, 2015 at 09:05 PM 0
Share

do you mean create something like IndividualUnitDelegate and then add both of these delegates to that delegate? I'm not adding a 2nd param and just using 1 delegate because then it would be processing that 2nd param in code where it's not needed.

avatar image YoungDeveloper · Sep 15, 2015 at 09:11 PM 0
Share

I mean passing in function everything you need MyDelegate(Sensor sensor, AttackType AttackType);

And or receive

 switch(attackType){
  case AttackType.Melee:
     //work
 break;
 }
avatar image Ziron999 YoungDeveloper · Sep 15, 2015 at 09:30 PM 0
Share

that's where my issue is sensor would only be used in attack and the vector3 would only be used in defend. maybe I should just do MyDelegate(AttackType AttackType)? Would this actually process information faster this way? performance matters a lot here it's 1000 units of data passing through scripts.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

29 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

Related Questions

Instance variables and this == null in event handler 1 Answer

Event NullReferenceException 1 Answer

Different approach to an upgrade system 4 Answers

Argument Exception when no arguments? Method Arguments are Incompatible 0 Answers

Delegate events inside Update 0 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