WEBVTT

00:00.200 --> 00:02.150
Welcome to your next quest.

00:02.330 --> 00:08.150
Okay, So the project manager for your game project walks up to you and says, My lovely developer,

00:08.180 --> 00:11.930
we need to add XP to our player state.

00:11.960 --> 00:15.830
They then proceed to tell you that we need a member variable.

00:15.860 --> 00:21.230
On top of that, this member variable should be replicated and we're going to need a rep notify.

00:21.380 --> 00:28.610
We'll want a set function that can set the XP to a certain value and an add to XP function so that we

00:28.610 --> 00:34.040
can just pass a value in and let the player state handle adding to XP.

00:34.370 --> 00:40.970
And every time XP changes, we need a delegate that we can broadcast whenever our XP changes on the

00:40.970 --> 00:45.440
player state and our rep notify can help us to broadcast on clients.

00:45.440 --> 00:52.310
So we need some kind of broadcast that's capable of broadcasting some kind of change in stats.

00:52.310 --> 00:56.690
So your project manager says to you, Here's the job I'd like you to get done today.

00:56.720 --> 01:00.030
Thank you very much, and leaves you with these tasks.

01:00.030 --> 01:05.370
So pause the video and handle these changes to your project now.

01:08.060 --> 01:08.900
All right.

01:08.900 --> 01:12.380
So we need some XP and we're going to have that on our player state.

01:12.380 --> 01:14.660
So we have a number of things to do.

01:15.080 --> 01:20.990
I'm going to close the editor and here in writer, I'm going to open up my player folder and get our

01:21.080 --> 01:27.890
player state, both the CP and the header files so we can add this new XP variable.

01:28.220 --> 01:31.940
So first we need the XP and that's going to be an integer.

01:32.030 --> 01:39.220
So this will be right here in the header file and I'd like it to be a private variable.

01:39.230 --> 01:46.670
Now in the scenario when I gave you the quest, I was kind of roleplaying saying that you had a manager

01:46.670 --> 01:51.350
for this project and notice that they didn't give you all the information involved.

01:51.350 --> 01:55.970
And that's kind of common when it comes to being delegated a task from a manager.

01:56.120 --> 02:02.780
Sometimes a project manager doesn't know all the details or leaves the details up to you or just trusts

02:02.780 --> 02:04.600
you to make the decision yourself.

02:04.610 --> 02:06.500
So there's a little bit of uncertainty.

02:06.530 --> 02:09.450
Sometimes you aren't given all of the instructions.

02:09.570 --> 02:11.190
That's how we're going to handle this.

02:11.190 --> 02:15.150
Now our player state has level and it's already private.

02:15.180 --> 02:17.880
We can follow this convention.

02:17.880 --> 02:22.110
In fact, we can copy level and paste it and just rename this to XP.

02:22.380 --> 02:30.450
We're going to call it simply XP and we're going to make a rep notify called on Rep XP and we can really

02:30.450 --> 02:34.440
just copy on rep level and rename this as well.

02:34.440 --> 02:44.070
We'll call this on rep underscore XP with old XP being passed in and with that we can generate the function

02:44.070 --> 02:47.400
definition for it and we have our rep notify.

02:47.430 --> 02:50.160
Okay, so we have our member variable.

02:50.160 --> 02:53.490
We have it replicated now because it's replicated.

02:53.490 --> 03:01.170
There's an important step that wasn't outlined in the quest and that is to add it to Git lifetime replicated

03:01.170 --> 03:01.890
props.

03:01.890 --> 03:06.450
So this is another boilerplate step when adding a replicated variable, right?

03:06.450 --> 03:10.680
So we can add that now we know it will be replicated.

03:10.800 --> 03:18.450
Now, next, we needed to create a setter and an add to function so we can put those in our class.

03:18.450 --> 03:25.110
We're going to put them up here in the public section and perhaps you might decide while you're implementing

03:25.110 --> 03:28.380
this task that, hey, we could probably implement a getter too.

03:28.380 --> 03:29.970
And I'm just going to do that first.

03:29.970 --> 03:32.010
And if you did that, that's fine too.

