Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Right now our enemy is defeated with a single bullet.
However, we want to implement a health system to give it a certain amount of health to tank a couple
of shots.
We are going to implement this in a very modular way using an actor component.
This would allow us to use the health system on different actors, even if they aren't in the same inheritance
chain, and it actually also makes it easy to create a plugin.
For example, you could export this actor component as a plugin and actually reuse your health system
across all of your different games.
That way you don't have to remake it from scratch every time, but you could use that time to expand
the system and make it perfect over time.
I will attach a video to this lecture that talks more about blueprint reusability.
You can check out if you're more interested in that topic.
For now, let's just close all of these blueprints on the right.
Just close tabs to the right, and we want to go to blueprints characters and do this in the action
chart because this needs to be shared between the player and the enemies.
So first I want to show you the way.
We're not going to do it in this video, but this is the way that most people do the system.
And we're going to talk about like, why this isn't the ideal way.
This is just for demonstration, so you don't have to follow along.
And the way that people usually make a health system is they go into the variables here on the character
and they just make health.
And set this to float.
They make something like Maxhealth as well and have something like is defeated, for example, and make
this a boolean.
And then when taking damage they will do something like health and then they minus uh subtract and set
the new health to this.
Right.
And this is all fine.
This works.
But as you keep on doing this, you're just going to add more and more variables to the player.
And you're also going to add more and more functions and code in the event graph to the player.
And it gets very hard to understand what belongs where.
And later on you might have a stamina system.
So you have stamina, max stamina, you might have mana, and the entire blueprint is going to become
bloated.
So on top of what I said before of actor components being easily reusable through different projects,
even just for organization purposes, they are great because we can put everything that belongs to health
into a single component, and everything is going to be a lot cleaner.
So now I'm just going to delete all of this again, right?
We don't need this anymore.
Don't need this anymore.
And we're going to create something very similar but in a component which we can then attach to this
blueprint.
So in the content drawer we're just going to go to blueprints.
Right click make a new folder called components.
Open it up.
Right click here and go to blueprint class.
And here under common we have an actor component right.
And here it already says an actor component is a reusable component that can be added to any actor.
And a health system is a pretty perfect example for this.
Just click here to create a new actor component.
And I like to call this BPC for blueprint component.
And we're just going to call it vitality because this isn't just going to be health, it is also going
to be tracking our is defeated state.
So I think vitality is just a better overarching concept than just calling it health.
And we actually have already been using components before.
Right.
So if you go to the action share, you can see the character movement component, the animation component.
Right.
And these have all been created by an engineer at Unreal Engine or in the case of the animation component,
by the creator of the paper XSD plugin as a component.
Just like we have been doing it right now and our BPC vitality, we can just go to add here and just
type BPC vitality and we can just add it here as well, just like the animation component, like the
character movement component, if it doesn't show up for you, maybe you have to compile and save first.
And this is how we can create a component and how we can add it to an actor.
But now we still need to implement the actual functionality in here in the BPC vitality.
So let's open it up.
And again we first want to start with our variables here, just like we did before in the demonstration
I'm going to start with.
I'm going to start with Max health and set it to a float.
And next I want to have the current health.
This is already a float, so this is fine.
And then I also want to have the is defeated state.
And this needs to be a boolean because this can either be true or false.
Now we want to compile and save, so we are able to set the default values here on the right.
So is defeated should be false by default.
So this is fine.
But when it comes to the current health and max health we actually create a problem.
So let's say we want to set the max health to ten.
Of course, when we start the game, we also want the current health to be ten.
So we could also just go here and type in ten.
But now a problem is that if we update the max health, let's say we update this to 15 and we forget
to update the current health.
Well, our max health is going to be 15, but when we start the game we only have ten health.
So this is a very common issue that can easily happen.
So what is better to do is to just set a value for the max health.
We want to use ten and the current health.
It doesn't matter what we set here, we can just set to zero.
And we actually just set the current health right.
Set current health on Beginplay.
We just get the max health here.
Like this.
And set the current health when the game starts.
This way, we can always make sure that we only have to change the max health.
There is not an issue that maybe we forget to type it in.
We just set it when the game starts.
Now just to demonstrate how this works.
Well, compile save and we want to go back to our action char base.
And now the edges on Beginplay.
We can just get the BPC vitality.
And here we can just get max health.
And we can just print this out.
Like this.
Just so you can see, this is how we access anything on this component, right?
I start the game and you can see we have ten max health on the player and also ten max health on the
crab.
What I want to do now is to keep ten health on the player and also as the default, but just set the
crab to three health.
So in the content drawer I can go to characters player no enemies and I have the BP enemy crab.
And in here open the full blueprint.
And again here we have the BPC vitality.
And we can actually have access to this default value here Max health Current health is defeated.
And also on the action chart base right.
We have access to these and we can change the values here, the default values as well.
And on the crab BPC vitality I just want to set this to three.
And current health we don't worry about is defeated.
We don't worry about.
But this way we just changed the health on the crab.
Right, on the top left.
On top left.
You can see now ten health on the player, three health on the crab.
And this is how we can make a component.
But just set the default values differently depending on which blueprint we are using this on.
Now that we have the base for the health system setup, we can open up the projectile.
So we go to blueprints, projectiles, and the base Bpy projectile base.
We want to, instead of calling destroy actor on the action char base, we want to apply damage so we
can just drag off here and apply damage.
This is a function that comes with Unreal Engine.
We didn't create this ourselves.
This is something that already is prepared for us.
And the great thing is it is network ready.
So this would even work for online games right out of the box.
Now just get rid of the destroy actor.
Put this up here through branch only.
Connect the rest.
And here we can set the base damage.
How much damage do we want to apply?
The instigator in this case would be the projectile.
Or we could just pass through the instigator of the projectile, which would be the player or the lizard
later on.
And this you only really need if you have plans to use it.
I don't think we're going to use the instigator so we can just leave it empty.
The same was with the damage causer.
Again, this is very similar with the projectile.
We can just pass these things through and then receive them on the other end damage type class.
Also something we don't have to worry about.
You can have like burning damage, different damage types and then use something there.
Again, the only thing we care about here is the base damage.
And again we could just type in a number here right.
Like one damage for the default projectile, which is correct.
But we want to turn this into a variable.
So promote the variable and just call it projectile damage.
And we can then easily just change the default value here later for the charged projectile for the enemy
projectiles.
So compound safe.
But yes the default value should be one.
So now if we shoot the enemy, still nothing happens.
But it doesn't get destroyed anymore.
This is the first step.
We actually need to use another function on the receiving end to get everything we set in the apply
damage, and the place where we want to implement this is the BP action char base.
And we can just find an empty slot here.
We can also get rid of this print string.
We don't need this anymore.
We know this works now.
And here, an empty slot.
We just right click and type in any damage.
And again this is something that Unreal Engine prepares for us gives to us.
And this is paired with the apply damage.
So the apply damage is going to execute.
And on wherever we call this the damaged actor, it is going to execute this any damage event and everything
we pass in here like the base damage event instigator, all of these things are going to come out on
the other side, and we can then decide what we want to do with these.
So first we just want to print the damage to show you that this actually works.
Let's print it out.
And on the grab you can see one because it receives the message that we want to apply one damage here.
Now we need to actually apply this to the BPC vitality though.
And again, this part, you don't have to follow along.
This is just a demonstration of how not to do it.
We could get the BPC vitality, we can get current health, and we can set current health.
Write like this.
And then we can, from the current health, we can, uh, subtract the damage and just set the new current
health.
And this works.
This works just fine.
But again we made this nice component.
So a function like this, we want to put it inside of the component and make it easily reusable.
So even though this works this is not a good way of going about it.
Instead I just want to delete this.
And inside of BPC vitality, I want to create a function and just call it receive damage.
And in here I want to get the current health.
And set the current health.
And by the way, if you drag out here, if you hold control and let go, you get the getter.
And if you hold alt and let go, you get the setter.
And this is just a nice shortcut.
But anyway, yes, we want to receive the damage and we can set the current health, but we need to
have an input here in this function.
Right.
So for the inputs we can click here to create a new one.
Make it a float and just call it damage right.
This is the incoming damage on this function.
And we want to have the current health minus.
The incoming damage and we have to cross lines here because if we do it the other way, it's not going
to work and just connect this to the current health.
And just to show this, we can print string here again.
And just print the remaining current health.
And in the action chart we can now compile and save.
And here on the BPC vitality, we now just call the receive damage.
We don't have to do anything weird outside of here.
We just have this one nice function and just connect the damage here.
And in our case, again, we don't care about the type instigator and all these kind of things.
And now let's just compile and save and try this out.
You can see if I hit them once we have two remaining health one remaining health zero remaining health.
And we can actually go minus.
So this is something we also want to take care of.
So again in the BPC vitality before we set the current health.
We actually want to use a clamp.
So we can clamp here.
Clamp.
Float.
Go like this.
And this will set a limit.
In both directions, so the minimum could be zero.
We cannot go below zero and the maximum should be our max health.
If we get healed or something like this well this is a damage function, so it's probably not going
to happen.
But if we want to heal somebody for example two we can use a clamp to make sure we cannot over heal.
And let's just try this again now, right?
I can shoot multiple times.
It never goes below zero.
So this fixes the minus damage problem.
The next thing you want to do is that if we take damage after this, we want to check that if we are
defeated or not.
So we can then get current health, get current health, and check for smaller equals or less equals
than zero.
And here we have a branch.
Connect this here.
And we are gonna.
In that case, set is defeated to true.
Just like this.
So we know that every time we receive damage, we also update the is defeated state in case the health
is lower equal to zero.
So we can then use this information here in the receive damage.
Here we can just get our BPC vitality and we check for is defeated, get is defeated.
And in this case we have another branch here.
Like this.
And if the character is defeated now, then we want to call destroy actor.
Right.
So first we apply the damage to the vitality component and then we check for the is defeated.
If we are defeated, we destroy the actor.
And this is the way all of these health systems work, right?
Like this I should once, twice, three times.
The health is zero and the character gets deleted.
And in this case, you might think that.
Why do we even need to have this is defeated state if we're going to destroy the actor anyway?
Well, especially for the player we actually later on don't just want to destroy the player right away
because we want to show the defeated animation.
And there should also be a delay between respawning.
And this is why we want to have the defeated state.
The next thing I want to take care of is making the damage feedback.
Good, because now we shoot the enemy, but you don't really see the enemy react to being shot.
And this doesn't feel good.
This doesn't feel satisfying to the player.
And an easy way to do this is to make the sprite flash.
So that's what we're going to do in the next lesson.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.