Welcome to your next quest.
Now we know that we need a data asset for our level up info.
Now your quest is to create one and add an array of structs to your level up info so that we can have
the amount of information we need for each level up.
That includes level up requirement, attribute point, reward, and spell point reward.
Now, as a bonus, you can make a function to find the level for a given amount of exp.
So you pass in the amount of exp and the function will find what level you should be at for that amount
of exp.
Remember exp is cumulative right over the course of the whole game.
So if you're up to the extra challenge, figure out how to find that level for a given amount of exp.
So pause the video and conquer this quest now.
Okay, so we need a data asset.
That means we're going to go into C plus plus classes or a public and I'll put this in ability system
data with our other data assets here.
So new C plus plus class, we're going to create a data asset and click next.
And this can be called level up info.
So we'll create the class, we'll close the editor and we'll define it here.
So let's go to public ability system data.
We'll open level up info and close all other tabs.
I'll go ahead and get both CP and H files open and here in the header file, the first thing we need
to do is create a struct.
Right?
We need a struct with our level up info for each level.
So we'll go ahead and create that struct here.
Called for a level up info.
Now this will be a use struct with blueprint type.
It gets a generated body and we're going to have basically three values here.
We're going to have a level up requirement and for level up requirements, this is a number an amount
of exp required in order to level up.
Now for Exp, I'd like to use discrete numbers integers rather than floats.
So it's kind of cut and dry.
Whether or not you have enough exp to level up.
You have a number of points and if you don't have enough to level up then you don't level up.
So for that reason, these are going to be integer numbers.
We're going to have an INT 32 and this will be a level up requirement and we'll give it a default value
of zero.
But this will be designed to be set from our data asset for each level.
So it's going to be edit defaults only and I don't see an immediate need to expose it to the event graph.
Now, in addition to the level up requirement, I'd like to know how many attribute points I should
be rewarded for leveling up to this level.
So I'm going to make an INT 32 called Attribute Point Award, and by default it'll be one.
And this will make it easier if most of the time we just want to gain one attribute point when leveling
up, then for most of these we won't have to change it.
And then every once in a while perhaps we can get to attribute points for leveling up and so on.
And we haven't created any spell points, but we're going to so when we level up, we'll be able to
spend points on spells as well.
So I'm going to have another int 32 called spell point reward or award.
Either one is fine award.
Sounds like you're going to be awarded this many attribute points, but we could call it spell point
reward.
And then it's like, this is your reward.
I don't think it's terribly important which one we use, so I'm going to call it spell point award for
the amount we award someone when they level up.
So now that we have this aura level up info, we need an array of these.
So we're going to make a public section and make a T array of F or a level up infos called level up
information.
Now this will be filled in in the blueprint.
So edit defaults only it gets and we have our data asset.
Now for a bonus, we have the task of creating a function to find the level for a given amount of experience.
So what we can do is make an int 32 returning function called find level for exp where we take an int
32 called exp and return what level we should be at for that amount of experience points.
So how are we going to do this?
Well, we have level up information which is a t array and the index for our t array is going to be
the level, right?
So level up information index dot one will contain the struct for level one.
Now because we're indexing by level and not by, say, the level up info in a map.
For example, we can't just do a random access lookup, we're going to have to go through the array
until we find the correct level.
So a simple algorithm would be to create some kind of a loop, like a while loop and while can search
through the array until it finds something.
So I can make a boolean called Be searching and set it equal to true and execute the while loop while
be searching is true.
So while be searching.
And then at some point we can set it to false as soon as we find the correct level up requirement.
Now we need to know what level to return.
So I'm going to create a local int 32 called level and we'll start it off at level one and in our loop,
once we're done with our loop, this should be set to the correct level and we'll return that at the
end.
So we'll return level at the end.
So the question is what does our loop do?
Well, the first thing we should do is check our level up information and make sure it has enough elements
in it.
In other words, we should say if level up information dot num, like how many elements are in the array?
If it's less than or equal to level, then we should return Y because well, let's say level up, information
is empty.
It has zero elements in it.
While level will be one and level up information num will be zero.
So in this case we should just return and we'll return level.
We'll just say one in that case.
Now level up information should at least have one element in it.
And if it does have exactly one, well then we should also return level because that's the only level
that we have stored in there.
Now, if it has two, for example, then level up information dot num will be two.
And that's really just for elements zero and one.
And the convention that I'd like to use is that first element in level up information will correspond
to no level.
It'll be just a placeholder.
So that element one can be for level one.
So in the case that level up information dot num is two and level is one.
Well in that case we want to return one, right?
So really we could have level up information num minus one here.
Now let's think about this.
If level up information has three elements, if level up, information has three elements and level
is one, then level up information dot num minus one is two.
That is not less than or equal to one.
So we would continue.
So what we can say here in a comment is level up information at one equals level one information, right?
And level up information at two equals level two information.
So this means that level up information at zero is meaningless.
It's just a placeholder.
Okay, So if we reach past this return, then that means we're going to need to search and see what
level we should return.
And that's going to depend on XP.
So what we can do is make a check.
We can check if XP is greater than or equal to level up information at the level.
Now level up information at this level returns a struct.
So if we use the dot operator, then we can access that level up requirement.
That's going to tell us how much is required to level up at this particular level.
So we start off with level equals one.
We're assuming we have enough elements in level up information to search it.
Now let's say that XP is say between 0 and 300.
In other words, level should be one, right?
Well, level starts at one here locally and XP.
Let's just say it's 100.
That means we should be level one, right?
Well, if we look up the array indexing at level one and we get that level up requirement, that should
be 300 for level one, which means that XP is not greater than or equal to level up requirement.
So in that case, we can set be searching to false and exit the while loop and return level so we can
have an else case here that says be searching equals false.
Now what do we do if we make it to the inside of the If statement?
In other words, XP is greater than the level up requirement of our current level.
Let's say the amount of XP is 400.
That's greater than the level one requirement, right?
So what we can do is add one to level, we can say plus plus level, and then our loop will start over.
So if level up information has enough elements in it, level has just gone up to two, right?
That means level up information should at least have three elements in it.
If it does, three minus one would be two.
That would be less than or equal to level, which would be two now.
So we'd return level.
See how that works.
But if level up information has way more than three elements, let's say it has, say, ten, well then
we won't get past this.
So what we'll do is we'll check our condition again.
XP was we said 400 and we can check now against level up information at level two.
Now let's say at level two, the level up requirement is 900.
Well, in that case, we would not reach the inside of this if statement because our XP was 400.
So we would get to the else be searching would be false.
We would exit early and return level.
So this gives us a function to search through the array and return the correct level.
And yeah, it's a while loop.
So the maximum number of iterations would be the length of our array.
And this also allows us to see what level we should be for any XP amount.
So if we're at level one and we gain 1000 experience points, if that's enough for a double level up,
we'll know because we'll know what we were at before.
We'll know what we should be at.
Now.
If that's two levels or three levels up, then we know we got a double or a triple level up and so on.
So now that we have level up info, all we need to do is make the blueprint.
So let's compile and launch the editor and make our data asset blueprint.
Okay, so let's make our blueprint based on level up info.
I can go into blueprints ability system data, right click, go to miscellaneous and data asset and
we can search for level up info.
And create a data asset based on that.
We'll call it a level up info.
Let's open that up.
And we have an empty array.
Now if we click plus we know that the zeroth element is just a placeholder, so we're not going to set
anything here.
We want to click plus again and we'll have our level up requirement for level one.
So again, we could have used a curve table for this.
I'm not I'm going to use this way because I like to set things manually just in case.
We want different values and I don't want to have to create curve tables for multiple things.
All I have to do is add to this struct.
If I want something else to put in this data asset per level.
Now for level one, I want my level up requirement to be 300.
We'll likely create some kind of a placeholder level up info for testing so that we can level up much
faster but 300 XP to reach level two sounds good to me and I'm going to click plus a whole bunch more
times so that I can have level up requirements for levels one through ten.
Now, how do you choose these values?
Well, one way is to take a look at other games.
Another way is to kind of ballpark estimate.
Another is to just fill this in as you go, as you're playtesting.
Now, there are lots of other popular role playing games.
Some are video games, some are tabletop RPGs.
And there's one pretty good one you could use as a reference.
It rhymes with Dungeons and Dragons, and I'm going to use numbers that are similar to that.
So my level up requirement for level two is going to be 900.
For level three, it's going to be 2700.
For level four, it's going to be 6400.
For level five, it's going to be 14,000 514,500.
And at level five, I'd like to gain two attribute points and to spell points.
So now we can see that we don't have to just get one point every time we level up, we can change this.
So level five is a special level up.
We get two points right so we can do something special like that.
For level six, I'm going to have a level up requirement of 20,000, and for seven it's going to be
35,000.
That is for level eight.
I'm going to go ahead and go up to 50,000 for level nine.
It's going to be 65,000.
And for level ten, it's going to be 85,000.
So now you can see that a mathematical formula might be hard to manage as the number of levels increases.
But manually setting these makes it a little easier.
And this is kind of cool because you're seeing that as the game progresses, as your character gets
higher and level.
We're dealing with higher numbers here.
At first you needed a measly 300 experience to level up to level two, but when you're level nine,
you need 65,000.
But then again, you'll be slaying monsters that give you a higher number here.
So this is one of the ways that RPGs can make you feel a sense of accomplishment.
You're capable of gaining much higher values for your XP, which is quite nice.
Okay, so now that we have level up info that takes care of that step.
And we'll continue in the next video with the next task.
I'll see you soon.