03:32.040 --> 03:38.170
You could even force inline it like we did get player level and you can just have 32 get XP.

03:39.990 --> 03:44.430
Which can be const and return exp.

03:45.630 --> 03:49.170
Now we need those add two and set functions.

03:49.170 --> 03:51.770
So let's just declare them both.

03:51.780 --> 03:59.910
We're going to have a void function called add to exp which will take an int 32 called NXP and we'll

03:59.910 --> 04:08.730
have a void function set exp which will also take an int 32 called in exp as well, and we can go ahead

04:08.730 --> 04:11.550
and generate function definitions for these both.

04:13.880 --> 04:17.910
And it seems like these would be quite easy to implement, right?

04:17.930 --> 04:23.750
Add to XP means we take our XP and we add to it plus equals in XP.

04:24.350 --> 04:25.500
Pretty simple.

04:25.520 --> 04:28.670
Set XP would probably be even simpler.

04:28.670 --> 04:29.300
Right?

04:29.330 --> 04:31.400
XP equals in XP.

04:31.640 --> 04:38.240
But we know that these functions also serve another purpose because one of our tasks was to create a

04:38.240 --> 04:41.470
delegate to broadcast when these values change.

04:41.480 --> 04:46.700
So what we should do is create a delegate and broadcast it when these values change.

04:46.700 --> 04:50.240
And this probably will bring something to our attention.

04:50.270 --> 04:56.660
We also have a level, and if we want to show that in the HUD, well, we definitely know that we're

04:56.660 --> 05:01.300
going to need a delegate to broadcast when level changes as well.

05:01.310 --> 05:07.250
So as we're doing this, and this is something I've seen in a professional environment, working as

05:07.250 --> 05:13.130
a software engineer is sometimes I'll be given a task like this, and while I'm working on it, I'll

05:13.130 --> 05:18.600
see a need that's similar for another variable, in this case player level.

05:18.600 --> 05:27.090
And as I'm implementing my add to XP and set XP, I may be seeing the need for that for the player level.

05:27.090 --> 05:32.970
And while I'm at it, I'll just kind of take care of that as well by creating an add to level and a

05:32.970 --> 05:36.240
set level, things like that depending on the needs.

05:36.240 --> 05:41.610
And that's of course going to depend on your level of experience and familiarity with the needs of the

05:41.610 --> 05:42.480
project.

05:42.510 --> 05:48.660
Now, before we start going rogue, let's make sure we have our own task assigned to us completed before

05:48.660 --> 05:49.920
doing anything else.

05:49.920 --> 05:52.620
And that's going to involve creating a delegate.

05:52.650 --> 05:54.480
Now, what kind of delegate do we need?

05:54.480 --> 05:59.520
Well, we need a delegate that at least can be bound to in C plus plus.

05:59.550 --> 06:05.760
We don't need to bind to this from blueprints as we're going through our widget controllers.

06:05.790 --> 06:09.900
Those are going to broadcast to our widgets in blueprint.

06:09.900 --> 06:12.960
So this doesn't have to be a dynamic delegate.

06:12.960 --> 06:18.900
It can be multicast or just simply a delegate non dynamic, non multicast.

06:18.990 --> 06:22.020
I'm going to choose to make it a multicast delegate.

06:24.150 --> 06:26.640
That can broadcast one param.

06:27.540 --> 06:37.860
That's going to be the stat that has changed and this can be on player stat changed and it's going to

06:37.860 --> 06:45.630
use an n 32 now with multicast delegates we don't need a comma followed by the param name like this.

06:45.630 --> 06:53.400
But what we can do is an inline comment that kind of says what this is and this is going to be stat

06:53.400 --> 06:54.120
value.

06:54.720 --> 07:01.440
So when one of our stats changes will broadcast that to whoever binds to our delegate and we're going

07:01.440 --> 07:06.300
to have one of them for our XP and that will be a public delegate.

07:06.300 --> 07:09.780
So we can just put our delegates right here.

07:09.810 --> 07:18.720
We'll have an on player stat change and this can be on XP changed and we'll call it on XP change delegate.

