In this post, we discuss delegates and how they can be used to make an event system. Delegates store methods and events used to broadcast listeners' messages when some event occurs.
Delegates
Delegates are the data type that is used to reference methods. When creating delegates, their parameters and return type should be defined. It can hold any method as long as the signatures match.
Similar to objects, delegates can be passed around and used from anywhere.
Creating Delegates
Let's declare a delegate that requires two floats and returns a void.
Now, let's create methods with the same signature as the delegate.
Instantiating Delegates
To use delegates and methods, we need to instantiate them first. This can be done in the same way as constructing an object. Instantiated delegates can be called in the same way as calling a method.
Multicast Delegates
A delegate can also reference multiple methods. We can assign multiple delegates by using the +=
operator. The +
and -
operators can be used to add and remove methods from delegates. Those operators can also be applied between two delegates.
Adding Multicast
Let's add both Adder
and Multiplier
in the delegate operations
.
C# Events
Events are used to broadcast messages to listeners when something happens. An event system consists of two entities: a listener and an emitter. There can be multiple listeners listening to one event invoked by an emitter.
Events can be useful for cases when we just want to know when something occurs without knowing other details. Listeners are also independent, so a listener should not care about what the other listeners do when an event is triggered.
C# events use delegates. They are used to define the type of event.
Adding Events
Let's create a Health
class that will trigger an event OnDamageTaken
when the player takes damage. This event will contain the amount of damage taken. When the player health
reaches 0, an event will be triggered notifying the player's death. This class will also contain a TakeDamage
method to do damage to the player.
Adding Listeners
Let's create two classes that will listen to the events of the Health
class.
Now to use them, create a HealthController
class that constructs the Health
and its listeners. It will also be responsible for damaging the player.
For demonstration, 20 damage will be done whenever the Spacebar key is pressed.
Notice here that the event invoker doesn't need to know what the listener does when the event is triggered. Also, the listener is unaware of other things except when the event is invoked. This causes separation of concern which makes the system more flexible and abstract.
Events Using Multicast Delegates
Multicast delegates can also be used as events. Making a delegate public allows other objects to add their methods to it. These added methods will get called whenever the delegate gets called. This is similar to using events.
Now, we can convert events of Health
into delegates as,
The difference between multicast delegates and event is that delegates can be called from other classes that have access to it, but an event can only be invoked from the object in which it exists. Therefore, using events prevents accidental invoking of events from other classes. It is recommended to use an event instead of delegates as events are safer.
Thanks for reading!