Game Development Reference
In-Depth Information
Delegates
We encounter delegates in our everyday lives. Sometimes they are managers, sometimes
they are subordinates, and they could even be the barista at your local coffee shop. Deleg-
ates effectively are methods that accept pieces of work to do on behalf of someone else.
Tip
Another form of delegates is to use the C# generics and the Action or Action<T>
methods, which is a shorthand version of the implementations mentioned in the next sec-
tion. For more information about generics and Action , refer to ht-
tp://msdn.microsoft.com/en-us/library/018hxwa8(v=vs.110).aspx .
There are two main patterns in which delegates are used: the configurable method pattern
and the delegation pattern.
Note
The scripts in this section can be found in Assets\Scripts\Examples under the
Delegates script.
The configurable method pattern
The configurable method pattern is used when a piece of work or function is passed to an-
other method to be used to complete a task. This pattern is usually used where different
pieces of code can perform a common task in unique ways, such as walking, running, or
patrolling. All these tasks can be the default behaviors of a character. Refer to the follow-
ing diagram:
Here, you will have your code calling a delegate method, but the contents of this
method can be different depending on what you have set it to.
For instance, refer to the following code:
Search WWH ::




Custom Search