Search Unity

  1. We've closed the job boards. If you're looking for work, or looking to hire check out Unity Connect. You can see more information here.
    Dismiss Notice
  2. We're running great holiday deals on subscriptions, swag and Asset Store packages! Take a peek at this blog for more information!
    Dismiss Notice
  3. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  4. Unity 2017.2 is now released.
    Dismiss Notice
  5. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  6. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  8. Unity 2017.3 beta is now available for download.
    Dismiss Notice

How to use Delegates. Like a BOSS.

Discussion in 'Community Learning & Teaching' started by Juan73908, Mar 22, 2015.

  1. Juan73908

    Juan73908

    Joined:
    Aug 20, 2013
    Posts:
    13
    zhangjin_ and HarpSeal1 like this.
  2. Arcan-Studios

    Arcan-Studios

    Joined:
    Mar 12, 2015
    Posts:
    32
    of course we do, thanks very much for great tutorials
     
  3. Vectrex

    Vectrex

    Joined:
    Oct 31, 2009
    Posts:
    194
    Good one. I personally avoid the null check (and the potential bugs from forgetting it) like this
    event SimpleDelegate OnTestEvent = delegate {};

    You could also mention static events.
     
    videoanime likes this.
  4. Juan73908

    Juan73908

    Joined:
    Aug 20, 2013
    Posts:
    13
    Thanks a lot Vectrex! :)

    I didn't know that trick with the initialization of the delegates. The only problem I see there is in case you reset the delegate like this: "OnTestEvent = null;". But it's not really a problem because you should do that. :)

    I already added your tips to the post! :D
     
  5. Adam-Buckner

    Adam-Buckner

    Unity Technologies

    Joined:
    Jun 27, 2007
    Posts:
    5,669
    Can you go into more detail?
     
  6. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,009
    Nice article. You can also use event Actions. It's an underlying delegate and is a bit cleaner in my opinion.
     
  7. Vectrex

    Vectrex

    Joined:
    Oct 31, 2009
    Posts:
    194
    Simply when you define an event, stick an empty delegate at the end. A Bit messy looking but does the trick. Lots of forums discussions seems to indicate that it doesn't have a performance penalty.

    public event ExampleDelegate OnExampleEvent = delegate {};
     
  8. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,009
    No!

    Stop. Don't ever declare an empty delegate. It's bad practice. Null check it.

    A problem will appear if you rely on this technique and then you have to serialize the class. You will eliminate the event, and then on deserialization you will get a NullRefference.... .So one can just stick to the "old way" of doing things. It's safer. It's better. It's the "RIGHT" way of doing it.
     
  9. Vectrex

    Vectrex

    Joined:
    Oct 31, 2009
    Posts:
    194
    I've been pwned! Yeah that's an issue. Although the serialiser should add it :)
    Really, I consider it a bug that you have to null check. It's an event, logically it shouldn't ever care if there's zero or a million listeners. The "UnityEvent" type lets you do this neater actually. I hope they expand the editor stuff for listeners subscribing themselves.
     
    SubZeroGaming likes this.
  10. Juan73908

    Juan73908

    Joined:
    Aug 20, 2013
    Posts:
    13
    Thanks SubZeroGaming for making everything clearer!

    It would be very hard to learn such things otherwise! :D

    Post updated thanks to your comments :)
     
  11. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,009
    Thanks for the creds :)