WEBVTT

00:00.670 --> 00:03.290
Welcome to your next quest.

00:03.310 --> 00:09.990
In this quest, I'd like you to replace the callbacks in our overlay widget controller with Lambdas.

00:10.000 --> 00:16.510
So we have some callbacks for when our attributes change and we're binding those to delegates on the

00:16.510 --> 00:18.130
ability system component.

00:18.250 --> 00:25.840
But I'd like you to actually get rid of those callbacks and instead use lambdas inside of which you're

00:25.840 --> 00:29.240
going to make those delegate broadcasts to the widgets.

00:29.260 --> 00:33.100
So pause the video and conquer this challenge now.

00:36.290 --> 00:36.800
Okay.

00:36.800 --> 00:42.680
So for this quest, we're going to replace some callbacks in the overlay widget controller.

00:43.040 --> 00:47.090
I don't really need gameplay tag container, so I'm going to close those files.

00:47.090 --> 00:51.740
And here we have our callbacks health change, Max, health changed and so on.

00:51.740 --> 00:59.050
And all these functions are doing is broadcasting some values with our delegates.

00:59.060 --> 01:07.100
And by the way, I'm noticing something right here my on max health changed is an on health change signature,

01:07.100 --> 01:09.410
not an on max health change signature.

01:09.440 --> 01:16.070
Something from copy pasting, I think so I'm going to take my on max health change signature and just

01:16.070 --> 01:17.210
replace that there.

01:17.240 --> 01:23.420
Now the reason it still worked is because this is a separate variable than this and they're both just

01:23.420 --> 01:26.630
delegates that can broadcast a float.

01:26.630 --> 01:34.760
So really there's no reason that we can't use the same delegate for all of our attributes and just create

01:34.760 --> 01:38.520
a new variable of that same type for each one.

01:38.520 --> 01:41.460
So maybe we'll refactor that here as well.

01:41.460 --> 01:49.230
But for the task at hand, we need to get rid of these delegates and replace them with lambdas.

01:49.260 --> 01:53.010
Now, the most important thing here is that our lambda has the correct signature.

01:53.010 --> 01:57.030
Otherwise it can't bind to a delegate that requires this signature.

01:57.030 --> 02:05.850
And that's a function signature with an input parameter of const reference to on attribute change data.

02:05.910 --> 02:12.720
So I'm just going to copy that input parameter and I'm going to come back up to where we are binding

02:12.720 --> 02:14.520
callbacks to these delegates.

02:14.610 --> 02:17.010
And I'm going to start with the health.

02:17.010 --> 02:24.240
And all we need to do here is instead of calling Add new object, we're going to call add lambda instead.

02:24.240 --> 02:28.770
And the lambda goes right here inside of the parentheses.

02:28.770 --> 02:34.980
So I'm going to put those each on new lines so we can put the lambda right here in between.

02:35.340 --> 02:42.930
Now it's going to have square brackets, it's going to have parentheses and it's going to have a body.

02:42.930 --> 02:49.020
And in the parentheses that's where we put what I copied, the const reference called data.

02:49.020 --> 02:55.860
And I'm going to put the body on a new line so we can see the scope of this lambdas body quite easily.

02:55.860 --> 02:58.380
And down here to health change.

02:58.380 --> 03:02.970
All I want to do is broadcast my on health changed delegate.

03:02.970 --> 03:04.340
So I'm going to paste that in.

03:04.350 --> 03:08.790
Now of course I get the red squiggles because it says this is not captured.

03:08.790 --> 03:14.520
Remember, the capture goes into the square brackets and we can capture this and that's going to take

03:14.520 --> 03:15.150
care of it.

03:15.150 --> 03:21.330
Now the cool thing is, while, yes, I put these all on a bunch of different lines, this is all very

03:21.330 --> 03:21.930
compact.

03:21.930 --> 03:26.940
We could put this all on a single line and keep this really nice and neat if we want.

