GameDev and C# : Coroutines(IEnumerator) and Loop(Part II)!
Today we will talk about WHILE LOOP.
I’ll do another article to talk about FOREACH and COLLECTIONS.
The While Loop is very similar to the Do Loop
Syntax is this
int n = 0;
while (n < 5)
{
Console.WriteLine(n);
n++;
}
As we can see, even syntax is rerally similar to the DO Loop
We set the variable n = 0;
keyword WHILE and open paretheses to set the condition
Our condition here is n < 5;
Inside While Loop print n at every iteration
and add 1 to n to not get stuck inside forever
This syntax means :
“WHILE n is minor than 5, KEEP RUNNING THIS LOOP.
And exactly like the For Loop, we can reverse it
int n = 5;
while (n > 0)
{
Console.WriteLine(n);
n--;
}
Run the code
Now, why did I mention that the While Loop is the most dangerous?
Let’s look the While loop inside Unity
We write the keyword while and press TAB to autocomplete.
Now, inside parentheses in autocomplete appear the keyword TRUE.
With While Loop, we can create SOMETHING which RUNS WHEN THE CONDITION IS ALWAYS TRUE.
This is incredible POWERFUL AND DANGEROUS.
Technically, we can create a While Loop which RUNS FOREVER IN OUR GAME OR PROJECT!
And this is incredible! Because with While Loop we can literally MANAGE ALL FLOW OF OUR DATA.
And this is why the WHILE LOOP WORKS INCREDIBLE GOOD WITH COROUTINE!
Because Coroutine give to our device the TIME TO BREATH. What it means? Remember, with Coroutine we can MANAGE THE TIME, so we can create a SORT OF infinite Loop throughout our Game/Project without have worry to break everything!
Of course we still have to be VERY CAREFUL, but the concept of the While Loop / Coroutine duo is exactly that.
In fact, the best way to use While Loop is WITH the COROUTINE.
Without is really dangerous, always for the same problem.
If we stuck in an INFINITE LOOP, THE PROGRAM CRASHES!
P.S. there is a way to handle EVERY KIND OF ERROR, so technically we can use it whenever we want, but in any case I don’t feel like recommending the use of While Loops outside of Coroutines, at least if you have no experience with it .
Ok, back to our project, we try to figure out how work with this While Loop and Coroutine
First, we create a Coroutine
We don’t worry about red underline.
This happen because this is a Coroutine, and if we don’t use the keyword yield inside it, Intellisense give us a “error”.
Then, we have to INSTANTIATE ENEMY PREFAB EVERY 5 SECONDS.
So we need a reference to a prefab
And drag and drop prefab from folder to SpawnManager
Than we create an INFINITE LOOP inside a Coroutine
And Instantiate an EnemyPrefab every 5 seconds.
The line : yield return new WaitForSeconds()
means : “Wait 5 second, and Instantiate Enemy again”
We have more than one option, for example WaitForFrame().
If we type yield return null we wait automatically for the next frame.
We can also give to a Coroutine a delay to start if before instantiate Enemy we setting a WaitFor”Something”, like frame or seconds.
To make object spwn in a right position, we need a variable because with this type of code every enemy spawn at 0, 0, 0, and this is not what we want.
So create a variable of type Vector3 spawnPosition
If you want we can create another variable for randomPos on X axis and use it inside a new Vector3, and set again the Instantiate method
With that, we now have our Coroutine ready for use.
But HOW?
First of all : to use Coroutine we need a right syntax
STARTCOROUTINE();
And inside we can pass directly the name of Coroutine.
If we now try to test it
Our Coroutine works great!!
In the next lesson we clean up and set better our Coroutine.