A Practical Use for Delegates in Video Games
Video games are full of triggers and reactions — in fact, you could argue that much of a game’s core logic is built on these interactions, layered and interconnected. For example, when a player dies, other characters might respond by attempting a revival or delivering a line of mournful dialogue. Complex behavior like this becomes much easier to manage and scale by using delegates and events, allowing systems to react dynamically without tightly coupling the code.
Objective: Understand the usefulness of delegates in video games by observing a practical example where the Player dies and different characters react to their death.
Table of Contents
· Scene Set Up
· The Player Script
· Reactions
∘ The Healer Script
∘ The Fighter Scrip
∘ The Game Over
· The Result
Scene Set Up
In this prototype scene, the characters are expressed as cubes.
When the Player dies, the Healer will respond by attempting to revive them, while the Fighter will react with a line of dialogue. Since this is just a prototype, we’ll demonstrate these responses using console messages — but the underlying logic would remain the same when replaced with actual in-game functions.
To keep things simple, we will be using a “Kill Button” that when press triggers the Player’s death.
The Player Script
The Player script is where the delegate is defined, allowing the Player to track their own death internally. Because this setup is completely self-contained and independent, any other object can subscribe to the delegate and respond in its own way.
The key idea is that the delegate signals that something happened — not how to respond. This means different objects can react differently to the same event: an enemy might celebrate the Player’s death, a companion might mourn, and the UI could display a game over screen. Each response is handled independently, making the system flexible and scalable.
Now we need to check if any objects are listening — that is, whether any have subscribed to the Player’s death event — before executing it.
Reactions
The Healer Script
The Healer will respond by attempting to revive the Player. To subscribe the Healer to the Player’s event, we simply access the Player and attach a listener to the event. This subscription is done within the OnEnable
function.
Note: In this case a listener refers to any object that is using the onDeath event.
Our reaction will be handled by the Revive
method. For the sake of this example, it simply logs a message to the console. We assign this method to the onDeath
event, allowing it to be triggered when the Player dies.
The Fighter Scrip
The Fighter follows the exact same logic as the Healer, however, they instead deliver a dialogue line.
Of course, what makes it truly unique to the Healer is when we create proper functions for Reviving and dialogue lines.
The Game Over
In a Game Manager Script, we can trigger a game over sequence where the Player is reset, this should only happen if the Healer fails to revive the Player.
The Result
When we click the Kill Button, all the different objects subscribed to our Player broadcast should react in their own unique way.
I hope this article was helpful! Check out my portfolio! Looking to get hired in the video game industry! :)