WEBVTT

00:06.920 --> 00:08.120
Welcome back.

00:08.270 --> 00:14.990
Now we're changing our attributes and we've seen that our health can go into the negatives and we can

00:14.990 --> 00:19.670
also fill up our health bar and continue increasing health.

00:19.700 --> 00:21.070
Same goes for Mana.

00:21.080 --> 00:24.600
In other words, we're not really enforcing any clamping.

00:24.650 --> 00:29.800
Now, there are different ways to enforce clamping, and in this video we're going to learn one of them.

00:29.810 --> 00:33.130
We're going to go into our attributes set.

00:33.140 --> 00:39.590
So I'm going to open my public folder, go to ability system and open Aura attribute set.

00:39.800 --> 00:46.400
And here in the public section, I'm going to override one of the functions that we've inherited from

00:46.400 --> 00:48.260
the attribute set class.

00:48.260 --> 00:51.290
So it'll be virtual and it's a void function.

00:51.290 --> 00:56.960
And the function I'm interested in is called pre attribute change.

00:57.350 --> 01:03.740
Now writer is offering to fill out the rest of it for me, so I'm going to go ahead and let it do that.

01:03.740 --> 01:10.230
And as you can see, it takes in a const reference of a gameplay attribute, the gameplay attribute

01:10.230 --> 01:17.550
struct and a float called new value and notice the float is passed in by reference and it's not a const

01:17.580 --> 01:24.630
reference that should hint to you that we can change new value and something somewhere is going to change.

01:24.630 --> 01:33.210
Now let's go ahead and generate this definition and let it call the super version of pre attribute change.

01:33.210 --> 01:40.920
And I'd like to just see what happens in this function when we change an attribute.

01:40.950 --> 01:46.590
Now, here's the cool thing about having an gameplay attribute here.

01:46.590 --> 01:54.750
In the input parameter list, we can check that attribute and see if it's equal to any of our attributes.

01:54.840 --> 02:01.830
We can use comparison operators to compare gameplay attributes.

02:01.830 --> 02:05.070
They override the double equals operator.

02:05.070 --> 02:14.400
So for example, we could say if attribute double equals and we can get any of our attributes thanks

02:14.400 --> 02:21.210
to the attribute accessors, we can say get health attribute, for example, and we can compare and

02:21.210 --> 02:25.740
see if the attribute is our health attribute.

02:25.860 --> 02:32.490
Now, when any of our attributes change, whether it's from a gameplay effect or from being set directly,

02:32.520 --> 02:40.620
this function will kick off in response and it's called pre attribute change, which means it's kicked

02:40.620 --> 02:43.650
off before the change actually occurs.

02:43.770 --> 02:50.790
Now this gives us the opportunity to do something preemptively, but we really shouldn't be firing off

02:50.790 --> 02:53.820
events or kicking off gameplay logic here.

02:53.820 --> 02:58.680
Epic recommends that we only use this function to do clamping.

02:58.680 --> 03:06.540
Basically, that's really all it's good for now, just for demonstration and educational purposes.

03:06.540 --> 03:11.930
We're going to place a log here, so we're going to say you log, we're going to make it a log temp

03:11.940 --> 03:12.930
with warning

03:15.480 --> 03:23.580
and we're going to say health percent F and we'll pass in new value.

03:24.390 --> 03:28.410
And I'd like to do this for all of the four attributes we have.

03:29.250 --> 03:38.040
So I'm copying it and changing this to get max health attribute and changing the comment to say Max

03:38.040 --> 03:41.220
health and I'll copy both of these.

03:41.340 --> 03:44.460
And instead of health we're going to change it to Mana.

03:44.490 --> 03:51.630
I'm just going to highlight this section and here in writer I can hit control H, I can search for health

03:51.630 --> 04:00.690
and I can replace with mana and I see that I have get health attributes, health get max health attribute

04:00.690 --> 04:01.530
max health.

04:01.530 --> 04:03.210
I'm going to replace all.

04:03.210 --> 04:07.320
And now these are the mana versions.

04:07.560 --> 04:09.020
Okay, cool.

04:09.020 --> 04:16.370
And in Visual Studio it's control shift f I believe to find and replace.

04:16.370 --> 04:22.580
Just make sure you know what you're doing there and I'm going to go ahead and close that mini menu.

04:22.580 --> 04:26.600
And now we're seeing when any of our attributes change.

04:26.600 --> 04:33.260
Now let's go ahead and run this in debug mode and change some attributes and see what we get printed

04:33.260 --> 04:34.700
to the output log.

