WEBVTT

00:00.050 --> 00:00.470
Hello guys.

00:00.470 --> 00:01.340
Welcome to the video.

00:01.340 --> 00:04.970
And now we have one slight problem in the game.

00:04.970 --> 00:08.120
As of now we cannot know who is player one.

00:08.120 --> 00:09.290
Who is player two.

00:09.320 --> 00:10.580
What I mean by that.

00:10.820 --> 00:13.550
Let's say I joined with the GamePad as a player one.

00:13.550 --> 00:15.950
This is my first layer, then I.

00:15.980 --> 00:17.870
Sorry, I forgot to change player count.

00:19.310 --> 00:23.330
Okay, so then I joined with a keyboard as a second player.

00:23.330 --> 00:24.920
That is my second player.

00:24.920 --> 00:33.440
Now, if one of them dies and then second one dies, now we don't know who was player number one, who

00:33.440 --> 00:34.520
was player number two.

00:34.520 --> 00:40.160
And if you join a game with a keyboard first, which is supposed to be the second player, now this

00:40.160 --> 00:42.140
will be a first player you can see in the list.

00:42.140 --> 00:43.340
It is a first player.

00:43.340 --> 00:44.900
So this what we need to fix.

00:44.900 --> 00:46.730
We need to remember the player number.

00:46.730 --> 00:49.190
We need to remember the players device.

00:49.190 --> 00:54.560
And also if we know player number we can give it a skin that player is supposed to have.

00:55.420 --> 00:58.480
That he will choose in the menu before game starts.

00:58.480 --> 01:00.100
So let's do that.

01:00.100 --> 01:05.140
First of all, let's change max player count to two because we're going to have two players.

01:10.040 --> 01:11.930
And now let's go to player manager.

01:11.930 --> 01:14.120
And I'm going to explain how we're going to do this.

01:14.120 --> 01:18.980
So there is no direct way to just get the device that was used.

01:18.980 --> 01:24.200
Maybe there is that I don't know about, but there's no like input device.

01:24.200 --> 01:25.700
Like debugging input device.

01:25.700 --> 01:31.730
We have to check all of the devices that were used for the input, and usually it returns only one device.

01:31.730 --> 01:33.200
I don't know, this is how it works.

01:33.200 --> 01:36.050
So what we have to do here is something very simple.

01:36.920 --> 01:42.440
We need to go over here and we can do for each var.

01:43.420 --> 01:45.640
Device in player.

01:46.730 --> 01:47.660
Devices.

01:47.660 --> 01:49.130
We're going to do debug log.

01:50.930 --> 01:52.790
Device dot name.

01:53.240 --> 01:55.190
So we're going to see what it does.

01:55.190 --> 01:56.270
And.

01:58.730 --> 02:00.530
Now I'm going to join the game.

02:01.460 --> 02:04.370
And we can see a message Xinput controller windows.

02:04.370 --> 02:06.110
Same goes for keyboard.

02:06.500 --> 02:10.130
Um, again, this is how we got to do it.

02:10.130 --> 02:11.030
There is no other way.

02:11.030 --> 02:15.620
Like maybe there is a way that I don't know about, but I couldn't get the device straight ahead from

02:15.620 --> 02:18.320
a like player device.

02:18.320 --> 02:20.030
You can see there is no such variable.

02:20.030 --> 02:21.530
So we're going to use for each.

02:21.620 --> 02:22.910
But it doesn't matter.

02:22.910 --> 02:24.680
It still works very well for us.

02:25.500 --> 02:30.450
Just something that I'm very curious about, but I can then get the device directly.

02:30.450 --> 02:36.480
Maybe because there may be a device that has two like input that can have two devices like keyboard

02:36.480 --> 02:37.740
and mouse, I don't know.

02:37.740 --> 02:40.380
So what we're going to do here, we're going to make.

02:42.250 --> 02:45.640
An array of possible devices.

02:45.640 --> 02:47.260
Let's do serialized field.

02:50.050 --> 02:51.190
Serialize field.

02:51.190 --> 02:53.500
Private string array.

02:54.600 --> 02:59.310
Player device, this one going to have an array of four strings.

03:00.610 --> 03:06.010
Let's go back and I'm making it as four, because I assume there can be maximum of four players in the

03:06.010 --> 03:06.700
game.

