Search Unity

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:
    58
    of course we do, thanks very much for great tutorials
     
  3. Vectrex

    Vectrex

    Joined:
    Oct 31, 2009
    Posts:
    267
    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

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

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    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:
    267
    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,008
    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.
     
    Railon23 likes this.
  9. Vectrex

    Vectrex

    Joined:
    Oct 31, 2009
    Posts:
    267
    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,008
    Thanks for the creds :)