Welcome back, my fellow game developers.
In this video, we are making the game so much cooler.
So finally, this video makes everything look amazing.
So we start off the game.
We start running around,
oh no, there is a door.
It's closed.
Oh, no. Let's go to the next door.
Oh, no, it's close though.
There are three enemies in here.
Oh, they tried to shoot as they try to follow as nope,
we kill the first one.
Maybe he takes a lot of it's the second one. How many does it take?
One hit his dies and his pleasure, blood.
We call this one. We tried to kill the last one.
We are shooting him as he shooting back.
Nope, he's not. Wait, but Tim shu Okay, It's not shooting.
So we should have, there are things flying in our direction.
We should around him and he dies.
Amazing.
So there you go. The doors are open.
We walked through and now we can go to the next level or we could
use the other door because it also opens when we kill all of our enemies.
So let's not waste any more time.
We will fix this thing right here,
seeing the blue things around us,
and let's get started.
Oh, case.
So now it's time to open up the doors whenever the enemies are killed,
let's go into the enemies right here or the room.
Actually run the game.
And I'm going to try to kill a couple of enemies that's opened up.
There you go.
Let's shoot the enemy right here.
Three bullets should be enough, and he's dead.
And you can see right here that he is now a missing.
Very good. He is missing.
The second one is missing.
The third one is also missing,
so we no longer have any enemies.
The door should open.
The only problem is that we still have these elements in here.
But thankfully, when we're working with lists,
we can actually remove the item very easily.
So the way we do that,
as we go into our updating here in the rooms manager update.
And we are going to create another loop.
And this time this loop is going to be a for loop.
So what is a for loop?
Let me explain.
So what is a for loop?
Loops can execute a block of code number.
Times. Loops are handy if you want to run
the same code over and over again each time with a different value.
Now, I'm delving a bit deeper into loops than we did with the for each,
but not worry for loops are similar to for-each loops.
But this time instead of using the items,
we use the indexes and side of the list or the array.
So you can see that this continues to execute
code inside until I reaches a certain limit.
What does that mean?
Let's look through this syntax right here and understand it better.
We start off by writing for, That's good.
We instantiate a variable which is an integer,
most probably which is an I equals 0.
We then add this semicolon.
We then tell it what is the limit that the I will keep working towards.
And then we stop and each time we add one to the eye.
So you can see right here, this plus,
plus means that it's I plus one.
So we start off at 0,
let's say the certain limit is three.
We will continue looping through until I is no longer less than three.
So we start off at 0,
we do some stuff, we execute the code I plus, plus.
That means I becomes one.
It's still less than three.
Great, we continue on.
We have I plus two,
as it's still less than three.
It is, we continue.
I equals three is it's still less than three.
No, it's not because we need to be less than 3 or less than the certain limit.
And we are going to be using that to see we are going to count through all the enemies.
And when we are at the last enemy,
we are going to go ahead and stop the loop.
And at each level we are going to check if that enemy is an empty game object,
then we are going to remove the entire item from the list.
So let's get started.
This will be a bit complicated,
but bear with me.
So I'm going to go ahead and create a for loop.
And instead of going from the first element to the last element,
I'm going to start at the back.
So I'm going to have an int I equals enemies dot count minus1.
First of all, what is the count?
Enemies dot count is the number of items that we have in the list.
And our case, this will be the three skeletons.
Why am I adding minus one?
Because lists work just as arrays do where we start,
the counting of the indexed from 0.
The last element of the three skeletons will be two.
We have the skeleton zeros,
skeleton one skeleton too.
So count is equal to three because we have three skeletons.
So the first element should be the count minus one.
I hope that is understood.
Now because we are starting from the back,
what is our goal?
We will keep looping through.
While i is greater than minus one.
Why is this minus1?
Well, because when we get to 0,
we still want to loop through the list.
And one we get two minus one because let me just finish this off.
There you go.
Because we are going.
Minus minus.
So just like Plus,
Plus adds a one,
minus, minus removes a one.
So again, what's happening?
We are starting from the back of the list.
We are making sure that we always are greater than minus one,
which means we don't want to have 0 as the last element that we go through.
And finally, we are adding I minus minus.
So let's continue one.
What are we going to do in here?
Well, we are going to check if the enemies
or the enemy at the position I is equal to null.
So null means that he is missing.
We saw previously in a previous video when we kill the skeletons,
the element in there became missing.
That means it's not.
So if the enemy or the collider 2D is missing on this enemy,
than what we're going to do is we're going to access the enemies.
Dot remove at, and we're going to remove it at the position I.
So again, let's just look at it one more time and understand what's happening.
We are starting at the back of the line because we want to remove things from down to,
up until we get to the first one.
So we are starting from the back.
We are using count minus one because count is the number of elements,
but the elements inside are indexed from 0.
So we start at 0, 1, 2,
but the count is three,
so we remove one.
We keep removing one by using AI minus-minus until we
are no longer greater than minus one y. I mean,
when we are less than minus one, why?
Because the last one is going to be 0, so minus1,
when we become minus1,
that means we've ended and we no longer want to continue on with the loop.
And each time we find that the enemies is missing or is null,
we are going to remove them from the list.
Great, Let's save that.
Let's go back into our game and let's go ahead and print out in here the enemies.
Okay, So enemies.gov.
And let's just see what happens.
So back in our game,
I'm going to run the gain.
I have the list of enemies,
I'm going to shoot first 1,
second 1, and the third one.
So let's wait for the books to appear.
There you go. You can see a pop-up. Oh, okay.
So now you can see that the list is empty.
There's something I forgot to say.
So we can see we have these and we didn't look at the Council even that may appear.
So the number of enemies a three, when I shoot them,
one of them dies,
the last element is removed.
That's why we are going from the bottom to the top.
So the number of enemies is to 0.
So the bullets that work for that kept going. Also.
This one, there you go,
The number of elements or the number of enemies is 0.
Great. So now we know that we are able to kill the enemies.
We are able to remove them from the list.
Lastly, we want to open the door whenever the number of enemies 0,
and that's going to be part of your challenge.
Your challenge is to open the doors after the enemies die.
So check to see that the enemies list is empty.
This will be, I don't know,
I will leave it to you.
Then you'll need to go through all the doors and the doors to close.
And you'll need to activate all of the doors.
And I'll give you a small hint.
You need to check the list length.
You can use the dot count.
Well, we've already done this.
If you want. You can use a for each loop to go through the doors or you can try
to use the for loop that we just learned and to get the length of the array,
this is a good exercise in loops.
We have a lot of loops. That's good.
So pause the video right now and go do the challenge.
Okay, welcome back.
So I'm just going to keep the debugger right here.
And what we're going to do,
we're going to check if the enemies dot count is equal to 0.
Then we are going to use the for loop.
I'm going to create an int I equals 0.
So we are starting from the 0.
We are going to keep looping while I is less than the doors to open or sorry,
doors to close dot length.
When we want to access the length of an array,
we use dot length when we want to access the length of the list, we use dot count.
So there you go. Now you have two examples.
You can even add a comment in here to make sure that you remember.
And at each step we are going to add a one.
So now door to close or actually door to close. Yes.
And I'm going to access it using the I because I is increasing every time.
So let's say we already have two doors.
So we have index 0 and index one. We start off at 0.
We get the door to close at 0.
We set active to false.
There we go.
We will no longer have the door.
We check is the I,
which is currently 0, is less than the doors dot length, which is 2.
So yes, 0 is less than 2.
I plus, plus, or actually we do this and then I plus, plus.
We go back here, I is, okay,
so we no longer go back here I is now one is one less than two?
Yes, it is.
We have r equals to 0 dot-dot-dot active,
sorry, I equals 1.
We set active. There we go.
It's false.
Finally, I becomes two.
Is two less than two?
No, it's not. B.
Go out of the loop.
We save all of that.
We go back into our game.
We run the gain,
we have the console right here,
so the number of enemies,
F3, you can see them.
We should 12, okay,
the enemies that we shoot him.
Second one, how many bullets does take?
One? It takes only one. Nice.
Let's see why is he?
Because I remember we added his life to be three.
There you go.
The enemy died.
You can see that at 0 the doors open and we can walk through.
How cool is that?
So finally, everything seems to be working, okay.
There are a couple of bugs.
One of them is the blood is not disappearing because of the animations.
We haven't still added the key at the end.
The skeleton right here.
How big is his life?
The enemies house is three,
so there seems to be a problem.
He is dying immediately. Why?
Because if we go into the layer bullet controller,
the damage amount is 100 k.
So the damage amount is standardized.
Life of the enemies is 3.
Think that's the problem.
So let me make the half of this guy.
So I just want to make sure that we are
not having something wrong save that run the game.
Because sometimes we might call a method too many times so we are giving too much damage.
So if I check, okay, so there you go.
He needs several bullets.
123 and there you go.
Okay, so everything is working fine.
The doors are opening whenever we are killing all the enemies,
everything seems to be working.
We've learned a lot about loops in this section,
we've learned actually a lot of information.
I hope you're enjoying.
I hope you're having fun.
I hope you're learning a lot.
I hope you are doing your research.
I hope you are always committing your changes.
I hope your game developer brains are getting stronger with each and every single video.
I hope you are not very frustrated,
and I hope you are very happy.
So I'll see you in the next section.
Don't forget to add a review.