03:06.730 --> 03:07.480
Okay.

03:08.480 --> 03:11.270
And this is like a slot for the device.

03:11.270 --> 03:13.010
And I'm going to make them empty.

03:13.040 --> 03:14.450
Empty, empty.

03:14.480 --> 03:15.890
Empty by default.

03:15.890 --> 03:16.910
Just like that.

03:17.330 --> 03:19.760
Um, now we can go back.

03:20.580 --> 03:27.000
And since this is a string that says empty, and also since we can get the name of a device inside of

03:27.000 --> 03:34.590
a player method, we can just check if field is empty and if it is empty, we can take device name and

03:34.590 --> 03:36.330
put it into that slot.

03:36.330 --> 03:37.530
That's what I want to do.

03:37.530 --> 03:39.420
Let's make for each.

03:41.380 --> 03:44.800
VAR device in layer.

03:46.410 --> 03:47.370
Devices.

03:47.370 --> 03:50.730
Maybe let's rename it to new player just in case.

03:51.760 --> 03:54.730
New player a bit cleaner, right?

03:55.570 --> 03:57.550
Easier to understand what is happening.

03:57.940 --> 03:58.600
Okay.

03:59.380 --> 04:04.510
Now inside of this foreach we're going to run for loop for the array of devices.

04:07.700 --> 04:08.450
Okay.

04:08.450 --> 04:13.850
As a length, we're going to use the player device dot length.

04:13.880 --> 04:21.200
So we run in for loop for the each device we run in for loop of the uh, array of the strings we have.

04:23.530 --> 04:26.890
And over here we're going to check each slot in this array.

04:26.920 --> 04:28.120
So we're going to do this.

04:28.120 --> 04:35.440
If layer device with a I is says empty it means we have an empty slot.

04:35.440 --> 04:37.270
We can fill it up with the variable.

04:37.270 --> 04:39.880
And I'm going to do that layer device with the I.

04:40.830 --> 04:43.020
Equals to Devicename.

04:43.020 --> 04:48.690
And now if we fill an app slot with a variable of a device name, it means that we did the job.

04:48.690 --> 04:49.410
The job is done.

04:49.410 --> 04:50.400
We found an empty slot.

04:50.430 --> 04:51.480
We filled it with a device.

04:51.480 --> 04:53.460
We don't need to use this for loop anymore.

04:53.460 --> 04:54.480
Like any longer.

04:54.480 --> 04:55.740
We don't need to check anything else.

04:55.740 --> 04:56.880
We don't need to proceed.

04:56.880 --> 05:00.660
So I'm going to just type here break and this will break the for loop.

05:03.630 --> 05:09.150
Also just so we can better understand why we're doing this, let's debug a message player.

05:09.540 --> 05:16.170
Plus I'm going to do plus uh, I plus one because I want to have a proper player's number and I'm going

05:16.170 --> 05:17.670
to type joined.

05:19.060 --> 05:19.810
The game.

05:24.710 --> 05:27.260
Uh, let's go to unity now and let's see what we have.

05:27.260 --> 05:29.450
As a result, we're going to go back.

05:30.710 --> 05:32.120
Going to go to play mode.

05:34.270 --> 05:38.950
And I'm joining with a gamepad and we can see we've got player device.

05:39.040 --> 05:42.790
Our first layer says Xinput controller windows.

05:42.790 --> 05:48.430
And also there is a message in the console player plus one joined the game.

05:49.090 --> 05:49.720
Okay.

05:50.350 --> 05:52.870
Same goes for keyboard player two.

05:52.870 --> 05:55.300
Join the game and let's go back.

05:56.440 --> 05:56.920
Over here.

05:56.920 --> 05:58.660
I want to remove this plus.

06:00.460 --> 06:05.470
And now let's check if player that joined before rejoining the game.

06:05.470 --> 06:08.950
We can do that by again checking player devices.

06:08.950 --> 06:09.580
String.

06:09.580 --> 06:18.700
Let's do else if player device with the I is equals to device dot name, it means that player with a

06:18.700 --> 06:22.240
device that was recognized before is rejoining the game.

06:22.990 --> 06:30.850
And we're going to do debug log player plus I plus one plus three.

06:31.710 --> 06:32.520
Joined.

06:33.780 --> 06:34.530
The game.

06:35.700 --> 06:38.190
And also, let's just break the.

