WEBVTT

00:00.020 --> 00:00.500
Hello guys.

00:00.500 --> 00:01.400
Welcome to the video.

00:01.400 --> 00:04.370
In this one we need to fix one of the bugs we have for a while.

00:04.370 --> 00:09.080
And I've been ignoring it for a while, but it just, you know, I did not have a great chance to fix

00:09.080 --> 00:09.170
it.

00:09.170 --> 00:11.120
But now we have an amazing chance.

00:11.120 --> 00:16.250
Because now I can show you what is, uh, event action and how to use it.

00:16.250 --> 00:20.810
It's a very nice feature that is very convenient, and it can be pretty useful if you use it.

00:20.810 --> 00:21.470
Right.

00:21.470 --> 00:26.870
So first of all, I want to show you a debug that we have on the prefab of a player manager.

00:27.470 --> 00:34.220
I'm going to set respawn delay to 1.5 seconds and let me drop another enemy on the scene.

00:34.220 --> 00:37.160
I'm going to take this chicken and I'm going to place it over here.

00:37.640 --> 00:39.410
So now I'm going to go to play mode.

00:39.410 --> 00:44.870
And once I die, you'll see that we have an error that says that we don't have access to the player

00:44.870 --> 00:46.850
transform over here.

00:46.850 --> 00:49.100
Missing reference exception.

00:49.100 --> 00:55.130
This happens because we try to get access to the player transform on the player manager, and at the

00:55.130 --> 01:01.880
moment when we kill the player, the player is dead and we do not have anything on the player manager

01:01.880 --> 01:02.660
field.

01:03.080 --> 01:03.200
Uh.

01:03.200 --> 01:04.220
Over here.

01:05.370 --> 01:07.020
You can see we have it now.

01:07.020 --> 01:09.720
But if I die, it says missing.

01:09.750 --> 01:10.260
Sorry.

01:10.260 --> 01:11.100
Once again.

01:16.080 --> 01:19.380
You can see it says missing and that's why we see an error.

01:19.380 --> 01:20.670
So we need to fix it.

01:20.670 --> 01:24.150
And there is a very simple and convenient way to fix it.

01:24.150 --> 01:26.010
Now let's go to the scripts.

01:28.230 --> 01:29.190
Enemy chicken.

01:29.850 --> 01:32.730
And we're going to go ahead.

01:34.630 --> 01:39.760
To, uh, Layer manager one second.

01:40.330 --> 01:41.590
Layer manager.

01:41.590 --> 01:49.000
And at the top over here I want to make a public static event action.

01:50.210 --> 01:54.170
And I'm going to name it as on layer respawn.

01:54.680 --> 01:59.900
So now we've made an action and we can invoke it every time we respawn the player.

01:59.900 --> 02:01.280
That's the first step.

02:01.990 --> 02:05.470
Let's go here at the place where we respawn the player.

02:06.810 --> 02:08.340
On player respawn.

02:08.340 --> 02:10.110
Question mark Doh't.

02:10.110 --> 02:10.950
Invoke.

02:11.570 --> 02:15.590
So now this will be invoked every time we respawn the player.

02:15.590 --> 02:18.830
And also now we can subscribe to this event.

02:18.830 --> 02:22.640
And every time it is invoked we can do something else.

02:22.640 --> 02:26.420
It's like we sort of attaching another method to this event.

02:26.420 --> 02:32.390
And every time this event is invoked, we're going to also call that method that is attached to this

02:32.390 --> 02:33.380
event okay.

02:33.380 --> 02:38.840
And I know it's all confusing and maybe have lots of questions, but in general it's just like that.

02:38.840 --> 02:39.680
That's how you use it.

02:39.680 --> 02:41.270
And it's very simple to deal with.

02:41.270 --> 02:45.770
You make an action, then you go and invoke it at the place you want it to invoke.

02:45.770 --> 02:52.490
For example, we want this event to happen every time we respawn the player, just like we did here

02:52.490 --> 02:53.330
and now.

02:53.330 --> 02:59.960
Every time this will be invoked, all of the, uh, methods that we subscribe to this event will be

02:59.960 --> 03:00.410
called.

03:00.410 --> 03:04.010
So let's go to, uh, enemy over here.

03:04.720 --> 03:07.960
And we have this method update player ref.

03:07.960 --> 03:10.630
So what we can do here is go to start.

03:11.730 --> 03:20.430
And we're going to do player manager on player respawn plus equals update players ref just like that.

03:20.430 --> 03:27.570
And maybe let's rename it to Update players update player reference just a little bit better.

03:28.110 --> 03:33.750
Now we don't need to do invoke repeating, because this will be called every time we invoke that event.

03:33.750 --> 03:38.640
And maybe we want to unsubscribe from the event every time enemy dies.

03:38.640 --> 03:47.850
So somewhere here I'm going to do player manager on player respawn minus equals update player reference.

03:48.300 --> 03:48.900
All right.

03:48.900 --> 03:49.620
Very good.

03:49.620 --> 03:51.270
That's all what we need to do.

03:51.270 --> 03:53.220
Now let's go back to unity.

03:54.430 --> 03:57.340
And, uh, we're going to go to play mode.

04:06.000 --> 04:06.630
Q.

04:07.990 --> 04:12.400
Going to get some fruits and we have a player manager reference here.

04:12.400 --> 04:16.240
But if I jump into the gap, I'm going to kill the player.

04:16.240 --> 04:21.310
It's going to say missing, but we do not have any errors in here at the moment.

04:21.550 --> 04:24.700
If we go to debug mode on the enemy chicken.

04:28.930 --> 04:31.150
We can see that player transform.

04:31.150 --> 04:32.350
Says none.

04:32.380 --> 04:34.480
Now let's unpause the game.

04:34.630 --> 04:40.900
I'm going to respond and we can see player chicken now has reference of a player clone transform, which

04:40.900 --> 04:45.820
is this one and same reference we have on the player manager over here.

04:45.850 --> 04:46.750
Isn't it great?

04:46.750 --> 04:51.880
It is great and probably I should do it like that in the first place.

04:51.880 --> 04:56.830
But I didn't because sometimes, you know, like my brain is not working.

04:56.830 --> 04:57.550
So yeah.

04:57.550 --> 04:59.440
Thank you so much for paying attention.

04:59.440 --> 05:01.150
That's all what I wanted to do in this video.

05:01.150 --> 05:03.370
Just a quick example on how to use action events.

05:03.370 --> 05:05.950
And let me know if you want to know more about action events.

05:05.950 --> 05:11.950
Maybe I can make some video for that and I will explain things in details, but as of now, we don't

05:11.950 --> 05:13.150
need to know much about them.

05:13.150 --> 05:18.010
We can just use this simple example because we don't really need other functionality from them.

05:18.010 --> 05:20.200
Okay, well see you in the next video.
