Search Unity

Live Training UI Tools: Making a generic modal window (Pt 2) UnityEvents and anonymous methods

Discussion in 'Community Learning & Teaching' started by RavenLiquid, May 13, 2015.

  1. RavenLiquid

    RavenLiquid

    Joined:
    May 5, 2015
    Posts:
    38
    So, I was watching the live training for the modal window, and I was a bit surprised at the way anonymous methods were explained. Besides the semantics about methods and functions (let's not go in to that), I feel people who are new to C# programming may walk away confused or not seeing what they are doing and missing the power of anonymous methods.

    Now, for those confused about what I am talking about it looks like this:
    Code (CSharp):
    1. UnityEvent( () => { DisplayText("Hello"); });
    Now, the main issue I had with the explanation is the limited usage it appears to have.
    He told us the syntax was Function(argument) that could be passed in, and the way it is written you might overlook what it actually is.

    Lets look at it from another angle:
    Code (CSharp):
    1. UnityEvent(
    2. () =>
    3. {
    4.     DisplayText("Hello");
    5. });
    Looks more familiar?

    It looks like a method(or fuction) right, but without a name or return type. Thats because it is exactly what it is.

    So, now that we know what it is what else can we do? Well, anything that you would do in a regular method.
    You are not limited to passing just other methods in here, but you can do actual code. Maybe you have an overloaded DisplayText that also takes color, and you want to randomize it.

    Code (CSharp):
    1. UnityEvent(
    2. () =>
    3. {
    4.     Color col = new Color(Random.value, Random.value, Random.value);
    5.     DisplayText("Hello", col);
    6. });
    Just try not to write anything here that would have been better off in it's own separate method.
    Also try to avoid code duplication. If you are typing things more than once, chances are you could have written a method for it. This keeps things simple and readable.

    So, whats another big advantage of using anonymous methods? Scope of variables.

    Take a look at the following example:

    Code (CSharp):
    1. private void DisplaySomeText(string text)
    2. {
    3.    string text2 = "World!";
    4.    UnityEvent buttonEvent = UnityEvent(
    5.    () =>
    6.    {
    7.        Color col = new Color(Random.value, Random.value, Random.value);
    8.        DisplayText(text, col);
    9.        DisplayText(text2, col);
    10.    });
    11.    classThatDisplaysText.AddButtonEvent(buttonEvent);
    12. }
    As you can see, your anonymous method will know both the parameter text and the variable text2. Both are limited to the scope of DisplaySomeText, so if you were to write a method for this, it would need 2 parameters, text and text2, to be passed in because they live outside the scope.

    I hope all of this makes sense to those new to C#. Please let me know if anything is not clear or wrong and I'll try to fix it.
     
    dterbeest likes this.
  2. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,670
    Thanks for this write up.

    Essentially, I didn't really want to explain what they were or what they did, as I didn't have time, but I also didn't want to not show how one could use them in context, so I did breeze over them.

    If always find it difficult knowing how far down any particular side-path to go when it's not related to the main topic.

    I appreciate the explanation.

    FWIW: I've added this post to the lesson as a community resource.
     
  3. RavenLiquid

    RavenLiquid

    Joined:
    May 5, 2015
    Posts:
    38
    I totally understand, and I think your lessons are very good. It's just that my viewpoint is from a software developer side, and I felt like this might make it easier to use it. I would have gone off that deep end and probably confused new users with mighty exposition on C#.