Hello guys.
Welcome to the video.
In this one we have to do a couple of more changes to the player manager.
And I want to make on player join functionality.
As of now when we join the player he just chooses the position and somehow it is placed at the starting
point where it's supposed to be.
But we may move the start point, or we may update the checkpoint position and then we need to be sure
player is created at the position we want it to be.
Also, we need to have skins for each player and also which is most important.
We need to remember which player uses which device, right?
And before we do all of that, I want to be able to kill the player just to check some functionality.
So we need to do a bit of a change that we're going to upgrade later on.
As of now, we need to go to the player script.
And over here we have this functionality where we restart the game.
That's not what I want to do.
I want to comment this out and I want to comment this out here as well.
I think we are starting the game from somewhere else.
So I want to double check that by going to Game Manager.
Let me find this restart game method.
Where is it?
Restart level.
It has zero references.
Okay.
It is not used anywhere for now.
Very good.
So now the player dies.
We don't need to restart the game.
Also, I want player to die no matter how many fruits he has.
So I'm going to go to prefab of a player and I'm going to choose difficulty hard.
And on the managers over here on difficulty manager, I'm going to choose hard difficulty as well just
in case.
Very nice.
Now let's go to Player Manager script.
We need to do a couple of changes here.
Uh, first of all, we want to know when player joined the game so we can give it some default variables,
change some maybe position or anything else.
Let me do close all by this.
We can achieve that by, uh, getting component of a player input manager.
And then there is a possibility to subscribe to the event of player joining the game.
Uh, let's go here and let's maybe let's do it above.
I'm going to do public, uh, player input manager, player input manager.
And I'm going to do get private set because we're going to need it in the future from another script.
Now on their wake, let's do player input manager equals to get component of player input manager.
Very nice.
Now we need on enable method.
I'm going to do on enable, and I'm going to do on disable and inside of on enable method we can do
this player input manager on player joined plus equals.
And now we can type name of a method that we want to have.
For example add player.
And then we can uh sorry like that.
And then we can press alt enter or maybe just mouse button.
Quick actions and refactoring generate method add player.
So now this method will be called every time player joins the game.
And also it will have information about the input that were used to join the game.
We can get access to the uh input component to the player component.
Everything can be done through this variable over here.
Let's rename it to player.
Sorry.
And let's move this method below.
Over here.
So it's pretty.
Maybe even here like that.
Okay.
Then on disable.
We want to unsubscribe from this event.
So I'm going to duplicate this.
Cut it out paste it in here and do minus.
Like that.
Very good.
I like it.
So now we have method that is called on player joined which basically is a respawn method.
Now that we have it let's take this on player respawn and walk and place it inside of this method.
And this will update the reference to the player.
However, it is not working perfectly because now we're going to have a bunch of players, not just
one.
And if you remember enemy.
There is the enemy.
Enemy over here.
Just updates transform of a first player in the player manager.
Let's not touch it for now.
Let it just do its job.
It's fine.
Let's just go ahead here and maybe you know what?
Maybe, just in case, I'll do this.
If player is equals to null, then player.
Right.
We need to check this this player because we have player variable over here.
So if this player is equals to null, then this player equals to player getcomponent of a player.
This is a placeholder.
I'm going to remove it later on, but for now we just need it so we don't see any errors.
Okay.
Uh very nice.
Let me place it like that.
And we can forget about this now what we want to have.
We want to update the respawn position when player is added, and then we want to place him at respawn
position.
So first of all, I want to take this.
I want to extract method.
Place player.
Place new player at respawn point and this one will require a transform.
So I'm going to pass transform new player.
Then we can remove it from the start.
We're not going to need it in the start.
Then over here.
You remember we just double checking if we have respawn points so we can keep it.
And after that we're going to do new player dot position equals to respawn point dot position just like
that.
And then we can just go ahead over here and do place new player at respawn point.
And we just need to pass it a transform.
And I'm going to pass player transform like that.
Okay, just to clarify things, when we created a player, uh, on player joined, this method will
be called.
And it will have information about player input component on a player game object.
Which is, uh, right here.
And through this component we can get access to any other component on the player game object.
That's why we're doing it this way.
We're not going to change this method in the nearest future.
So I'm going to cut this out from here and place it somewhere below because we can forget about it for
now.
Okay, let's go ahead and just make sure it's all working.
So I'm going to place it here.
And also by the way we don't need this start method anymore.
So I can just clean this like that.
Okay.
Okay, okay.
Very good.
I'm going to show you a wing, just in case if you want to see this.
This is how it looks right now.
And let's go back to unity.
I'm going to keep first player in the game for now, but later on we're going to just join both of the
players.
Okay, so I'm going to use my gamepad to join the game and I'm there.
I don't see any errors.
Let me heal the player from a gamepad.
Done.
And let me change respawn point.
Let's say we have a checkpoint in the game.
Prefab.
Uh, where is it?
Checkpoint.
I don't see anything.
Let's say checkpoint is, um.
It was updated.
Oh, my.
Wait a second.
Let's say checkpoint is over here, and first player from a keyboard jumped to that point to activate
the checkpoint.
Now it's supposed to be updated on the player manager.
Over here it is.
Checkpoint.
And I'm going to use GamePad to create a new player.
And I'm at checkpoint so it works okay.
Very nice.
Love it.
What other things we want to do on player join?
Well, we want to keep track of players that are in the game, so probably it is a good idea to add
a list of players.
Uh, let's make it this way.
Let's do serialize field.
Try what player?
Actually, we're going to do list of player.
It should be as serialized field.
And I'm going to name it as Player list.
And let's make it equals to new list of players by default.
All right.
Um.
I don't remember if we used the list before in this course, but it's pretty simple to understand.
It's just like an array, but the difference is that you can change list dynamically.
You can change content of the list in the script while you playing the game at runtime, and you cannot
do that with an array, but with a list it is possible.
So now.
We can go to add Player method.
And over here we can get component of a player script.
I'm going to do player.
Um.
Player.
Let's do.
Player.
Player script.
Equals to player.
Get component of a player.
And just like that.
And then we're going to do player list.
And.
Player script.
All right.
So now we're going to have reference to the player script in the player list.
And later on if we need to get one of the players from player manager from enemy or any other place,
maybe camera that can be done through this variable.
So I think since we do need this way, it would be a good idea to go here and make it as a get private
set.
So I'm going to do over here, get private set like that might look a bit confusing, but it's very
simple.
We just combined everything that we did before we made it public with a public getter, private setter.
So it can be changed only inside the player manager.
Then we make it a serialized field so it is visible in the inspector, and also we made it equals to
the new list.
So it has a list every time we start the game.
Because we need to initialize the list.
Uh, that's what we need to do.
Okay, okay.
Now let's go and make on player disconnected or on player removed method.
Because when player getting killed we need to remove him from the list.
So I'm going to go here and make player input manager.
On player left plus equals remove player.
Let's generate method as well.
I'm going to take this method.
Place it over here.
Very nice.
Before I forgot.
Let's copy this line of code.
Paste it in here.
Let's do minus here.
And then we can go to Remove player and do something very simple.
Whereas this should be named as player.
And then we're going to do player uh, with the name of player script equals to player Getcomponent
of a player script.
Sorry, we don't need script here, just player.
And then we're going to do player list, remove player script.
So we're going to take it away from the list okay.
Let's go and double check that.
Let's go now.
Uh, I don't see the list, but I made it a serialized field, man.
Uh, should be should be visible.
You know what?
Let's just.
Not over complicated.
I'm sorry guys.
Let's make it as private without this.
And then to get the list, I'm going to make a public method once I need it.
If I need this list from another script, I'm just going to make a method that would return me this
variable.
Okay, let's go back now.
Now we have player list over here that is empty.
We can go to play mode, and this default player will not be in the list because it was there from the
start of the game, but I'm going to kill it.
Okay, now let me join the new player to the game.
There is a player with a keyboard and there is this player clone over here.
Now I'm going to join another one and there is another player in the list and we can keep track of them.
There is also a couple of errors over here in the console.
You can see.
Let's just ignore them for now.
Uh, they're not so important and they're not, you know, interrupting us from the development.
If we have them in the end of the section, I'm going to fix them.
But in my prototype, they just fixed themselves during the development.
So we have no reason to be worried about this right now.
Now, let's take care of something else.
Something very important.
As of now, we can join unlimited number amount of players to the game and we need to limit that.
Uh, this player input manager has a variable that says limit number of players.
If you make it true, you can set the number.
Max player count can be one, two, three or whatever you like.
The problem is that this variable is read only.
You cannot change it in the script.
You can go here and you can go player input.
Manager max player count equals to one.
And you can see it does not work because it will tell you that it is not accessible.
It is read only.
Yeah.
So we'll have to interfere into the script a little bit.
Let's go over here.
Let's click Edit Script.
And this is unity script.
This is what makes player Input manager work.
We're not going to just, you know, we're not going to look at the script.
It doesn't really matter what is written here.
We just need to do one simple adjustment.
I'm going to look for max player count variable.
And you can see there is this.
To me.
Yeah, there is this public integer max player count that we can use to read the value, and it returns
us the max player count, which is private variable.
It may be confusing.
Don't worry about that.
It doesn't matter.
We just need to make a method that will change this variable.
That's it and we need it many times in our custom scripts.
So let's go just somewhere here and let's do public void set maximum.
Layer count.
And we're going to pass int maximum player count.
And it's going to do something very simple.
It's going to do m player count m max player count equals to maximum player count just like that.
Now we're going to save this.
We can close this script.
And then in the player input manager over here, we can do player input manager.
Set maximum player count.
So now we can use this method that we just created to set up a player account.
Why do we need a method.
Actually why do we want to have it right.
We can just change it in the inspector.
Actually we cannot.
Later on in the main menu we're going to have the.
And the buttons that will define how many players will be there in the game.
And as you know, if you want to do something from a button on the UI, you probably should have a method
because you cannot just go from a button to the inspector over here and change value over here.
That's the issue.
So let's enable this.
Let's set max player count to four by default.
And then later on we're going to change it if we want to.
As of now let's just make sure it's working by doing something very simple.
Over here I'm going to do public int max player count.
I'm going to make it equals to one by default.
And then in the week I'm going to do player input manager.
Set maximum player count to max player count again.
Later on, we're going to call this method from a button, but as of now this will just do fine because
we need to make sure it's working.
Okay.
Let's go back.
I'm going to delete this player from here because I want to be able to join the player from the start.
Let's select player manager and let's go to play mode.
So no players.
Maximum player count one is here.
And also it did change variable over here.
So we are fine.
Now I'm going to join the player.
There is one.
And you can see a message that says maximum number of supported players reached one.
If I try to join another one, I cannot.
However, this message may be irritating in the future.
I don't want to see it every time.
So I'm going to go ahead to this message and I'm going to comment out this line of code.
If you want to uncomment this.
It is very easy to find.
It is just very close to the method we created before over here.
Okay.
Good.
Uh, let's save this.
Let's go back to unity.
And now player will not be joining.
If you have set the limit, there's the first player.
It's all fine.
If you click buttons on a keyboard, it doesn't work.
And now we can set the number to bigger value if you want to.
And that is what we're going to do from the UI later on.
As of now, this is just fine.
There are other things we need to do with the player manager, but it may take more than a couple of
minutes, so I think it's better to do it in the next video and I'll see you there.