21 - Delegates
Delegates are the gateway to event-based programming. Event-based programming is a very important concept which allows the user to interact with objects at run time. Common run time actions include clicking a mouse or typing keys on the keyboard. Delegates allow us to use what is similar to a method signature to call on methods. A delegate is a reference type variable that holds the reference to a method. The reference can also be changed during runtime. This is the first step to creating events which we will learn in the next tutorial. This tutorial will teach you how to use delegates in a simple script which resets the player's position when they hit a boundary. To start off with this tutorial, create a new script and add the following code below. Then set-up your scene to look similar to the one below and make sure that the walls on the sides have the tag "wall".
The declaration of a delegate has a few parts. The first part is the access level which we are giving the delegate. In our case, it's public. The second part is the keyword delegate. The third part is the return type of the method that is being referenced by the delegate. The fourth part is the name of the delegate. And the final part in parenthesis is any parameters that the method that is being referenced has. A can reference any method which has the same return type and parameters that it has.
The next step to using delegates is to instantiate them. Instantiating a delegate is pretty similar to instantiating a class. The first part is the name of the delegate followed by a a unique name for the action that you are accomplishing. Then equals new the delegate name again and then the method that you would like to implement using the delegate.
Now that we have instantiated the delegate, the next thing that we would like to do is actually use the delegate. Using a delegate is just like calling a method. How is using a delegate different than using a method then? A delegate focuses on the encapsulation pillar of object oriented programming. A caller invokes a delegate and then the delegate calls the target method. The effect decouples the caller from the target method. In the code below, you can how we can use the delegate to reset our player's position when they collide with a wall.
Delegates also can be combined using the "+" operator. This allows multiple methods to be called when the delegate is called. You can use multicasting to develop a list of actions that you would like to occur every time the delegate is called. In the example below we create delegates for resetting the position and rotation of the transform when we run into a wall. Then we add those delegates to a new delegate which resets the transform. Now to completely reset the transform we simply call the one delegate.
Delegates are an important concept to working with event based programming. In today's tutorial we went over how you can use them to map methods to a single delegate as well as multi-casting delegates. We did this by going through a simple character reset example.