07:19.170 --> 07:22.470
And it doesn't have to be blueprint assignable.

07:22.620 --> 07:30.390
Now this delegate can be broadcast whenever we set the value or change the value of XP.

07:30.480 --> 07:35.370
So let's go into Aura Player State and make sure we broadcast our delegate.

07:35.400 --> 07:38.910
When XP changes, that's going to be when we add two XP.

07:39.150 --> 07:46.290
So we're going to say on XP change delegate dot broadcast and we're going to just broadcast the value

07:46.290 --> 07:46.800
of XP.

07:47.460 --> 07:50.970
Now this has to happen any time it changes.

07:50.970 --> 07:53.760
So we're going to do that in set XP as well.

07:53.850 --> 08:02.560
Now XP will be actively set only on the server and since it's a replicated variable, then it'll replicate

08:02.560 --> 08:07.690
down to clients and it's on rep XP function will be called at that point.

08:07.690 --> 08:14.890
That's when we need to broadcast our delegate on the client side so that any HUD elements can respond

08:14.920 --> 08:21.160
and update themselves with the correct XP for our XP bar, for example, on clients.

08:21.460 --> 08:25.840
Now we see that this is a need for level as well.

08:25.840 --> 08:34.450
So now that we've finished our current task, we can see the need for an additional set of changes that

08:34.450 --> 08:37.240
could be added to our project for our level.

08:37.330 --> 08:43.390
Now first of all, it would be nice to have a set and an add to function for level.

08:43.600 --> 08:45.340
I think that is important.

08:45.370 --> 08:47.080
We already have a getter, right?

08:47.080 --> 08:54.310
So we need add to and set and for that reason we could have our getters here, our add two functions

08:54.310 --> 08:56.800
here and our setter functions here.

08:56.800 --> 09:02.500
So in addition to add to XP, we can also have add to level.

09:02.500 --> 09:09.670
So void add to level which can take in an int 32 called in level and I'll go ahead and generate the

09:09.670 --> 09:14.680
definition, but I'm also going to declare a set level.

09:14.680 --> 09:16.420
So void set level.

09:17.750 --> 09:20.840
Which takes an int 32 called in level as well.

09:21.140 --> 09:23.780
And I'll generate a function definition for that.

09:24.230 --> 09:28.400
And these will be similar to their counterparts for XP.

09:28.760 --> 09:37.040
Add two level is going to take level plus equals in level and we're going to need a delegate to broadcast

09:37.040 --> 09:42.530
for it to set level just takes level and sets it equal to end level.

09:42.830 --> 09:47.780
So this one is going to need to broadcast a delegate and so is the rep notify.

09:47.810 --> 09:55.760
So let's make that delegate and thanks to our delegate on player stat changed we didn't call it on XP

09:55.760 --> 10:03.680
changed it's just generic for stats so as long as that stat is an int 32 we can use this same delegate

10:03.680 --> 10:11.240
signature and we'll call this on level changed delegate and we'll broadcast this whenever the level

10:11.240 --> 10:12.020
changes.

10:12.020 --> 10:19.580
So add to level gets it, we'll say on level change delegate dot broadcast and we'll broadcast level.

10:20.150 --> 10:26.300
We'll copy this and we'll broadcast level when we set it and when it replicates.

10:26.300 --> 10:28.460
So the rep notify has it too.

10:28.970 --> 10:29.450
Okay.

10:29.450 --> 10:36.620
So we filled a need for level and that's looking pretty nice and we can follow that same trend for new

10:36.620 --> 10:41.810
stats that we add to our player state, such as attribute points and spell points.

10:41.810 --> 10:47.390
If we want to store those on the player state, we have a nice pattern going where we can create a delegate

10:47.390 --> 10:55.190
for it, make a getter and setters or add two functions and create those variables and their rep notifies

10:55.190 --> 10:56.030
and so on.

10:56.480 --> 10:57.170
Excellent.

10:57.170 --> 11:00.100
So this is the next step in our level up system.

11:00.110 --> 11:04.820
Excellent job implementing it and I'll see you in the next video.