03:26.970 --> 03:29.130
Here's what it looks like if you do that.

03:30.140 --> 03:32.960
We'll just go ahead and delete the empty space.

03:35.300 --> 03:36.350
Like so.

03:37.310 --> 03:42.000
And one more thing that closing parentheses there.

03:42.020 --> 03:44.600
So here's what it looks like on a single line.

03:44.630 --> 03:50.030
Now, as you can see, we have quite a few tokens just bunched up.

03:50.030 --> 03:52.100
So I prefer not to do that.

03:52.100 --> 03:53.600
But if you wanted to, you could.

03:53.630 --> 03:58.700
So I'm going to hit Control Z a few times until it gets back to how it was.

03:59.300 --> 04:01.250
And I think that's it.

04:01.340 --> 04:02.390
So we're good.

04:02.690 --> 04:07.010
And now our callback health changed is obsolete.

04:07.040 --> 04:08.070
We don't need it.

04:08.090 --> 04:09.350
I'm going to remove it.

04:10.500 --> 04:12.810
And I'll remove it from here as well.

04:12.960 --> 04:17.080
So this is a nice way to clean up our widget controller.

04:17.100 --> 04:17.940
Right.

04:18.150 --> 04:21.980
So we can do the same thing for our maxhealth.

04:21.990 --> 04:28.750
So I can copy this being extremely careful because copy and paste can be dangerous.

04:28.770 --> 04:31.250
So we're going to go through the entire line.

04:31.260 --> 04:33.630
We see that we're getting the health attribute.

04:33.660 --> 04:39.000
We want to get max health attribute and we're adding a lambda.

04:39.090 --> 04:44.910
And what we want to broadcast is on max health changed like so.

04:45.030 --> 04:50.490
So we're done with this one and we're done with Max health changed.

04:50.520 --> 04:55.140
We don't need that and we don't need the declaration as well.

04:55.260 --> 04:56.460
We'll delete that.

04:56.820 --> 05:01.500
Okay, so two more we want manage changed and Max manage changed.

05:01.620 --> 05:10.200
So I'm going to copy this and we have to change the getter for the attribute we want get mana attribute

05:10.300 --> 05:12.610
and we want to change the delegate.

05:12.640 --> 05:14.710
We want on mana changed.

05:14.710 --> 05:16.270
And then last one.

05:18.400 --> 05:28.030
It's going to be get max mana attribute and on max Mana changed and we can get rid of these lines where

05:28.030 --> 05:34.960
we're binding callbacks instead we're now binding lambdas and we can get rid of these callback functions

05:34.960 --> 05:37.600
altogether so we'll get rid of them.

05:39.550 --> 05:44.110
Okay, so we've just reduced the amount of code quite a bit.

05:44.110 --> 05:49.620
And again, we could make this four lines of code right here if we wanted to.

05:49.630 --> 05:55.990
It doesn't have to take up this much space, but I prefer to be able to kind of see what's going on.

05:55.990 --> 05:57.730
So I'm going to leave it like that.

05:57.730 --> 06:01.750
And now we see that thanks to Lambdas, we don't need callbacks.

06:01.750 --> 06:06.520
We can just make an anonymous function that broadcasts a delegate for us.

06:06.550 --> 06:07.690
This is kind of nice.

06:07.690 --> 06:11.860
It's a little bit cleaner and I prefer this way for a lot of things.

06:11.860 --> 06:14.740
And that concludes the challenge.

06:14.740 --> 06:21.290
Now, before we wrap up, though, I'm going to go back to this issue where we have four delegates that

06:21.290 --> 06:22.850
are essentially identical.

06:22.880 --> 06:25.370
The only thing that's different is their names.

06:25.370 --> 06:32.240
And down here, we're creating one of each kind to broadcast an attribute value.

06:32.240 --> 06:38.540
And really, because these are all functionally identical, we don't really need a delegate for each

06:38.570 --> 06:39.470
attribute.

