GameDev and C# : Coroutines(IEnumerator) and Loop(Part I)!

Let’s start immediately with the description in the Coroutine documentation

Then look at example

we see the keyword “YIELD”
This keywords means : “We make something, we WAIT “n” TIME and repeat”.

This is exactly what we need for our “Wave of Enemy”.

Let’s look at the syntax: there is a method called WaitAndPrint (), preceded by word IEnumerator.
IEnumerator
represents the Coroutine, so after creating a method, to turn it into a Coroutine, we need the word IEnumerator before it.

Coroutine working really good with LOOPS, expecially WHILE LOOP.
We haven’t seen LOOP yet, but this is a good time to talk about it as we would have to use it in the Coroutine to make Enemy spawn.

In C # language, to iterate through a collection such as Array or List (we talk about Collection in the next articles), we need LOOPS.
The best place to write about it is C# documentation by Microsoft.

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/do

In this link we have all the information about the four types of Loop

As we can see above, the four type of Loop are
DO, FOR, FOREACH and WHILE

Always at this link, if we press RUN near the example, appear a Console that show us the result of this type of iteration

The syntax of DO LOOP is

In this example we start with initialize a variable n = 0.
Then keyword DO and open parentheses
Inside we simply print the value of n (Console.WriteLine() has same function of Debug.Log(), but in Console application with C# we use that method).
last line IS THE CONDITION :
WHILE (n < 5)

Let’s talk about the REAL IMPORTANT PART :
n++ means at every iteration we MUST ADD one to n.
Why?
Beacuse if we not add 1 at every iteration, n WILL ALWAYS MINOR THAN 5, SO WE REMAIN STUCK IN A N INFINITE LOOP!

INFINITE LOOP are really dangerous!
We need to pay real attention when we use Loop, EXPECIALLY WHILE LOOP.

Ok, we try to split down logic about Do Loop
The syntax we read above simply is :
“Print n when n is minor than 5”, ok?

The peculiarity of Do Loop is this : it runs the code AT LEAST ONE TIME EVEN THE CONDITION IS FALSE.

It means that if I write this
int n = 7;
do
{
Console.WriteLine(n);
n++;
} while (n < 5);

THE CONSOLE PRINT 7 EVEN IF 7 IS NOT MINOR THAN 5.

Remembere : Do Loop run code one times and AFTER check the condition.

Let’s talk about FOR LOOP
This is the syntax

for (int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}

FOR keyword, open the brackets and write Condition
In this example we set the variable i = 0;
Run when i <5;
Then add 1 in i in each iteration.

We can read it like this :
“For int i = 0; i is minor than 5; i++;”
After every section we need ;
And the Console print i at every iteration

Normal For Loop

We can use For Loop in reverse if we want, just be careful when setting the conditions, or we will end up in an INFINITE LOOP!

In this example simple reverse condition like this:

Run the code

Reverse For Loop

Next time we talk about FOREACH and WHILE!

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!