Fixing a bug in my Wave System
Objective: Create variables to control when the wave system is called.
Today, I noticed a bug in my wave system. The way the wave system was set up before, I had it set to when the number of enemies are destroyed, that would trigger the next wave of enemies. However, if you were to destroyed enemies faster than the spawning loop was finished then the coroutine would be called before it was finished meaning, the enemy spawning would continue outside of the limits that are set.
To establish a little but more control over the spawn system I created a whopping two bool variables.
With the first of the two we want to go to the bottom of the spawn manager coroutine. This means when the wave has stopped spawning the bool will switch to the positive.
Because we want this to be repeated with a new wave, at the beginning of the coroutine we want the bool to be set in the negative.
Now there is a variable that signifies when the wave spawning coroutine is finished. This bool will not be in the positive unless the coroutine is absolutely finished. Now we want to address the second bool we created earlier. We will place this in the method that deals with the enemies destroyed that triggered the new wave.
Let’s rip out the new wave trigger and replace that with the bool to switch to the positive.
Like before, we want to reset this bool in the negative when a new wave is started. This I placed in my spawn enemy wave routine, the first routine on the spawn manager called when a new wave starts.
Now it’s time to create a method that we will use to trigger the new wave. This wave will only be triggered if both the bools a in the positive, meaning the spawning coroutine is finished and all the enemies were destroyed.
This will now rid our game of the pesky spawning bug. With the new method we can destroy the enemies as fast as we can, and the new wave will never be called until that half a second later when the coroutine is finished. We may take advantage of the half second delay to call new wave later to build suspend on an incoming boss wave.
As I always say, never be afraid to experiment with your code. With every bug you encounter, you’ll be made a better programmer for fixing it. I’ll see you in the next tutorial.