Welcome to your next quest.
Okay, So the project manager for your game project walks up to you and says, My lovely developer,
we need to add XP to our player state.
They then proceed to tell you that we need a member variable.
On top of that, this member variable should be replicated and we're going to need a rep notify.
We'll want a set function that can set the XP to a certain value and an add to XP function so that we
can just pass a value in and let the player state handle adding to XP.
And every time XP changes, we need a delegate that we can broadcast whenever our XP changes on the
player state and our rep notify can help us to broadcast on clients.
So we need some kind of broadcast that's capable of broadcasting some kind of change in stats.
So your project manager says to you, Here's the job I'd like you to get done today.
Thank you very much, and leaves you with these tasks.
So pause the video and handle these changes to your project now.
All right.
So we need some XP and we're going to have that on our player state.
So we have a number of things to do.
I'm going to close the editor and here in writer, I'm going to open up my player folder and get our
player state, both the CP and the header files so we can add this new XP variable.
So first we need the XP and that's going to be an integer.
So this will be right here in the header file and I'd like it to be a private variable.
Now in the scenario when I gave you the quest, I was kind of roleplaying saying that you had a manager
for this project and notice that they didn't give you all the information involved.
And that's kind of common when it comes to being delegated a task from a manager.
Sometimes a project manager doesn't know all the details or leaves the details up to you or just trusts
you to make the decision yourself.
So there's a little bit of uncertainty.
Sometimes you aren't given all of the instructions.
That's how we're going to handle this.
Now our player state has level and it's already private.
We can follow this convention.
In fact, we can copy level and paste it and just rename this to XP.
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
just copy on rep level and rename this as well.
We'll call this on rep underscore XP with old XP being passed in and with that we can generate the function
definition for it and we have our rep notify.
Okay, so we have our member variable.
We have it replicated now because it's replicated.
There's an important step that wasn't outlined in the quest and that is to add it to Git lifetime replicated
props.
So this is another boilerplate step when adding a replicated variable, right?
So we can add that now we know it will be replicated.
Now, next, we needed to create a setter and an add to function so we can put those in our class.
We're going to put them up here in the public section and perhaps you might decide while you're implementing
this task that, hey, we could probably implement a getter too.
And I'm just going to do that first.
And if you did that, that's fine too.
You could even force inline it like we did get player level and you can just have 32 get XP.
Which can be const and return exp.
Now we need those add two and set functions.
So let's just declare them both.
We're going to have a void function called add to exp which will take an int 32 called NXP and we'll
have a void function set exp which will also take an int 32 called in exp as well, and we can go ahead
and generate function definitions for these both.
And it seems like these would be quite easy to implement, right?
Add to XP means we take our XP and we add to it plus equals in XP.
Pretty simple.
Set XP would probably be even simpler.
Right?
XP equals in XP.
But we know that these functions also serve another purpose because one of our tasks was to create a
delegate to broadcast when these values change.
So what we should do is create a delegate and broadcast it when these values change.
And this probably will bring something to our attention.
We also have a level, and if we want to show that in the HUD, well, we definitely know that we're
going to need a delegate to broadcast when level changes as well.
So as we're doing this, and this is something I've seen in a professional environment, working as
a software engineer is sometimes I'll be given a task like this, and while I'm working on it, I'll
see a need that's similar for another variable, in this case player level.
And as I'm implementing my add to XP and set XP, I may be seeing the need for that for the player level.
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
set level, things like that depending on the needs.
And that's of course going to depend on your level of experience and familiarity with the needs of the
project.
Now, before we start going rogue, let's make sure we have our own task assigned to us completed before
doing anything else.
And that's going to involve creating a delegate.
Now, what kind of delegate do we need?
Well, we need a delegate that at least can be bound to in C plus plus.
We don't need to bind to this from blueprints as we're going through our widget controllers.
Those are going to broadcast to our widgets in blueprint.
So this doesn't have to be a dynamic delegate.
It can be multicast or just simply a delegate non dynamic, non multicast.
I'm going to choose to make it a multicast delegate.
That can broadcast one param.
That's going to be the stat that has changed and this can be on player stat changed and it's going to
use an n 32 now with multicast delegates we don't need a comma followed by the param name like this.
But what we can do is an inline comment that kind of says what this is and this is going to be stat
value.
So when one of our stats changes will broadcast that to whoever binds to our delegate and we're going
to have one of them for our XP and that will be a public delegate.
So we can just put our delegates right here.
We'll have an on player stat change and this can be on XP changed and we'll call it on XP change delegate.
And it doesn't have to be blueprint assignable.
Now this delegate can be broadcast whenever we set the value or change the value of XP.
So let's go into Aura Player State and make sure we broadcast our delegate.
When XP changes, that's going to be when we add two XP.
So we're going to say on XP change delegate dot broadcast and we're going to just broadcast the value
of XP.
Now this has to happen any time it changes.
So we're going to do that in set XP as well.
Now XP will be actively set only on the server and since it's a replicated variable, then it'll replicate
down to clients and it's on rep XP function will be called at that point.
That's when we need to broadcast our delegate on the client side so that any HUD elements can respond
and update themselves with the correct XP for our XP bar, for example, on clients.
Now we see that this is a need for level as well.
So now that we've finished our current task, we can see the need for an additional set of changes that
could be added to our project for our level.
Now first of all, it would be nice to have a set and an add to function for level.
I think that is important.
We already have a getter, right?
So we need add to and set and for that reason we could have our getters here, our add two functions
here and our setter functions here.
So in addition to add to XP, we can also have add to level.
So void add to level which can take in an int 32 called in level and I'll go ahead and generate the
definition, but I'm also going to declare a set level.
So void set level.
Which takes an int 32 called in level as well.
And I'll generate a function definition for that.
And these will be similar to their counterparts for XP.
Add two level is going to take level plus equals in level and we're going to need a delegate to broadcast
for it to set level just takes level and sets it equal to end level.
So this one is going to need to broadcast a delegate and so is the rep notify.
So let's make that delegate and thanks to our delegate on player stat changed we didn't call it on XP
changed it's just generic for stats so as long as that stat is an int 32 we can use this same delegate
signature and we'll call this on level changed delegate and we'll broadcast this whenever the level
changes.
So add to level gets it, we'll say on level change delegate dot broadcast and we'll broadcast level.
We'll copy this and we'll broadcast level when we set it and when it replicates.
So the rep notify has it too.
Okay.
So we filled a need for level and that's looking pretty nice and we can follow that same trend for new
stats that we add to our player state, such as attribute points and spell points.
If we want to store those on the player state, we have a nice pattern going where we can create a delegate
for it, make a getter and setters or add two functions and create those variables and their rep notifies
and so on.
Excellent.
So this is the next step in our level up system.
Excellent job implementing it and I'll see you in the next video.