Search Unity

C# newb-- delegates?

Discussion in 'Scripting' started by iceshaft07, May 27, 2011.

  1. iceshaft07

    iceshaft07

    Joined:
    Jul 23, 2010
    Posts:
    293
    So we downloaded one of Prime 31's plugins, and at first I thought "man, I totally would have done this differently"-- until I realized that he was doing something better than what I would have done.


    I noticed he uses delegates and events, and it appears as though you can just add a function to an event.

    Are the addition and subtraction operators overloaded to mean "AddEventListener" and "RemoveEventListener" for the event class?

    Also, while I understand what a delegate is and to some degree how they work, I've never seen that delegate keyword in front of a function in the languages I've worked in. Does anyone mind explaining how it works?
     
  2. iceshaft07

    iceshaft07

    Joined:
    Jul 23, 2010
    Posts:
    293
    And for bonus points, can I use the + and - operators on the event class in java script ;-)
     
  3. iceshaft07

    iceshaft07

    Joined:
    Jul 23, 2010
    Posts:
    293
    I earned my own bonus points, yes you can :)
     
  4. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    How? I was under the impression that events were not usable in "JavaScript".

    You'd be wise to check out Prime31's videos, especially if you're using the plugins.
    http://www.youtube.com/watch?v=N2zdwKIsXJs
     
  5. matthewminer

    matthewminer

    Joined:
    Aug 29, 2005
    Posts:
    331
    I'm unsure if you can define an event in JavaScript, but if one's defined in a C# script then it's usable no problem.