WEBVTT

00:06.810 --> 00:07.960
Welcome back.

00:07.980 --> 00:15.270
In this section, we're concerned with implementing the cornerstone of all RPGs experience and leveling

00:15.270 --> 00:15.740
up.

00:15.750 --> 00:22.170
Now, if you're creating an RPG, then you've likely played many of them and you're well aware of how

00:22.170 --> 00:24.540
experience and leveling up works.

00:24.540 --> 00:28.110
But let's just make sure we're on the same page about all this.

00:28.260 --> 00:32.940
In RPG games that have experience, the player has a level.

00:32.970 --> 00:39.930
Usually you begin at level one, but not always, and you start off with an amount of experience also

00:39.930 --> 00:41.370
referred to as XP.

00:42.090 --> 00:49.710
Usually your XP begins empty and you can typically see it on the screen in the HUD in the form of an

00:49.740 --> 00:50.820
XP bar.

00:50.820 --> 00:55.380
Now when you do things in the game that are significant, you're awarded XP.

00:55.410 --> 01:02.460
So you start to see your XP bar steadily increasing until it gets almost filled up, at which point

01:02.460 --> 01:09.400
the next time you gain XP is going to push your bar up to full and perhaps beyond.

01:09.400 --> 01:12.070
At this point it's said that you level up.

01:12.130 --> 01:20.350
This is associated with an award, usually in the form of your level going up and your XP bar draining

01:20.350 --> 01:26.350
back down to empty, at which point you're going to repeat the process, continue doing significant

01:26.350 --> 01:33.430
things in the game, Gaining XP and leveling up yet again in each level up is generally associated with

01:33.430 --> 01:41.650
an increase in power capability and in general the player's capacity to affect the game world in more

01:41.650 --> 01:42.970
dramatic ways.

01:43.030 --> 01:48.520
This is essential to the RPG game loop and keeping players engaged.

01:48.550 --> 01:52.600
Now let's talk about how a system like this works mathematically.

01:52.630 --> 01:59.980
Let's say that we're starting off at level one, which means that we have an XP bar and our experience

01:59.980 --> 02:02.470
points begin empty with zero.

02:02.470 --> 02:10.030
And let's just say that in order to level up to level two, we have to reach 300 experience points.

02:10.030 --> 02:14.800
Now we'll call this the level up requirement at level one.

02:14.800 --> 02:20.650
If you're level one, you have to reach 300 in order to level up to level two.

02:20.680 --> 02:28.450
Now, let's just say at level two, you must reach 900 experience before you can level up to level three.

02:28.570 --> 02:34.930
And let's just use the convention that we'll say the level up requirement at level two is 900.

02:34.960 --> 02:35.650
Be careful.

02:35.650 --> 02:39.970
This is where a lot of people start to get confused with the mathematics here.

02:39.970 --> 02:43.900
I'm saying the level up requirement at level two is 900.

02:44.020 --> 02:47.440
That's the total XP gained so far in the game.

02:47.440 --> 02:56.050
In other words, you had to gain 600 more XP after that initial 300 that you used to get to level two.

02:56.080 --> 03:04.390
Now let's say that the next level up requirement in order to get from level three to level four is 2700.

03:04.420 --> 03:07.630
Now again, that's only 1300 more.

03:07.660 --> 03:15.880
You reached level three, you had 900 total and then you got 1800 more, bringing that total up to 2700.

03:15.910 --> 03:20.890
So we're treating XP here as a cumulative total throughout the game.

03:20.890 --> 03:28.180
Now, if you wanted to, you could say that your level two requirement is 600 and your level three requirement

03:28.180 --> 03:29.590
is 1800.

03:29.590 --> 03:35.380
But the reason I like to do it the other way, keeping a cumulative total is because this allows us

03:35.380 --> 03:38.530
to handle situations when leveling up.

03:38.530 --> 03:43.240
That would be difficult to calculate and we'll talk about those in just a moment.

03:43.270 --> 03:49.980
Now, if you're a very mathematically inclined person, you're probably noticing a pattern here 300,

