Using Events to Destroy all the Enemies
Objective: Stop spawning and destroy all the enemies on the screen if the player dies
This article is part of an ongoing series of gems I discover while making a shoot 'em up that I feel is worth sharing.
As the objective states, in this game, I want the Spawn Manager to stop spawning enemies and have all the enemies on the screen explode as soon as the player dies just so it has more of an impact. There is a number of ways that come to mind on how we could tackle this.
- Every Update frame — Have each active enemy currently in the level constantly checking if the player is still alive and if not, destroy itself. Have the Spawn Manager do the same.
- Game Manager — Holding a list of active enemies and monitors the player and if the player dies, go through its list and destroy each enemy. Have the Spawn Manager do the same.
- Events — Have the player be the publisher of an event and have each enemy subscribe to that event. When the player dies, the publisher fires the event and all the subscribers of that event are notified. The key thing is the publisher doesn’t need to know who the subscribers are
As you guess, we are going to implement the third option.
In my player script, I define the event. By standard naming convention, we start it with “On…” followed by what happens. In this case, “OnPlayerDies”.
In the PlayerController script, in the OnTriggerEnter2D method where it detects, if there is a collision with an enemy, I am going to set up the trigger for the OnPlayerDies event by simply calling it.
If we were to play the game now, it would show an error because there are no subscribers for this event yet. So the underlying structure for the event is set to null.
All we need to do is do a null check.
You may not be aware but since C# 6 we can shorten this by calling Invoke. This Invokes the OnPlayerDies event only if the event is not null.
With the event defined and triggered, let’s move on to subscribing to this event. First of our subscribers is the Spawn Manager. First, we need a reference to the PlayerController and then subscribed to the event and specify which function to call when the event is fired.
Notice in Visual Studio, when you typed += if you press TAB, it autocompletes the statement with a function.
Let’s replace the function’s code with disabling the Spawn Manager
That’s it for the Spawn Manager. It will deactivate as soon as the player dies. Let’s do the same for the enemies. Add the same code for the Start method.
In the enemy, I have a method that disables the enemy, has exploding effects, and sends it back into the pool of available enemies (Object Pooling).
It is best practice to also unsubscribe from an Event when you no longer need to listen for it. To do this, instead of the +=, replace it with -= as follows.
Yes, I refactored the code so we get the reference in Awake and subscribe to the event.
A much nicer, cleaner code.
If you enjoyed reading this article give me a Clap, also if you would like to see more, “Follow” me, so you may be notified of future releases. You may also send me a message if you need any further help.