06:38.990 --> 06:41.990
Or loop because we did detect that player rejoined the game.

06:41.990 --> 06:43.580
Let's go and try this again.

06:45.490 --> 06:49.930
We're going to go to game and I'm going to use gamepad and join player one.

06:49.930 --> 06:51.070
Join the game.

06:52.400 --> 06:54.350
Going to go ahead and destroy the player.

06:54.380 --> 06:58.400
Now I'm going to rejoin and we can see player one rejoin the game.

06:58.400 --> 07:04.250
So now we can have information about same player rejoining the game with the same device, which is

07:04.250 --> 07:07.160
very, very cool and convenient.

07:07.640 --> 07:09.650
Let me do collapse like that.

07:10.680 --> 07:11.520
Okay.

07:11.610 --> 07:18.990
Now, uh, we not only have device being stored in this, uh, array of strings, but also, now that

07:18.990 --> 07:25.950
we have device being stored here, we can get the player's number by just getting access to the, um,

07:25.950 --> 07:27.840
index of the element.

07:28.930 --> 07:33.430
So now let's make a method that would return an integer of a player number.

07:33.430 --> 07:37.540
And then we would use this code over here to detect the player number.

07:37.540 --> 07:41.230
And then we would return the number to null the number of a player.

07:41.770 --> 07:47.530
Uh let's go over here and let's make private int get player number.

07:48.800 --> 07:54.740
We need to pass the player input here, and I'm going to just name it as player device.

07:55.980 --> 07:59.640
Then we're going to need int new player number.

07:59.640 --> 08:01.740
It's going to be equals to zero by default.

08:01.740 --> 08:04.920
But we're going to update the value inside of this for loop.

08:04.920 --> 08:08.430
Let's take this code over here I'm going to cut it out.

08:09.210 --> 08:11.250
Go here, paste it in.

08:12.940 --> 08:17.620
In the end, after four each, we're going to return new player number.

08:17.770 --> 08:20.560
And then we just need to wait.

08:20.560 --> 08:21.220
What is it?

08:29.160 --> 08:30.960
Should not be like that.

08:33.590 --> 08:35.330
This should be new player.

08:35.360 --> 08:37.220
Let's name it as new player.

08:37.640 --> 08:38.120
Okay.

08:40.850 --> 08:46.100
So now over here, instead of debugging a message, we're just going to change the number because we

08:46.100 --> 08:46.910
know how it works.

08:46.910 --> 08:48.050
We know why we're doing this.

08:48.080 --> 08:51.830
We're doing this to check if player joined for the first time or rejoin in the game.

08:51.830 --> 08:54.740
But basically we just need to keep track of the same player.

08:54.740 --> 08:55.310
Right.

08:55.310 --> 09:01.100
So we're going to do new player number equals to I here as well.

09:01.130 --> 09:03.650
New player number equals to I.

09:04.460 --> 09:05.450
Just like that.

09:05.450 --> 09:07.160
And now we can go back.

09:09.160 --> 09:13.540
To this method, and we can use it to detect which player joined the game.

09:13.570 --> 09:15.640
As of now, I'm just going to do a debug log.

09:17.500 --> 09:21.070
Uh, let's do get player number with a new player.

09:22.040 --> 09:25.580
Last message joined the game.

09:26.930 --> 09:27.530
Okay.

09:27.530 --> 09:28.280
Very good.

09:28.280 --> 09:29.870
So let's go ahead and try this.

09:29.870 --> 09:35.390
And then I'm going to show you how to apply skins on players that just join the game.

09:37.820 --> 09:38.510
Uh, zero.

09:38.510 --> 09:39.380
Join the game.

09:39.830 --> 09:41.090
Well, this is the first one.

09:41.090 --> 09:41.870
We know it.

09:42.170 --> 09:45.140
I'm going to just join again.

09:45.170 --> 09:45.560
Zero.

09:45.560 --> 09:46.520
Join the game again.

09:46.520 --> 09:47.480
I'm going to kill it.

09:49.110 --> 09:50.220
Join the game.

09:51.250 --> 09:51.910
Sorry.

09:51.910 --> 09:53.080
Let me kill it.

09:57.000 --> 09:58.590
And join the game.

09:58.590 --> 09:59.790
Now the another one.

09:59.790 --> 10:00.750
Join the game.