03:49.990 --> 03:52.090
900, 2700.

03:52.180 --> 03:54.130
That's a mathematical formula.

03:54.130 --> 03:57.250
And if you're sharp, you probably have figured it out.

03:57.250 --> 03:58.720
It's this formula.

03:58.840 --> 04:04.570
The requirement for any given level is 100 times three raised to the power of the level.

04:04.570 --> 04:11.020
At level one, your level up requirement is 100 times three raised to the first power or just 100 times

04:11.020 --> 04:11.730
three.

04:11.740 --> 04:15.970
At level two, your level up requirement is 100 times three squared.

04:16.000 --> 04:17.260
Three squared is nine.

04:17.260 --> 04:25.360
Nine times 100 is 900 at level three, 100 times three raised to the third, power gives 2700 and so

04:25.360 --> 04:25.860
on.

04:25.870 --> 04:32.260
So you may be tempted to say, well, we could just use a mathematical formula to dictate how much XP

04:32.290 --> 04:37.240
is required at any given level, and there's nothing wrong with doing it that way.

04:37.240 --> 04:42.730
That is actually a valid way in some games do use mathematical formulas.

04:42.760 --> 04:49.900
The problem with a mathematical formula is you're forced to stick to that formula unless perhaps you

04:49.900 --> 04:51.400
have multiple formulas.

04:51.400 --> 04:57.250
You could use this formula up to level three and then a different formula from level four on and so

04:57.250 --> 04:57.640
on.

04:57.640 --> 05:00.070
But that makes things a bit more complicated.

05:00.070 --> 05:05.740
And some games just choose these values by hand without a mathematical.

05:05.770 --> 05:06.400
Formula.

05:06.580 --> 05:11.110
Let's say that your level four requirement is 6500.

05:11.140 --> 05:18.220
In other words, you'd have to gain a total of 3800 more from your 2700 in order to get from level four

05:18.220 --> 05:19.420
to level five.

05:19.450 --> 05:22.660
Now, this doesn't adhere to this mathematical formula.

05:22.690 --> 05:28.150
If we stuck to the formula, it would be 100 times three raised to the fourth power.

05:28.540 --> 05:33.520
That would be a level requirement of 8100, not 6500.

05:33.550 --> 05:37.270
So I'm just pointing out that there are a couple ways to do this.

05:37.300 --> 05:43.690
You can use a mathematical formula and make your life easier, or you can do things in a different way

05:43.690 --> 05:46.180
that gives you more fine tuned control.

05:46.240 --> 05:50.110
And that's totally going to depend on the needs of your game.

05:50.910 --> 05:52.530
Now for this game project.

05:52.530 --> 05:56.580
I'm not going to use a mathematical formula for level up requirements.

05:56.610 --> 05:57.030
Why?

05:57.060 --> 05:59.940
Because I'd like them to be set by hand.

05:59.940 --> 06:07.200
And in addition to that, I'd like other values associated with our level up information, not just

06:07.200 --> 06:12.270
the XP requirement, but things like rewards for leveling up.

06:12.570 --> 06:19.980
For that reason, I like to use a data asset to keep this data driven and we can make one called level

06:19.980 --> 06:20.910
up info.

06:21.180 --> 06:29.370
Now this data asset is going to contain information for each level, and the most obvious piece of information

06:29.400 --> 06:34.380
per level is going to be that level up requirement and this could simply be an integer.

06:34.560 --> 06:42.000
So we can have perhaps an array of some kind of data structure that contains a level up requirement

06:42.000 --> 06:46.920
and we can have any other additional information in there, such as an attribute point reward.

06:46.920 --> 06:49.090
This could also be an INT 32.

06:49.110 --> 06:55.390
So each time we level up, we can look at the attribute point reward for that level and see how many

06:55.390 --> 07:01.140
attribute points we get rewarded and then we can spend those on our attributes to buff them up.

07:01.150 --> 07:07.210
Now I'd also like other types of rewards, such as a spell point reward so we could have a spell point

