Go to your Character Class now and Create a New Function. Call it DamagePlayer.
This is going to take our Armor and Health variables, subtract a value from them and then set the variables to the new value.
First, get the Armor variable and drag out; search for a Float > Float node.
This node is going to be our initial check.
What are we checking for? Whether the game should subtract from Armor or from Health.
So what we’re going to be saying is this: If Armor > 0, subtract from Armor; If Armor == 0, start subtracting from Health.
From the > node, pull out the bool pin and create a Branch node.
Get the Armor variable, drag out, find Float - Float. Set the second Float value to 0.25.
Then drag out a Set Armor node and attach it to the Float - Float node AND also attach it to the True Execution of the Branch node.
What this does is simple: When the function runs, if Armor is not 0, it’s going to deplete armor by 0.25.
The first time this runs it will be 1.0 - 0.25, then the next time it will be 0.75 - 0.25, etc.
Go back to the Branch node and next to the False Execution, get the Health variable.
Repeat the above steps, this time with Health and ensure that Set Health is connected to the False Execution.
Let’s just review this quickly:
When the DamagePlayer Function runs, it’s going to encounter a Branch.
The Branch will check if the player has Armor (if Armor is above 0), and fire.
If Armor IS above 0, the Armor will take damage.
If the Armor IS NOT above 0 (i.e. if Armor = 0), the Health will be depleted.
Now there’s one more thing to do here…
Go back to Set Health and create another Branch.
Something we need to check for is whether or not Health = 0. If it does, we want to display the YouDied widget.
Get Health and create a Float = Float node. Set the second value to 0.
Attach that to the Condition of the Branch node and on the True Execution, add a Create Widget node, choose YouDied, and then from the Return Value create a Add to Viewport node.
When the value of Health reaches 0, the Player will see the text “You Died.”
Close that and create a new Blueprint Actor. Call it DamageVolume.
We want a way to test our code, so we’ll setup an Actor that will cause Damage to the player in some way.
In the Components tab, add a Box Collision. SCALE the Box Collision to be pretty large, then also add a Cube Mesh.
Put the Box mesh in the center (this isn’t needed, but it’s just helpful so that when we put it in the level, we know where to step).
In the actor’s Graph click on the Box Collision on the left hand side under Components, and create a new On Component Begin Overlap node.
Cast to your Character class and from As [Your Character] drag out and search for DamagePlayer.
This is a simple check system.
What’s going to happen is that when the player enters the Box Collision, DamagePlayer is going to run. At first it will subtract 0.25 from the Armor until Armor = 0, then it will just depleting Health
Now this check is admittedly a bit tedious. Because of how the Overlap node is setup, you’re going to have to step into the Box Collision, get the Armor damaged by 0.25, then step out and step back in again. If you do that 4 times, Armor will equal 0 and the 5th time you step in, it will start to deplete Health.
(You can make this loop so that as long as you’re in the Collision it will deplete the Armor every second but since we’re not looking at Timer loops right now, I won’t go into all that).
Place your DamageVolume actor into your level and then Press Play.
Walk into it once and Armor will be 0.75
Step out, walk in again and Armor will be 0.50
Step out and walk in again and Armor will be 0.25
Step out and walk in again and Armor will be 0.0
Step out and walk in again…and Health will be 0.75…
That’s all there is to it. That’s the very basic way to create a distinction between Armor and Health.
I hope this helped. If you have any questions, leave a comment here or PM me if you need.