Welcome back.
In this section, we're concerned with implementing the cornerstone of all RPGs experience and leveling
up.
Now, if you're creating an RPG, then you've likely played many of them and you're well aware of how
experience and leveling up works.
But let's just make sure we're on the same page about all this.
In RPG games that have experience, the player has a level.
Usually you begin at level one, but not always, and you start off with an amount of experience also
referred to as XP.
Usually your XP begins empty and you can typically see it on the screen in the HUD in the form of an
XP bar.
Now when you do things in the game that are significant, you're awarded XP.
So you start to see your XP bar steadily increasing until it gets almost filled up, at which point
the next time you gain XP is going to push your bar up to full and perhaps beyond.
At this point it's said that you level up.
This is associated with an award, usually in the form of your level going up and your XP bar draining
back down to empty, at which point you're going to repeat the process, continue doing significant
things in the game, Gaining XP and leveling up yet again in each level up is generally associated with
an increase in power capability and in general the player's capacity to affect the game world in more
dramatic ways.
This is essential to the RPG game loop and keeping players engaged.
Now let's talk about how a system like this works mathematically.
Let's say that we're starting off at level one, which means that we have an XP bar and our experience
points begin empty with zero.
And let's just say that in order to level up to level two, we have to reach 300 experience points.
Now we'll call this the level up requirement at level one.
If you're level one, you have to reach 300 in order to level up to level two.
Now, let's just say at level two, you must reach 900 experience before you can level up to level three.
And let's just use the convention that we'll say the level up requirement at level two is 900.
Be careful.
This is where a lot of people start to get confused with the mathematics here.
I'm saying the level up requirement at level two is 900.
That's the total XP gained so far in the game.
In other words, you had to gain 600 more XP after that initial 300 that you used to get to level two.
Now let's say that the next level up requirement in order to get from level three to level four is 2700.
Now again, that's only 1300 more.
You reached level three, you had 900 total and then you got 1800 more, bringing that total up to 2700.
So we're treating XP here as a cumulative total throughout the game.
Now, if you wanted to, you could say that your level two requirement is 600 and your level three requirement
is 1800.
But the reason I like to do it the other way, keeping a cumulative total is because this allows us
to handle situations when leveling up.
That would be difficult to calculate and we'll talk about those in just a moment.
Now, if you're a very mathematically inclined person, you're probably noticing a pattern here 300,
900, 2700.
That's a mathematical formula.
And if you're sharp, you probably have figured it out.
It's this formula.
The requirement for any given level is 100 times three raised to the power of the level.
At level one, your level up requirement is 100 times three raised to the first power or just 100 times
three.
At level two, your level up requirement is 100 times three squared.
Three squared is nine.
Nine times 100 is 900 at level three, 100 times three raised to the third, power gives 2700 and so
on.
So you may be tempted to say, well, we could just use a mathematical formula to dictate how much XP
is required at any given level, and there's nothing wrong with doing it that way.
That is actually a valid way in some games do use mathematical formulas.
The problem with a mathematical formula is you're forced to stick to that formula unless perhaps you
have multiple formulas.
You could use this formula up to level three and then a different formula from level four on and so
on.
But that makes things a bit more complicated.
And some games just choose these values by hand without a mathematical.
Formula.
Let's say that your level four requirement is 6500.
In other words, you'd have to gain a total of 3800 more from your 2700 in order to get from level four
to level five.
Now, this doesn't adhere to this mathematical formula.
If we stuck to the formula, it would be 100 times three raised to the fourth power.
That would be a level requirement of 8100, not 6500.
So I'm just pointing out that there are a couple ways to do this.
You can use a mathematical formula and make your life easier, or you can do things in a different way
that gives you more fine tuned control.
And that's totally going to depend on the needs of your game.
Now for this game project.
I'm not going to use a mathematical formula for level up requirements.
Why?
Because I'd like them to be set by hand.
And in addition to that, I'd like other values associated with our level up information, not just
the XP requirement, but things like rewards for leveling up.
For that reason, I like to use a data asset to keep this data driven and we can make one called level
up info.
Now this data asset is going to contain information for each level, and the most obvious piece of information
per level is going to be that level up requirement and this could simply be an integer.
So we can have perhaps an array of some kind of data structure that contains a level up requirement
and we can have any other additional information in there, such as an attribute point reward.
This could also be an INT 32.
So each time we level up, we can look at the attribute point reward for that level and see how many
attribute points we get rewarded and then we can spend those on our attributes to buff them up.
Now I'd also like other types of rewards, such as a spell point reward so we could have a spell point
count.
And as we level up and get more spell points, we can spend those on either upgrading our spells or
unlocking spells that we haven't yet acquired.
This is a very common way to handle unlocking new content, so our level up info can also contain that
spell point reward.
So it's a data asset that's kind of simple, kind of basic.
It's an array of structs that contain integers, but it's a nice way to sort things out and it also
allows us to handle complications associated with the mathematical formula method.
And we'll talk about that in a second.
Now, the data asset should also have an easy to use function for looking up what level we should be
at for any given XP amount.
Now, how would a function like this work?
Well, let's talk about that.
Let's say that for each level we have level requirements.
In other words, if we know how much XP we have, we know what level we should be.
Now let's say that we're playing our game and we've reached an XP total of 180 so far, so we haven't
leveled up yet.
We're still level one right now.
Let's say we just happened to defeat a higher level enemy.
Perhaps we're just that good and that enemy gives us a lot of XP, say a thousand points.
What happens in this case?
Now we only needed 120 more XP points to level up to level two, but a thousand is far more than that.
A thousand puts our total at 1180.
That means we're right here.
We have enough experience to be level three now.
In other words, we've just achieved a double level up.
Now, this is not as uncommon as you might think.
This happens all the time in games, and that's the whole fun of RPGs is you start to get more powerful
and more capable of defeating higher level foes that reward you with more experience points.
So it would be a serious shortcoming if you did not account for this in your code.
So we'll make sure that we account for it.
Now, I mentioned complications with using mathematical formulas as opposed to data assets.
A formula could be used to calculate Exp requirements, but this makes the mathematics a little bit
cumbersome to handle things like double level ups or perhaps triple or quadruple level ups.
You see, you'd have to jump through mathematical hoops.
You'd have to take the player's current level, which would be one, and plug that into the formula
to see how much more experience they need to level up to level two.
Then you'd have to plug two into the formula to see how much would be required to level up to level
three and check the current amount of experience being received and see if it's enough to do a double
level up.
You'd also have to check to see if this was a triple level up and so on and it gets a little bit out
of hand.
Whereas if we have a data asset, we can simply write a neat little algorithm that will check for double
and triple level ups.
So we'll be talking about ways to handle this situation.
So we have a number of steps moving forward to implement Exp and leveling.
First, we need to create a level up info data asset where we can specify level up requirements for
each level as well as any rewards that can be gained as a result of leveling up, such as attribute
points and spell points.
Now we need XP.
And while XP could be an attribute, that's one way to implement it.
I personally think it makes more sense for this not to be an attribute, but rather a value on the player
state.
Attributes are values that affect the game in various ways, such as affecting combat, and they're
great for when you need interoperability between various elements of gas.
But in the case of XP, all it really is is a mark of progress and something we check to see whether
or not it's time to level up.
We don't really use XP in our combat calculations, for example, so I personally think it's better
not being an attribute.
However, you may disagree and feel like it should be an attribute and that's fine.
And this course project though, we're going to put it on the player state.
Now we're going to need it to have a rep notify.
So that way as we set it on the server and it should only be set on the server, we can replicate it
to clients and that way its changes can be reflected in the HUD.
Now we're going to need a delegate to broadcast when the XP changes, and that way our widget controller
can respond to that delegate broadcast and update any widgets such as our XP bar and the HUD.
Now we also need an XRP reward.
The amount of XRP gained each time we kill an enemy.
And this can be based on the enemy's level and its class or anything else we want.
But we need to know how much XRP any given enemy is going to give us.
And then we actually need to award that XP when killing an enemy.
Now, this is going to be closely related to our systems in gas as when things happen, such as applying
gameplay effects and changing attributes like health.
That's when we can determine whether or not damage was fatal.
So even though XP is not going to be an attribute, that's not to say that we won't have some kind of
attribute that we can update in order to make those XP calculations something like a meta attribute
as that's what our incoming damage attribute is.
So we'll get to that.
And finally, we have to actually handle leveling up and potentially handle multiple level ups.
As we mentioned before, we may defeat an enemy that gives us enough XP to double or even triple level
up.
We need to handle that potential situation.
And along with leveling up, we need to reward our player with any rewards that we decide we should
get, such as attribute points and spell points.
So leveling up is quite a system.
It's not terribly complex, but there are a lot of parts to it that we have to keep in mind.
But once we get this system in place, it's going to really bring our game project to that next level
as we have that sense of progression and as the player gains progress, they can then become more powerful
and move on to higher level enemies.
Dungeons.
ET cetera.
So we'll implement this system in the videos to come.
I'll see you soon.