06:39.470 --> 06:43.040
In fact, this project is going to have more attributes.

06:43.040 --> 06:45.260
They're going to grow in number.

06:45.410 --> 06:50.150
So really, we don't need to declare a new delegate each time.

06:50.150 --> 06:52.280
We can just use a single delegate.

06:52.280 --> 06:54.770
In fact, I'm going to declare one here.

06:54.770 --> 07:01.970
It's going to be called F on attribute changed signature.

07:02.440 --> 07:10.600
And we'll call this float new value and then we can just have one of these for each of these instead

07:10.600 --> 07:13.200
of each of them being a different type.

07:13.210 --> 07:17.290
Each of these can all be the on attribute changed signature.

07:17.290 --> 07:22.720
We can change them all to this and we can come back here and delete these four declarations.

07:22.720 --> 07:27.340
And now we just have a single delegate capable of broadcasting a float.

07:27.340 --> 07:31.780
That's all we care about our delegate being able to do.

07:31.780 --> 07:36.820
And for some reason I think I've deleted a semicolon down here.

07:36.820 --> 07:38.050
So I'm going to fix that.

07:38.050 --> 07:44.350
And with that, I should make sure this all compiles because we've made quite a few changes here.

07:44.350 --> 07:53.440
So compiling and the build succeeds because all of these broadcasts just take floats and they work.

07:53.590 --> 07:59.680
But we're going to want to play test because when you make this many changes, you should really test

07:59.680 --> 08:00.220
out your work.

08:00.220 --> 08:05.360
But before we do take a look, our widget controller looks a lot better.

08:05.390 --> 08:06.710
It's a lot cleaner.

08:06.710 --> 08:10.880
We have less delegate declarations and we have less functions.

08:10.880 --> 08:13.940
In fact, we have no callbacks at all.

08:13.970 --> 08:15.890
We just bind lambdas.

08:15.890 --> 08:16.820
So that's cool.

08:16.820 --> 08:20.000
Let's hit the debug button and test it out.

08:21.280 --> 08:22.090
All right.

08:22.090 --> 08:28.990
So I'm going to press play, and pressing play is a good way to find out if we have some errors in the

08:28.990 --> 08:29.930
blueprints.

08:29.950 --> 08:36.130
I'm going to open all errored blueprints and my message log gives me links to where those errors are.

08:36.160 --> 08:38.710
I'm going to click on the one for Health Globe.

08:38.710 --> 08:43.270
And really, it's just because our delegate has changed.

08:43.300 --> 08:49.600
All I need to do is right click and refresh nodes and make sure that those connections aren't broken.

08:51.620 --> 08:55.160
And I have another one down here on Maxhealth changed.

08:55.190 --> 08:57.500
We can refresh the nodes here.

08:58.710 --> 09:00.030
And hook that up.

09:00.910 --> 09:02.860
Compile again, and we're good.

09:02.920 --> 09:04.240
Now that's the health globe.

09:04.240 --> 09:07.170
Let's go to the Mana Globe and compile here.

09:07.180 --> 09:10.960
Actually, I see the two errors, refresh nodes here and hook up.

09:10.960 --> 09:12.700
Refresh nodes here.

09:13.530 --> 09:14.700
And hook up.

09:15.940 --> 09:19.660
Compile and we're good and we just need to play test.

09:19.660 --> 09:24.490
I'm going to save all and then test and pick up mana.

09:25.680 --> 09:27.630
We're getting Mana looks good.

09:27.630 --> 09:28.890
Let's pick up health.

09:28.890 --> 09:30.240
We're getting health.

09:31.060 --> 09:32.500
Looks great.

09:34.160 --> 09:35.420
And everything works.

09:35.420 --> 09:36.200
Excellent.

09:36.200 --> 09:37.940
So awesome job.

09:38.030 --> 09:40.400
I'm proud of you for tackling the challenge.

09:40.400 --> 09:44.000
It was a pretty good one and I'll see you in the next video.