07:07.210 --> 07:07.900
count.

07:07.900 --> 07:15.100
And as we level up and get more spell points, we can spend those on either upgrading our spells or

07:15.100 --> 07:17.860
unlocking spells that we haven't yet acquired.

07:18.010 --> 07:25.090
This is a very common way to handle unlocking new content, so our level up info can also contain that

07:25.090 --> 07:26.620
spell point reward.

07:26.650 --> 07:31.080
So it's a data asset that's kind of simple, kind of basic.

07:31.090 --> 07:37.090
It's an array of structs that contain integers, but it's a nice way to sort things out and it also

07:37.090 --> 07:43.210
allows us to handle complications associated with the mathematical formula method.

07:43.420 --> 07:45.700
And we'll talk about that in a second.

07:45.730 --> 07:52.330
Now, the data asset should also have an easy to use function for looking up what level we should be

07:52.330 --> 07:55.240
at for any given XP amount.

07:55.270 --> 07:57.740
Now, how would a function like this work?

07:57.760 --> 07:59.440
Well, let's talk about that.

07:59.770 --> 08:04.140
Let's say that for each level we have level requirements.

08:04.150 --> 08:09.610
In other words, if we know how much XP we have, we know what level we should be.

08:09.640 --> 08:16.150
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

08:16.150 --> 08:16.960
leveled up yet.

08:16.990 --> 08:19.600
We're still level one right now.

08:19.600 --> 08:24.250
Let's say we just happened to defeat a higher level enemy.

08:24.280 --> 08:31.130
Perhaps we're just that good and that enemy gives us a lot of XP, say a thousand points.

08:31.150 --> 08:33.300
What happens in this case?

08:33.370 --> 08:41.560
Now we only needed 120 more XP points to level up to level two, but a thousand is far more than that.

08:41.590 --> 08:45.240
A thousand puts our total at 1180.

08:45.250 --> 08:47.430
That means we're right here.

08:47.440 --> 08:50.920
We have enough experience to be level three now.

08:50.920 --> 08:54.370
In other words, we've just achieved a double level up.

08:54.870 --> 08:57.640
Now, this is not as uncommon as you might think.

08:57.660 --> 09:05.070
This happens all the time in games, and that's the whole fun of RPGs is you start to get more powerful

09:05.070 --> 09:12.450
and more capable of defeating higher level foes that reward you with more experience points.

09:12.540 --> 09:18.810
So it would be a serious shortcoming if you did not account for this in your code.

09:18.840 --> 09:21.270
So we'll make sure that we account for it.

09:21.300 --> 09:27.360
Now, I mentioned complications with using mathematical formulas as opposed to data assets.

09:27.450 --> 09:35.130
A formula could be used to calculate Exp requirements, but this makes the mathematics a little bit

09:35.130 --> 09:41.410
cumbersome to handle things like double level ups or perhaps triple or quadruple level ups.

09:41.430 --> 09:44.610
You see, you'd have to jump through mathematical hoops.

09:44.640 --> 09:49.680
You'd have to take the player's current level, which would be one, and plug that into the formula

09:49.680 --> 09:54.210
to see how much more experience they need to level up to level two.

09:54.240 --> 09:59.890
Then you'd have to plug two into the formula to see how much would be required to level up to level

09:59.890 --> 10:07.330
three and check the current amount of experience being received and see if it's enough to do a double

10:07.330 --> 10:08.140
level up.

10:08.170 --> 10:13.180
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

10:13.180 --> 10:13.870
of hand.

10:13.900 --> 10:19.990
Whereas if we have a data asset, we can simply write a neat little algorithm that will check for double

10:19.990 --> 10:21.250
and triple level ups.

10:21.250 --> 10:24.430
So we'll be talking about ways to handle this situation.

10:24.430 --> 10:29.320
So we have a number of steps moving forward to implement Exp and leveling.

10:29.410 --> 10:36.970
First, we need to create a level up info data asset where we can specify level up requirements for

