Welcome back.
My favorite game developers in this video, we are going to be creating a level manager will learn what
it means when we want to delegate scripts, where we want to create different scripts for different
things to do.
We'll also learn about methods and public methods and how scripts can communicate with each other,
even though they have private variables.
So let's not waste any more time and let's get started.
Okay, so currently we can see that when we run our game, everything is working fine and we can move
from one level to the next.
Now we have a small problem that the camera is not following the player when he goes to the next level,
but will fix that later on when we learn while loops.
For now, let's focus on creating a level manager.
Why do we need a level manager?
Well, when it comes to programming and creating games specifically, you always need to find a way
to delegate your code.
So we don't want our player to always be keeping track of everything.
We want our player to have to deal with finding the bounds of our level.
So what we'll do is we'll create a level manager that will do that for the player and then send the
information of the edges to the player using methods.
So let's first of all, create a level manager.
So in the hierarchy will create an empty game object will call it the level manager and will reset its
position to zero zero zero on all sides.
Next, we are going to create a script so and the scripts will right click create a C sharp script.
It's going to be called The Level and without any spaces.
Manager hit Enter wait for the script to compile.
We will add it to the level manager and let's go ahead and the proof of it before we forget because
we will be needing the level manager and all of our scripts.
Now let's open up the script and let's see what we are going to do here.
So the script will be responsible for determining what the edges are of our level.
So first, of course, we'll need to add a file maps.
So this will give us the ability to access tile maps.
Next, we'll need around the same variables that we have in the player.
So we'll just copy these and paste them in here in the player.
And let me just move this up in here and there.
And there we go.
So now we have a reference to the tile map, the bottom left edge and the top right edge.
But you might be telling me right now, how are we going to communicate between them or even before
that?
Let me just remove the burden from the player.
And I just cut them using control X and I will place them in here and they start.
So whenever the level manager starts, it gets the tile map that we have in here and it sets the bounds
of it.
OK, great.
So now that we've finished with that, you might be asking me, well, Michael, how is player going
to know what the bottom left edge are and what the top right edge is?
So even though we have this private and we have these in here also private, how are they going to communicate?
Well, they will communicate using something called a method.
Let me explain what methods are.
So just like a variable method also have a similar structure.
First of all, we have the access type.
It's the way accessibility of our method.
So can we access it from the same script only that means it's a private.
If we can access it from multiple scripts, that means it will be a public.
Next, we have a return type.
So just like we have a variable type, we also have for a method, a return type, and it can return
a string and integer a float.
So when we finish calculating things in our method, we can return the next.
We have the name of our method and just like we name our variables, we also have a way of naming our
methods by always making every single word of the method be capitalized.
So adding velocity, the AI and the V are capitalized and we also always name our methods by what they
do.
So adding velocity adds velocity.
And finally, a very powerful tool of methods is that we can send parameters into them.
So if we, for example, need to have two things that need to be calculated, we can send them in the
parameters and in the other script we can calculate them.
We'll see how all of that works when we do an example.
So back in our script in here, we can actually remove the tile map because we won't be needing it in
the player and the using library to save a bit of memory.
So we're going to create a method.
And the player that will be able to accept the bottom left edge and the top right edge and said so will
create a public because we want to be calling it from the level manager, we are going to make it void
and then we are going to call it this set limit.
And this settlement is going to take to arguments.
The first one is going to be a vector three and it's going to be the.
Bottom left or the bottom edge to.
Set and another one, which is going to be the top edge to set.
So now that we have these variables being passed in, we can access the bottom left edge of our player
or the variable on the player up here.
This one, and we are going to set it as the bottom edge to set, we'll do the same with the top edge.
The right edge is equal to the top edge to seventh.
Great.
So as you can see, we have a public method which has no return and it's called set limit.
Notice how set how the S and the L are both in the upper case.
We have two arguments that are being sent in here and we are setting our variables based on the arguments
or the parameters that are being sent to it.
Now, the next step is to actually call that method in level manager.
And I'm going to issue you a challenge to do that.
So you'll need to call the method settlements.
First of all, go to the level manager script acts as the instance from the player script, access the
method, set limits and give it the parameters.
So whenever we finally set our edges using the level manager, we need to call the method player and
tell it that these are edges.
Here you go using the parameters.
So does the video right now and go do the challenge.
OK, welcome back.
How did you get on with that?
So, first of all, we are going to access the instance of the player, then we are going to call the
set limit.
And in here will need to pass the two parameters.
So the first parameter is going to be the bottom left edge and the second parameter is going to be the
top right edge, and we'll save that.
So now what happens is when we run the game and the level manager starts, it takes the bottom edge
or it calculates the bottom edge, it calculates the top edge.
It calls the player.
It accesses the instance.
It sets the limit, which is the method in here.
So it calls the method in here and it sets both of these parameters.
So what happens then?
When we call this method, we jump into the player script.
We said the bottom left edge, the top right edge, and now we can use them in here.
And this is just a small line that I used in order to make sure everything was working correctly will
save all of that.
And now let's go into our game and see if that actually works.
So level manager should have a place for us to add the background will set it in here.
We'll save it.
Let's see if that works or we run the game.
And there you go.
Yep, there you go.
We can walk down in here now.
Let's go into the second scene, the mountains.
Let's add a level manager in here also, and let's give it the background.
So that say that if we run the game, let's see if we can work out, nope.
There you go.
We do not walk through everything.
How cool is that?
And another very important thing to know is that if we save these variables on the player and the player
doesn't get destroyed, the values of the map are not changed.
So it's very important to have a level manager separated from the player.
Now, the final thing we need to test is if we go from town to to the mountains.
Let's see when we go.
Yep, we can see that we don't have the camera following the player, we will fix that, as I said,
using the wire loop when we learn them.
But as you can see, we cannot cross the edges, neither on up edge, neither on the left and neither
down on the bottom.
So with that done, I hope you enjoyed in the next video, we are going to be learning about canvas
and unity.
I'll see you then.
Make sure to complete your changes.