Communicating between game objects
In any game, there are planned interactions between any components within the game. These could be as follows:
Physics collision tests
Reacting to being shot or shooting
Opening and closing doors
Triggers, switches, or traps
Two or more characters talking
There are several ways in which you can achieve this, and each has their own particular traits. The selection of the implementations depends on what you need to achieve. The methods are as follows:
Delegates
Events
Messaging
In this section, we will go through each method in detail and highlight the best uses of each.
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. Delegates 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...