10:36.970 --> 10:43.900
each level as well as any rewards that can be gained as a result of leveling up, such as attribute

10:43.900 --> 10:45.370
points and spell points.

10:45.880 --> 10:47.680
Now we need XP.

10:47.950 --> 10:53.260
And while XP could be an attribute, that's one way to implement it.

10:53.290 --> 10:59.320
I personally think it makes more sense for this not to be an attribute, but rather a value on the player

10:59.320 --> 11:00.040
state.

11:00.160 --> 11:07.300
Attributes are values that affect the game in various ways, such as affecting combat, and they're

11:07.300 --> 11:13.100
great for when you need interoperability between various elements of gas.

11:13.120 --> 11:20.710
But in the case of XP, all it really is is a mark of progress and something we check to see whether

11:20.710 --> 11:22.720
or not it's time to level up.

11:22.750 --> 11:30.130
We don't really use XP in our combat calculations, for example, so I personally think it's better

11:30.130 --> 11:31.780
not being an attribute.

11:31.810 --> 11:36.910
However, you may disagree and feel like it should be an attribute and that's fine.

11:36.910 --> 11:41.020
And this course project though, we're going to put it on the player state.

11:41.050 --> 11:43.690
Now we're going to need it to have a rep notify.

11:43.720 --> 11:49.580
So that way as we set it on the server and it should only be set on the server, we can replicate it

11:49.580 --> 11:53.840
to clients and that way its changes can be reflected in the HUD.

11:55.080 --> 12:01.050
Now we're going to need a delegate to broadcast when the XP changes, and that way our widget controller

12:01.050 --> 12:07.740
can respond to that delegate broadcast and update any widgets such as our XP bar and the HUD.

12:08.760 --> 12:11.460
Now we also need an XRP reward.

12:11.490 --> 12:15.870
The amount of XRP gained each time we kill an enemy.

12:15.870 --> 12:20.880
And this can be based on the enemy's level and its class or anything else we want.

12:20.910 --> 12:25.410
But we need to know how much XRP any given enemy is going to give us.

12:26.220 --> 12:30.210
And then we actually need to award that XP when killing an enemy.

12:30.240 --> 12:38.670
Now, this is going to be closely related to our systems in gas as when things happen, such as applying

12:38.670 --> 12:41.360
gameplay effects and changing attributes like health.

12:41.370 --> 12:45.670
That's when we can determine whether or not damage was fatal.

12:45.690 --> 12:52.680
So even though XP is not going to be an attribute, that's not to say that we won't have some kind of

12:52.680 --> 12:59.280
attribute that we can update in order to make those XP calculations something like a meta attribute

12:59.280 --> 13:02.400
as that's what our incoming damage attribute is.

13:02.460 --> 13:04.120
So we'll get to that.

13:04.140 --> 13:09.930
And finally, we have to actually handle leveling up and potentially handle multiple level ups.

13:09.930 --> 13:17.370
As we mentioned before, we may defeat an enemy that gives us enough XP to double or even triple level

13:17.370 --> 13:17.730
up.

13:17.730 --> 13:20.460
We need to handle that potential situation.

13:20.460 --> 13:27.700
And along with leveling up, we need to reward our player with any rewards that we decide we should

13:27.700 --> 13:30.880
get, such as attribute points and spell points.

13:31.000 --> 13:33.670
So leveling up is quite a system.

13:33.670 --> 13:38.860
It's not terribly complex, but there are a lot of parts to it that we have to keep in mind.

13:38.950 --> 13:44.860
But once we get this system in place, it's going to really bring our game project to that next level

13:44.860 --> 13:52.780
as we have that sense of progression and as the player gains progress, they can then become more powerful

13:52.780 --> 13:55.630
and move on to higher level enemies.

13:55.630 --> 13:56.590
Dungeons.

13:56.590 --> 13:57.580
ET cetera.

13:58.300 --> 14:01.240
So we'll implement this system in the videos to come.

14:01.390 --> 14:02.620
I'll see you soon.
