Using lambda expressions

Many of the fields you specify on the movement effect are actually actions. For example, one of the most important actions is the RetrieveEnd field, which retrieves the location that the sequence should move towards and end up at.

Every action, including the RetrieveEnd action, receives a reference as input. This reference is usually the object or script which holds all the information necessary to do the work you need. Many times the easiest thing to do is to set Reference to the “this” pointer.

In order to define RetrieveEnd one option is to create a function in your script that accepts a reference variable and returns the value, like so:

float RetrieveEndFunction(Controller reference)
{
      return 5f;
}

 
You can then set RetrieveEnd to point to the function you just created:

var moveOver = new Effect<Controller, float>();
moveOver.RetrieveEnd = RetrieveEndFunction;

 
However, it can get quite cumbersome to define a new function for every action that you want to define. Fortunately the C# compiler accepts lambda expressions, which are a quick way to define a function without even giving that function a name. A pointer to that nameless function is then stored in the RetrieveEnd field. Movement over Time can then use that function to retrieve the end value whenever it needs it. You know it’s a lambda expression when you see the => sign:

var moveOver = new Effect<Controller, float>();
moveOver.RetrieveEnd = reference => 5f;

 
If the function accepts more than one variable then you need parenthesis, and if you want the function to run more than one instruction then you need brackets, but in this case you need neither.