Hello and welcome in this lecture we are going to be setting the help from the progression.
So the player here has got a health of what 700 and the enemy has got the health of 40.
But if I went and tweaked our player so that he had a different starting player level in the base that's
not three for example then you should see that when we go ahead and hit play and select that player
in the scene.
His initial health is going to be down at 200 because that is what I had set in the progression.
Let's dive in and see how this can be achieved so the fundamental premise of this lecture is that we
should be able to get our health from in the health class instead of having it from the Health points
configured on a prefab.
We should be able to get this from the progression.
So where are we going to get that from.
Well if we are to our stats namespace it should be the base stats should provide that so I'm going to
create a public terrible public method here called Get Health it's going to return to float and not
take any documents and for now I'm just going to leave it returning zero and we'll fill this out in
a bit.
But basically what we want to do is over in the health CNS we want to introduce a start method in here.
And with this start method if I just fixed this curly brace round here what we want to do is take our
health points not from this configuration but instead we want to get components of type base stats and
we might need to include the stats namespace for that.
Now my parentheses.
And then we can with that base that we should be able to get health which is that method we've just
created.
So base stats will be getting the health it'll be getting it from its progression that it's linked up
to.
But we'll do that in just a second.
First of all I want to apologize for having introduced a bug here because start can get sometimes called
after we have restored the state from the save file which does mean that you could kill an enemy leave
the scene and then come back and the enemy will be back alive again.
The reason being the health has been reset in start overwriting what had been saved in the save file.
Now we are going to fix this but not for a few lectures.
So just live with the problem for now and don't worry we are going to fix it.
Secondly that you'll see an issue here.
We've got a circular dependency because we have got core depending on stats and if you look at stats
somewhere inside stats there will be a dependency on core or if there is not dependency on core at some
point there may well be core is the most fundamental low level module.
And if we put things depending outside of that then they are going to be problems.
So what I'm gonna do is say that health isn't really part of our core and it's part of something a bit
more general something a bit higher level which is our resource module.
So I'm gonna go ahead into the unity Ed and create a new folder under scripts called resources create
folder called resources and inside the resources folder we're just going to move from core the health
script into resources and that's going to do all the right renaming on the stuff that already has a
health component in unity.
Now it says it's been deleted from disk so I need to close it.
Make sure you do that visual you don't save there or you'll get two copies.
So I'm gonna go get open up the hell components new location which has taken the new code with it.
That's good.
Now I want to change the name space from RPG core to RPG resources to match the folder it's now in and
resources might include other things here as well like we might have a minor component or other sorts
of components like that which are resources like maybe stamina or something like that.
Now you see there have become some errors here.
First of all in health we've got this reference to the action scheduler and the action scheduler is
in core.
And because we were in court previously and now we're no longer in court we need to have that using
statement and otherwise this seems to be mostly okay.
So we can go ahead and say that now I want to go and find the other places where we've got errors lots
and lots of errors where we previously were using core and now we might want to be using the resources
namespace.
So I'm gonna go to combat.
Target is the first place I'm going to be doing a using RPG resources and that should make that error
go away.
But we now have an unnecessary using if I click on the core line you can see there's a little light
bulb saying hey remove this unnecessary using which I'm going to do and then I'm gonna go ahead and
fast forward doing the same thing basically just pasting the resources into these classes that need
a link to resources and then removing any unnecessary using.
But before I fast forward I want to show you that fighter C.S. has got another problem here.
We've got this namespace clash.
There is a resources class in the unity engine namespace that we were using to load our weapon and there
was a resources namespace.
Now what you have to do in these cases is just make it unambiguous.
I think it's a good name resource is a good name for our module.
So what we're gonna do is just gonna make it unambiguous by saying Hey we're gonna use Unity engine
resources in this case and then everything is fixed because C sharp knows that where this is coming
from it's not trying to reference the namespace which is what it tries to do by default.
So that fixes the errors in fighter.
And now if we had any extra using around yes we do this system is an extra using I can go ahead and
remove it and I'm going to repeat this process for the other files.
So we start off the process by going to projectile and then weapon up next go into control and a control
room player controller and then in movement we do the movement c s move a C S and that seems to have
fixed all the problems I was having.
So if I go into unity now hopefully we can go and do damage to the guard who's standing over there.
So I'm going to take my big weapon and smack him in the face and he does indeed die.
So the health seems to be decreasing probably because our health initial health is a bit low.
Let's just check this with a higher base health I'm going to cause if you remember we replace that with
the getting the health and the base that is going to set that's 30 because at the moment he had zero
health which is why he died so instantly whack in a couple of times and he dies.
So the health component is still working as it was before.
So as I mentioned the place for this health to come from is not actually the base stats but rather the
progression itself.
So what we're going to have is a few hops.
Basically we start at the health component which gets its health from the base stats which in turn gets
its health from the progression the scripts will object.
So this scriptural object is going to have a public method which is going to return afloat float again
it's going to be get health but this time we need to give it some arguments so that it knows which health
to get because this character we've got many character classes and inside a character class we've got
many levels.
So first of all we need to say hey which character class were you looking for.
First of all so it's the character.
Class is the first argument and then within that.
Which level were you looking for.
That's an int saying the level.
Now this is the bit that you are going to do here so I get to fill this out as a to do I'm just going
to for now put a return value in.
But you are going to have to make this actually get the correct health from the correct character class
at the correct level and in base stats.
What would you do here.
This is a little mini challenge for you How would you return their health coming from progression instead
of this fixed value.
Well the way we can do it is we can take the progression that is assigned in a serialized field and
do a dot get health instead.
And because we have the character class here and we have the starting level we can add those in.
So we are adding a bit of information as we go through this hop through base stats and that is hunky
dory so now I'd like you to fill out that get health method.
So remembering this is what our progression looks like.
For each is good enough at this point.
If you're basically what you want to do is go through each of the classes and find the health basically
and then you want to go and find the level now doing a for each loop over the classes and just comparing
them is plenty fine for now in the future.
We don't want to worry about how the performances on that but not just yet so check which class is the
correct one and then find the right level and return the health at that level.
So Paul's video and have a go okay.
Welcome back.
Let's give this a shot shall we.
So in the progression we are going to do a for each and we're going to do a four each over the progression.
Character class.
So I'm going to call this the progression class in the full loop and then the we're circling over the
character classes array and basically want to say if let's see if our progression character class adopts
character class is equal to the character class that we passed in to get health.
So it's the one we're searching for.
This is the droid that we're looking for then braces we are going to go ahead and well get return the
health level.
So I'm going to return the progression classes dot health and we're going to look up in that array by
the level.
Now remembering here that's the level we're passing in and the array is slightly different at level
1.
We're going to look at left element number 0 in the array.
So he wants to level minus 1 in order to return the right thing here.
And that should hopefully return as a float.
And then at the end of this for each loop we just need some kind of default in case we didn't find the
right class I'm going to go ahead and return 0.
Now why is this complaining at me.
So you may have got this far and got a little bit stumped now.
These errors are basically saying that in get health we've got inconsistent accessibility.
This means that it's saying that the parameter character class is the problem here.
It's saying it's less accessible than the method.
Now the method is public.
Now there are different levels of accessibility in C sharp.
We know that you can have private methods for example and that would be fine.
But actually the e num by default is something called internal and you can see by basically me putting
my function as internal.
It's suddenly okay but we do want it to be public and all that means is that we also need to make our
enum the character class public too.
That needs to be at the same level of accessibility or higher than the methods that use it.
So that makes that error go away.
Now what's going on with the cat class and health.
Ah well that's fairly straightforward.
The problem here is that these two aren't public and I can't access them so I'm going to make them public
and remove serialized field because they're public methods will automatically be serialized so we don't
need to repeat that basically.
So public catch class public health should do the trick.
Now if we go ahead and hit well let's just check what I've got set up in my game.
Core progression and at the moment my player is set to at level 5 have 700 and the grunt.
I haven't got anything set up so what's going to happen.
I'll go ahead and hit play right now.
K we've got a bunch of errors in the console.
These are about the index array out of bounds reason for that is that we've got our grunts set up to
be at level 1.
I believe I have a look at my grunt and his progression script says yeah he's a level one but we don't
have anything in our progression for the grunts at level 1 and at the moment I don't want to fix that
in codes.
Give us a better error.
But I do want to give this some values I'm going to give this a value of 40 so if you go ahead and hit
play now hopefully the errors are gone from the console yes they are.
If I look at my player and have a look at his health his health is currently 14 so quite low.
And if I go and look at the grunt and grunt will do.
And here we've got health points of 40 as well.
So if we start off the player on a slightly higher level let's say he's currently the player is a grunt
level which is odd.
So let's go into the player prefab I thought I'd change this the character progression class should
be player and let's try and set him to level five.
So go ahead and play with that and let's see now.
Who.
What's our level.
If I go into the scene have a look at the player have the player selected take health points seven hundred
you can see that that is now being set by the progression rather than the default values in the prefabs
great.
So in this lesson we've learnt how we can hop through our base stats model behavior use some of the
information there to annotate our method and then go to the progression and find the values of the health
and character class that we want it now as we can see it's getting slightly annoying to have to select
the player all the time to see what health it's got left to in the next lecture we are going to look
at how we can put in a debug display for the health see that.