10:00.750 --> 10:01.470
Okay.

10:01.680 --> 10:03.690
It works, I love it.

10:03.810 --> 10:09.570
Now let's go back and let's use this number to apply skin on the character.

10:13.400 --> 10:14.870
Uh, how it's going to work.

10:14.870 --> 10:18.680
Well, it will require a bit of a rework, but it's fine.

10:18.680 --> 10:25.880
So on the skin manager over here before we had this, choose skin ID and we could get the skin ID and

10:25.880 --> 10:30.980
set skin ID, but we did it just for one player, so it did work fine just like that.

10:30.980 --> 10:37.640
But now we'll have to change it, and we will have to turn it into an array because we might have multiple

10:37.640 --> 10:38.900
players in the game.

10:38.900 --> 10:43.670
Let's take this chosen skin ID, let's rename it to skin ID.

10:45.280 --> 10:47.590
And let's make it work as an array.

10:48.300 --> 10:48.960
Okay.

10:48.960 --> 10:49.890
This is good.

10:49.920 --> 10:51.720
Now we have to go here.

10:51.720 --> 10:59.160
And instead of setting skin ID directly, we also need to get end player number to set the skin ID.

10:59.310 --> 11:02.100
And now we're going to do something like this.

11:02.100 --> 11:07.380
Skin ID with a player number equals to ID be passing through the method.

11:07.380 --> 11:12.780
And on the get skin ID we need to pass in player number.

11:12.780 --> 11:13.770
And then.

11:14.550 --> 11:16.920
Skin ID with a player number.

11:17.460 --> 11:19.080
Yeah, this is a bit better.

11:19.110 --> 11:20.010
Very good.

11:21.170 --> 11:24.380
Let's save this and we can adjust this manually.

11:24.380 --> 11:27.320
We're going to use menu to change this variable later on.

11:27.320 --> 11:30.440
But as of now let's let's do it manually in the inspector.

11:30.440 --> 11:34.160
And also there are must be some errors that we need to fix.

11:34.160 --> 11:35.030
Yes.

11:35.030 --> 11:35.840
Okay.

11:36.080 --> 11:38.060
Uh, this one going to be fixed later on.

11:38.060 --> 11:39.350
But as of now.

11:40.470 --> 11:41.850
Not to worry about it.

11:41.850 --> 11:44.850
Let's just ask it for zero, okay?

11:44.880 --> 11:46.770
Let me see other errors.

11:49.450 --> 11:53.320
We just need to fix it for now, because later on it's going to be used in a different way.

11:53.350 --> 11:54.790
I'm going to comment this out.

11:56.760 --> 11:58.440
Uh, this one over here.

11:58.440 --> 11:59.610
Select skin.

11:59.970 --> 12:05.520
Let's just, uh, do skin index and pass zero over here.

12:05.520 --> 12:09.510
Because, again, it doesn't matter what we fix in here, it's going to be changed later.

12:09.510 --> 12:12.840
We just want to avoid errors so we can continue development.

12:12.840 --> 12:15.030
Get skin ID with a zero.

12:15.030 --> 12:15.810
It's fine.

12:17.270 --> 12:17.930
Good.

12:17.930 --> 12:19.640
Let's go back to unity now.

12:22.770 --> 12:27.000
And let's go ahead and find managers.

12:27.000 --> 12:29.850
Prefab managers, skin manager.

12:30.330 --> 12:32.610
Over here we have four elements.

12:33.440 --> 12:38.540
And let's say that first layer chose the pink man, second layer chose the ninja frog.

12:38.540 --> 12:40.880
And these two can be empty because it doesn't matter.

12:40.910 --> 12:42.770
Now let's go back to.

12:44.110 --> 12:45.700
Let's go back to player manager.

12:46.030 --> 12:48.760
And over here I'm going to do int skin ID.

12:50.760 --> 12:57.270
Or maybe new player skin ID equals to skin manager dot instance.

12:57.420 --> 13:04.920
Get skin ID and now we need to use player number that we can get with this method.

13:05.010 --> 13:08.160
I could just pass it here but I wanted to do a bit cleaner.

13:08.160 --> 13:10.860
So I'm going to do new player.

13:11.960 --> 13:15.320
Number equals to get player number with a new player.

13:15.320 --> 13:17.360
And then we can use this variable over here.

