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
1
Question by disorganized · Feb 27, 2014 at 07:12 PM · referenceeventdelegate

Delegates versus class references

I have an open question/concern about delegates/events.

I have units in my project that need to update/send information to a single manager class (one manager class per player). Originally I was going to have each unit have a reference to that manager class and send updates that way. I'm looking to instead implement a delegate/event structure where every unit has a delegate(s) and event(s) and the manager class subscribes to each unit upon creation (manager class is responsible for creating each unit). If units are updating events such resource gathering, spotting enemies, losing territory (essentially each unit could be sending out an event every minute or so and there would be around 30 units per player) is this an inefficient/costly way to be sending messages around?

I really have no practical experience in using delegates so I'm just looking for a little feedback on the matter. Big thanks in advance.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by whydoidoit · Feb 27, 2014 at 07:17 PM

Yes that's very efficient. Just make sure you unwire the events when the units are destroyed.

Comment
Add comment · Show 11 · 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 whydoidoit · Feb 27, 2014 at 07:20 PM 1
Share

To clarify - an event call is roughly half the speed of calling a normal function - so in other words it's very fast. $$anonymous$$assively faster than a Send$$anonymous$$essage/Broadcast$$anonymous$$essage - however with the frequency you are talking about even that would be totally fine.

$$anonymous$$ind you a direct reference from Unit to manager sounds good too. If something in the event pattern helps then use that, but it will be slower than a direct call to a child reference.

avatar image disorganized · Feb 27, 2014 at 07:22 PM 0
Share

By 'unwire' I assume you mean something along the lines of: void Dead(Unit u) { for(int i= 0;i

                 (gang[i]as Unit).OnPress -= Adding;
                 (gang[i]as Unit).OnDeath -= Dead;
                 Object.Destroy((gang[i]as Unit).transform.gameObject);
                 gang.RemoveAt(i);
 
                 return;
             }
         }
     }

Thanks for the feedback.

avatar image whydoidoit · Feb 27, 2014 at 07:23 PM 0
Share

Yes - if you forget there ends up being memory leaks that are hard to fix when Unity nulls all of your references.

avatar image whydoidoit · Feb 27, 2014 at 07:23 PM 0
Share

Are you wanting to use events because of multicasting the event to multiple listeners?

avatar image disorganized · Feb 27, 2014 at 07:25 PM 0
Share

It's not so much the pattern that helps, I just want the quickest and least-memory-using method of communicating between classes. I have no problem keeping with class references (say that each Unit had a reference to the $$anonymous$$anager class and communicated through that), I just thought it might be less 'expensive' in terms of memory to use a delegate.

Again, thanks.

Show more comments

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

21 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

Related Questions

Sharing delegate types across scripts 1 Answer

When attempting to call a method from within another script I get this error. 3 Answers

Why doesn't unregistering an anonymous delegate trigger an error? 1 Answer

Destroy(gameObject) only works once 1 Answer

Create delegate from MethodInfo 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