Welcome back my fellow game developers
to a brand new long video.
We are not going to waste any time.
And this video, what we do is when we kill the enemies,
this is not very exciting.
I'll just tell you, when we kill the enemies,
we pick two random numbers.
That's it.
That's all that we do.
Next video, you'll see why,
but it's a pretty complicated concept
where we learn a new kind of loop.
We learned the truth tables.
So it's very, very worth it.
So that's not waste any more time and let's get started.
Oh, case.
So now we are completing the waves.
It's time to leave the room and go to the next one.
So how are we going to do this?
Let's just take a brief moment to talk about that.
We're going to go into the rooms, the doors.
And as you can see, we have
two doors with two portals behind them.
And this is something that will be the
same in every single level.
I didn't want to add too much thing just to keep
everything less complicated and understandable.
So what we have in here are
the two portals and the two doors.
So what happens when we kill
all the waves or all the enemies in the last wave,
wave, these doors will have the ability to open up.
But as you saw in the game Hades,
we have always two options.
So we have two portals that we can go through 14,
let's say the sharp, the other one
to another enemy arena.
And that's what we want to also do.
We want to have the option to go to one of these two?
So we'll need to set these two portals
beforehand and these two doors also beforehand.
Later on we will be creating maybe some kind
of maybe name appearing once we are next to the door.
So when we open it, we know where we are going.
But for now, let's just focus on actually changing The,
whereas if we click on the Portal and unlock this,
you can see that we have the level 2
goes or the level to load.
So what we'll do is we'll try to
change these as soon as we kill the enemies.
So let's get started.
I'm going to go back into
our code and the first thing I will
do is I'm going to go into the level of exit in here.
And instead of using a string,
we are going to be using a integer.
So an integer.
Why are we working with integers?
Well, because in here we will be
using several methods in the scene manager
that can only taken
an integer parameter that we can work with.
So that's why we are changing it to endanger.
Later on you'll see why all of this is happening.
I'm going to make sure that this is
commented out and we'll
later on we'll come back and change it.
But for now because the loading level is
taking in an integer or you know what, let's keep it.
And let's keep it.
And let's go to the level manager.
And in the loading next level,
instead of a string,
we are going to use an integer.
So the next level is an integer,
and the load scene can actually
take an integer as a parameter.
So everything is working good for now.
The next thing we need to do is we need to set
the two doors or the two-level exits.
And because we already have everything
set up and we know that there will always be two doors.
To just make simple,
I'm going to create
a public and level level
to go to underscore one.
And then I'm going to copy this
and make it the underscore two.
So this is for the second level.
And then I'm also going to create a public level exit.
And this will be the level exit underscore
one and the level exit underscore two.
So now we have the two level exit strategy
and let's go ahead and create a level picker.
So I'm going to scroll down,
but not too much, maybe right here.
And I'm going to say that I have
a public void level picker.
And in here we are going to be
employing a couple of things.
So what we want to do is we want to pick a level.
But that's not as simple as you might think because we,
let say we are currently on the skeleton level.
This Kelly's. Okay.
Let's say we have the option to go,
for example, to the shop.
One of them will be the shop.
We will try to choose a random level.
So we have the option to go to the top on the first door,
but on the second door we get the option to go
to the skull is but we've already been in the Skelly.
So the thing that we need to keep in mind is that every
time we want to have the option to choose another level,
it has to be different
than the current level that we're on.
And not only that,
we need to also have the level.
On the left door different
than the level on the right door.
So essentially, we need three things to keep track of.
The level we're currently on.
The level that will go to if we go through the first door
and the level will go to if we
go through the second door.
So how are we going to do this?
How are we going to implement this?
Finding things while we are still on the same level?
This is where we will use something called a while loop.
So let me tell you what a while loop is and you'll see
how important it is in this situation right here.
So first of all, it's similar to for loops at
executes a block of code a number of times.
Instead of determining the number of times before hand,
we wait until a certain condition is met.
So this is perfect for our case right here,
because we don't know how many times we have
to go through looking for a level,
but we know that we will keep looking while
the level that we want to set on
the first door is different than the current level.
And we will also keep looking while
the second level on
the second door is different than the first one,
which is already and
different than the level we are currently on.
So let me just take you through
the structure of the for loop.
You can see right here,
we have the while.
We open up the brackets,
we put in a condition.
It's exactly the same as the if statement.
And if it is true,
then we will do something called execute and then
go back to the condition, condition change.
If it hasn't, we go back.
So make sure to take precaution whenever using while
loops because they can cause a lot of problems.
You can end up in a endless loop where
the condition doesn't change and your Unity will crash.
Trust me, it happens and happens to me
all the time whenever I'm using while loops.
So make sure you know the problem.
Make sure you always change
the condition and always get out of the while loop.
Anyways, with that said,
let's go back to Unity.
And here, I mean Visual Studio.
And what are we going to do in here?
We are going to say that while level to go
to one is equal to the scene manager,
dot, get active, seen dot index.
Then we are going to go ahead and create
a random random number which is
equal to a random dot range from 0 or actually from one.
What are we using one?
Well, because we know that the scene on index
0 is not something that we would like to go to.
Let me show you what I mean.
If I go back into the File,
BuildSettings, the first one as the title screen,
the new weapon spikes and everything
is available except for the 0,
which, and the, obviously the last one.
So in here, the random we'll start at one.
So from one to the scene manager,
the scene count in Build Settings.
And you can see right here, this is the number
of scenes and the build settings.
And we are going to reduce it by one,
because obviously we don't want the last one in there.
So now that we have a random number of our scenes,
I'm just going to go ahead and print that random number.
So print and let's say the room for the random number,
random number is and first round.
And then in here we are going to set the level
to go one to be equal to rant.
Okay, so what's happening in here?
First of all, we say that while the level to
go is equal to the current build index,
then we are going to enter the while loop,
chosen random number assigned
the level to go if it's still the same,
and then check the condition in here.
If it's still the same,
then again we will create
another random number and assign it to the level to go.
And we will keep doing this.
While the level to go 1 is equal to
the scene manager or the build index
of the scene that is active right now.
Once It's not, once this condition is no longer met,
we will continue on to setting over things.
Before we continue.
Obviously, we need to do something which is set the level
to go one to be equal to the scene manager.
The just copy this one.
So we're just sending it at
the beginning as this one just so we
can enter into the loop and then continue on.
So let's save that.
Let's go back into our gain o and we actually.
We haven't got this yet.
So to call this,
where should we be calling it?
I believe this is part of your challenge.
Well, I think it will be part of your challenge.
So unfortunately we will not be able to test it.
But, or you know what,
let's test it out.
So if I go to where do you think if you want to,
you can make it a challenge.
But unfortunately, I've already shown you.
So in here when we said the debug,
all waves are complete.
Let's just keep this,
remove the common because now we can
actually go ahead and access the level manager.
And I believe it is an instance.
And we will access the level picker.
And now we can actually pick a level and we
should also make sure that the waves are complete.
And we also need to make sure that
the waves are complete.
So how are we going to do this?
This is something we should have
done prior, but no worries.
We are going to create a private variable in
here and waves complete.
And we will set it as false at the beginning.
So private, oh, sorry, Boolean waves complete.
And where do we set it to true?
Down here after all the waves are complete.
So waves complete is equal to true.
And where does this affect everything?
Well, up here, if we
look where we are spawning every single time.
So I'm going to indent this using the tab.
And I'm going to say if waves are
not complete yet, so, right?
Waves complete are not yet complete.
So if this one is still false,
which it shouldn't be false until we
turn into true right here.
So as this keep it as that,
I believe this will work,
but you know what, I'm going to
add the two squiggly lines,
make sure everything is set up properly.
So now, once we destroy or finish all the waves,
Waves complete is done.
We are no longer counting down and we choose a level.
So we'll try to see how that looks in our game.
So we run.
And I believe we should remove
the just to make it faster.
So I'm going to shoot the enemies couple of times.
Wait for the second wave,
which are the cowardly find eyes.
I'm going to shoot the cowardly flying eyes.
And there we go.
And we have one still here.
So once we kill it,
we have the random number three.
And let's open up the console and pause the game.
So if we look in the console,
we have the random number is three.
So obviously this work for the first time.
Let's see what the build index 40s,
Kelly's are, its two.
So three is the boss level.
So we chose another level.
Excellent.
I really want for you to see
the random number being chosen multiple times,
but no worries, we will have a lot of
opportunity for those things.
I'm going to remove this one in
here and the debug right here,
because we already know everything
is working perfectly fine.
But there is obviously
still the second level that we want to set.
So I'm going to issue it to you as a challenge.
And your challenge is to set the level to go to,
to get it pun intended.
So after setting the first-level to go to,
we need to set the second one.
And the second one should be
different than the first level.
And the first-level to go
to and the one we are currently on.
So you'll need to make sure to initialize it.
And I'm going to give you a hint because
we want to make sure that it's different
than the first one and
different than the one we are currently on.
You'll need to use the OR
for the while condition to stop.
And I haven't shown you the sea yet,
so I'm going to show it to you right here.
If you want, you can maybe, I don't know,
take a screenshot or maybe then go back
and see the whole steps of the challenge.
I'm going to now show you
the truth table and the truth table.
So let's start with the end.
So the end a truth table.
How does it work?
If we have several conditions,
as I've said right now,
we need to make sure that it's different than
the current level and
different than the first one we've set.
So it has to be a while loop which checks
for using the all so while level
two that we're going to set is
equal to love and want to go or level two to go,
then we will need to change it.
It needs to be different than both.
So let me show you how the true,
the truth table works.
If you have two true, we get a true.
If we have a false and a true, we get a false.
If we have a false and a false, we get a false,
we have a true and a false,
we get a false.
But when it comes to the, OR,
you'll see that if we have one false and one true, then.
Answer will be true,
but if both of them are false,
then the answer will be false.
So with that information,
I know it's a bit confusing now when we
do actually have the solution for the challenge,
I will explain to you everything,
even though this video is getting very long,
but no worries about that.
Pause the video right now and go do the challenge.
Okay, welcome back.
How did you get on with that?
So in the level manager,
I know this is one of the tougher challenges,
but we are in the Advanced Challenge.
So in here I'm going to say that the wild and level 2,
oops, the level to go to is equal to the level to go one.
Or how's the, or it's these two columns,
or the level to go to is
equal to the scene manager dot index.
So the current one.
Then in here we are going to go ahead
and do the same exact thing.
Copy this, oops.
Paste it in here.
And the random number 1, oops, sorry.
Now, random number 1 and random number two.
And the level to go to is equal to random 2.
And India, we are going to save that the level to go to
is equal to the scene manager dot instance.
Or we can say that level to go to
as equal to level to go one.
But you know what, I don't want to risk
that does just keep things safe.
But, oops, paste it in here,
save that what is happening right now.
So there is a problem because
we cannot use the level exit to,
we need to set the level to go to save that.
So what's happening in here?
We've set the level to go to,
to be exactly as
the scene manager or the index on the current scene.
And in here we check what is
the condition in here, as we've said,
if we go back into our truth table of the off,
let's keep it here and let's
see how everything is looking.
So we start off by checking as level to go to equal two,
level to go one.
And currently the level to go
to or solid level to go one
will be different than the current scene manager.
So this condition is false, which is good.
That means we will not enter into the while loop.
But we then check level to go to,
is it equal to the current build index?
It is.
Then we have a true condition which is right here,
and a false condition which is right here.
So if we look in the truth table,
we have a true condition with a false condition,
that means the whole set of conditions is true.
That means we then go back into the while loop,
and that's exactly what we want.
If we have 11 of these true,
we want to go back and create
another random number and set the random number again.
And we will keep doing that
until this condition is false.
And this condition is false,
because only two conditions that are
false make a total false.
So back in here, if the level to go
to different than the level to go one,
and it's different than the current one.
Only then when both of these are false,
the entire condition is false and we continue on.
So let's go back in here.
We will print those and run
the game and lets you know what,
let's remove 11 manager who has remove one
of the scary guys.
And let's remove this one also and run
the game because we want to demonstrate this pretty fast.
So the enemy will appear in
just a bit so that we will have
only one skeleton to skeletons actually,
once we killed them, we should see that.
There we go. So we call them,
go to the console,
pause the video, pause the game.
I mean, and you can see that we run them.
Number 1 is 3,
the random number 2 is 4.
They are both different.
We've picked levels, make sure to commit your changes,
and I'll see you in the next one.