04:35.640 --> 04:40.860
Okay, I'm back in the editor and I'm ready to test out some changes.

04:40.890 --> 04:47.850
Now let's make sure we understand what each of our aura effect actors are doing right.

04:47.850 --> 04:50.670
We have health potion and mana potion.

04:51.150 --> 05:00.690
And we know that those use these GE potion heal and mana and these are instant gameplay effects which

05:00.690 --> 05:07.020
means we know that they permanently change the base value of those attributes.

05:07.140 --> 05:11.340
Now we also have these crystals and these are different.

05:11.370 --> 05:15.840
They have duration and they also are periodic.

05:15.870 --> 05:22.830
Now, because they are periodic, they act like instant effects, so they also permanently change the

05:22.830 --> 05:23.940
base value.

05:24.570 --> 05:30.150
We also have area the GE Fire area is periodic as well.

05:30.150 --> 05:37.320
So all of these affect the base value permanently because they're either periodic or instant.

05:37.350 --> 05:39.150
Now let's open the output log.

05:39.150 --> 05:43.620
I'm going to dock it in the layout and bring it down here.

05:44.430 --> 05:51.390
So that I can see it and I'll go ahead and press play and let's see what happens if we pick up a mana

05:51.390 --> 05:52.110
potion.

05:52.140 --> 05:55.080
Here we see Warning Mana 40.

05:55.110 --> 06:00.350
Let's pick up the health potion and it says warning health 75.

06:00.360 --> 06:04.770
I'm going to show debug just so we can compare and make sure that these are correct.

06:04.770 --> 06:06.450
And sure enough they are.

06:06.480 --> 06:15.030
Let's go into the fire volume and we see that our health is going down, going down by looks like one

06:15.030 --> 06:16.980
per second as expected.

06:17.070 --> 06:21.030
And let's go ahead and pick up a mana crystal.

06:21.640 --> 06:23.020
And we see it going up.

06:23.020 --> 06:26.470
And let's pick up a health crystal and we see it going up.

06:26.860 --> 06:35.590
So as we can see, we have pre attribute change being called in response to these attribute changes.

06:35.860 --> 06:37.420
Now here's the thing.

06:37.420 --> 06:45.250
It's called pre attribute change, which means we have the opportunity to perform some sort of clamping

06:45.250 --> 06:47.980
before the change occurs.

06:48.220 --> 06:50.830
So if we change this new value.

06:50.860 --> 06:52.630
Well what happens?

06:52.810 --> 06:53.240
Right?

06:53.260 --> 06:55.210
Because we see that it's passed in by reference.

06:55.210 --> 07:00.640
So is that going to actually change the attribute value?

07:00.790 --> 07:02.500
Well, let's find out.

07:02.530 --> 07:08.580
Let's say we don't want our health to ever go above our max health, for instance.

07:08.590 --> 07:14.890
What we can do is we can set new value equal to the result of a clamp.

07:14.890 --> 07:21.800
So f math clamp, we're going to clamp new value.

07:22.610 --> 07:29.030
And we'll clamp it between zero and let's say, our max health.

07:29.030 --> 07:31.100
So get max health.

07:32.400 --> 07:38.460
And now, as our health value changes, it'll never go below zero or above max health.

07:38.550 --> 07:41.100
Let's do the same thing for mana.

07:41.130 --> 07:49.260
So down here, if attribute is the mana attribute, we're going to say new value equals new value but

07:49.260 --> 07:54.790
clamped between zero and instead of max health we'll say max mana like so.

07:54.810 --> 07:58.350
Now let's go ahead and run in debug mode.

07:59.320 --> 08:00.130
All right.

08:00.130 --> 08:03.040
And we're going to go ahead and press play.

08:03.040 --> 08:09.580
I'm going to show debug ability system and let's start picking things up.

08:09.580 --> 08:12.190
Let's go to the nanocrystals first.

08:12.220 --> 08:14.680
We have ten mana max Mana is 50.

08:14.680 --> 08:16.390
I'm going to pick up a health crystal.

08:16.420 --> 08:20.620
We go up to 20 picking another one up we go up to 30.

08:20.650 --> 08:25.150
Another one we go to 40 and another one we go up to 50.

08:25.150 --> 08:28.690
And I think we have a man of potion over here if I pick that up.

08:29.460 --> 08:30.480
Look at that.

08:30.510 --> 08:32.670
We actually stay at 50.

08:32.700 --> 08:35.190
Now that mana potion does work.