13:17.360 --> 13:18.680
New player number.

13:18.890 --> 13:23.510
Now that we have skin ID we can pass it to the player to update the skin.

13:23.960 --> 13:24.350
Um.

13:25.530 --> 13:27.120
Let's go to player script.

13:27.120 --> 13:33.630
And over here we have this update skin method which is updating the skin through the saved value in

13:33.630 --> 13:34.680
a player prefs.

13:34.680 --> 13:38.820
But now since we have a bunch of players, it should be used in a different way.

13:38.820 --> 13:43.110
So over here in the start I'm going to remove this update skin method.

13:43.230 --> 13:44.670
I'm not going to use it anymore.

13:46.620 --> 13:50.520
And then we need to go to this update skin method.

13:50.730 --> 13:54.900
And we can see it's not used anywhere now because it has zero references.

13:54.900 --> 13:58.830
And we just need to pass int skin index.

14:00.250 --> 14:01.240
Over here.

14:01.240 --> 14:05.290
And then in the animators over here, we're going to use skin index.

14:05.290 --> 14:05.950
Okay.

14:05.950 --> 14:08.200
Now there is something else.

14:08.200 --> 14:09.490
We're going to have a bug.

14:09.490 --> 14:11.290
Now that we need to fix.

14:11.290 --> 14:12.700
It's going to be very easy to fix.

14:12.700 --> 14:14.350
But first I want you to see this bug.

14:14.350 --> 14:16.840
Before we fix it, let's go to player manager.

14:16.840 --> 14:21.670
And now we're going to get player script that we got before update skin.

14:21.670 --> 14:24.040
And we're going to give it new player skin ID.

14:25.530 --> 14:30.510
So the logic is fine, it's supposed to work, but it's going to have an error message.

14:30.840 --> 14:32.430
Let's go back to unity.

14:33.920 --> 14:35.420
Let's try to join the game.

14:35.420 --> 14:41.780
We know that first player should have a pink man, and I can see there is a null reference exception

14:41.780 --> 14:48.080
that leads us to the, uh, anim line of code over here.

14:48.080 --> 14:54.140
So what happens is something I would say kind of strange, not supposed to be like that, but it happens.

14:54.140 --> 15:00.680
And the problem is that by the time we are trying to update, the skin, player does not yet have a

15:00.680 --> 15:02.330
reference to the animator.

15:02.330 --> 15:06.560
And that's why we can see that message of null reference exception.

15:06.560 --> 15:13.580
We can easily fix it by directly getting the animator at the time of uh, method being called.

15:13.580 --> 15:17.930
I'm going to do Getcomponent in Children of Animator.

15:19.300 --> 15:20.320
Uh, here.

15:20.320 --> 15:21.430
Just like that.

15:21.670 --> 15:23.440
And this will solve the problem.

15:23.440 --> 15:27.250
Let's save this and let's go back to unity.

15:28.630 --> 15:29.980
Going to go to play mode.

15:32.970 --> 15:34.650
And now let me join the game.

15:34.860 --> 15:35.970
There is a pink man.

15:36.150 --> 15:37.380
Very cool.

15:37.470 --> 15:40.170
I'm going to join as a second player on the Ninja Frog.

15:40.260 --> 15:41.910
And this is the GamePad player.

15:41.940 --> 15:42.840
Ninja frog.

15:42.870 --> 15:45.150
Now I'm going to kill the both players.

15:45.150 --> 15:49.140
And now I'm going to join with the GamePad first, which is a second player.

15:49.140 --> 15:53.400
And even though he's joining the first, he's supposed to be player number two.

15:53.430 --> 15:54.690
Let's check this.

15:54.990 --> 15:55.410
Yes.

15:55.410 --> 15:56.310
Player number two.

15:56.340 --> 15:58.260
So everything works fine.

15:58.260 --> 16:00.750
I love it again, ignore this message for now.

16:00.750 --> 16:02.130
We're going to fix it later on.

16:03.520 --> 16:05.080
And we did achieve the goal.

16:05.080 --> 16:05.500
Now.

16:05.500 --> 16:08.590
Now we can know the information about layer number.

16:08.590 --> 16:10.780
We can update the skin if we want to.

16:10.780 --> 16:13.510
And all of this works perfectly good for us.

16:13.750 --> 16:17.950
Okay, let's save the project and let's go to the next video.