08:35.190 --> 08:38.400
Right, Let's pick up the mana potion first.

08:38.430 --> 08:38.940
There.

08:38.970 --> 08:39.240
See?

08:39.240 --> 08:40.650
We see that it does work.

08:40.650 --> 08:44.850
We have 40 mana, but if we pick up a crystal, we go up to 50.

08:44.850 --> 08:48.090
But if we pick up a second crystal, look at that.

08:48.090 --> 08:48.960
Nothing happens.

08:48.960 --> 08:49.260
Why?

08:49.290 --> 08:56.700
Because before the attribute changes, we have the opportunity to do something in pre attribute change.

08:56.700 --> 09:02.700
And because we're clamping it and we can't go above max mana, which is 50, we're preventing that mana

09:02.700 --> 09:04.770
from going above the max.

09:04.800 --> 09:06.180
We're clamping now.

09:06.180 --> 09:10.110
The same goes for the health and it goes both ways, right?

09:10.110 --> 09:13.980
We clamped so that the health cannot go below zero.

09:14.130 --> 09:20.700
But this takes one away every second, which means I'm going to have to wait for Looks like 40 more

09:20.700 --> 09:22.590
seconds to test that out.

09:22.590 --> 09:23.850
That's going to take too long.

09:23.850 --> 09:25.770
So I'm just going to test the other way.

09:25.770 --> 09:27.690
I'm going to pick up a health potion.

09:27.690 --> 09:30.460
I'm going to pick up the health crystal.

09:30.700 --> 09:32.490
Is it enough to push me over?

09:32.500 --> 09:34.030
No, but that's fine.

09:34.030 --> 09:35.590
I believe that it's working.

09:35.590 --> 09:39.610
We saw that it works for mana, so that's pre attribute change.

09:39.760 --> 09:47.530
And again, it's not really meant to kick off any logic, it's really just meant to clamp and that's

09:47.530 --> 09:48.640
what we're going to use it for.

09:48.640 --> 09:53.950
So before the attribute changes, we can perform clamps.

09:53.950 --> 10:02.440
I don't really feel like I need all of these logs and I'm not really going to clamp our max mana and

10:02.440 --> 10:03.160
health.

10:03.160 --> 10:07.920
I mean, I guess we could clamp it so it doesn't go below zero.

10:07.930 --> 10:11.170
But for now I think I'm fine without clamping those.

10:11.170 --> 10:16.420
I'm going to go ahead and remove all of these logs.

10:16.420 --> 10:22.570
I don't feel like we need them, but I'm going to leave our clamping for health and mana.

10:22.570 --> 10:25.660
I think those are useful, so we'll go ahead and leave those.

10:26.490 --> 10:34.490
So pre attribute change is where we can modify the changes to the current value of an attribute.

10:34.500 --> 10:39.000
And this function is called before the change actually happens.

10:39.030 --> 10:44.040
Now that is a significant detail and we'll see why in a second.

10:44.370 --> 10:50.520
Pre attribute change is triggered by changes to attributes, whether that's through attribute accessors

10:50.520 --> 10:56.940
like the setters that we've created thanks to our boilerplate macros or due to gameplay effects.

10:56.940 --> 11:00.900
Literally anything that changes the attribute can trigger this function.

11:01.140 --> 11:06.990
Now, this does not permanently change the modifier for a given attribute.

11:06.990 --> 11:12.510
It just changes the value returned from querying the modifier of the attribute.

11:12.510 --> 11:19.650
In other words, we can clamp changes before the change happens, but there are later operations that

11:19.650 --> 11:27.340
happen after pre attribute change and these will recalculate the current value from all modifiers involved.

11:27.340 --> 11:34.450
So if the value of health changes, for example for any reason, then there are later operations that

11:34.450 --> 11:41.590
will recalculate that health change, which means that our clamping will have no effect and we'll have

11:41.590 --> 11:43.810
to clamp again later on.

11:43.810 --> 11:51.190
So pre attribute change is not the most attractive choice to implement final clamping changes, but

11:51.190 --> 11:52.210
it is there.

11:52.480 --> 11:59.590
Now you may be curious as to what might be the best function to implement something for when an attribute

11:59.590 --> 12:02.470
changes and be sure that that change will be final.

12:02.470 --> 12:06.430
One of those functions is called post gameplay effect execute.

12:06.460 --> 12:12.910
This actually happens after a gameplay effect has changed an attribute and we're going to learn about

12:12.910 --> 12:15.310
this function in the next video.

12:15.340 --> 12:16.540
I'll see you soon.
