Dev Log#13 Adding Health, Damage, Death and Respawns
- camjhill737
- May 2, 2024
- 3 min read
Updated: May 3, 2024
Adding health to a character is universally straightforward you apply a couple publics float call health and max health and congrats you now have health in your game.
What was interesting was learning how to apply damage in this multiplayer game. On the my projectile bullet class which is derived from my bullet class I have an "OnHit" function that calls the gameplay statics function apply damage the rest of the OnHit functionality is dealt with on the parent class "Projectile".
In the character class I'm binding a callback function to the unreal delegate "on take any damage". This callback is where I'm subtracting the damage from the projectile bullet from my health and I'm clamping this calculation between 0 and my character's max health to prevent the players health dropping into the negative numbers.
Damage is public variable that is on the projectile bullet class it can edited from blueprints and this will be the same for all the types of bullets that I will create.
How Elimination is handled:
The game mode which is on the server is responsible for handling the destruction for the player and the respawning of a new one. The visualisation of the players death and respawn is handled in a multicast remote procedural call function found on the character class.
I have a function called elimination which includes logic for the player dropping their weapon, calling the multicast elimination function that handles all the VFX for eliminations, and calling the function set timer. This timer activates the function "EliminatedTimerFinished" which calls the respawn function on the game mode which destroys the player and calls the function restart player at player start.
Creating the visual effects was really fun as I learned how to create a dissolve material and just seeing the visualisation of the code is what makes programming fun. I used another free pack off the marketplace and migrated this little robot particle system. In the multicast elimination I am dynamically changing the material instance on the character to my dissolve material. The dissolve effect works through calling a curve blueprint that I made and interpolating that with a timeline variable so that the effect smoothly transitions the character from visible to not visible. I'm also spawning in the robot particle system and a sound cue simultaneously. I was having difficulty destroying the mesh of the robot particle system but I fixed this issue by setting a lifetime property to the particle effect in its blueprint, and calling the virtual destroyed function for the robot's component in C++.
Note: To spawn particle effects you need a particle system component to call the function spawn emitter at location.
The next steps were to visualise the player losing health, making sure it reset to 100 on respawn, tracking kills and deaths for the player and a message to say you were eliminated by the player who killed you. These are all matters for the HUD. I first created the UI by creating an overlay widget and adding that to the HUD. The player controller class is responsible for the visualisation of this overlay widget so it updates the UI after the player takes damage or dies. This on it own works for the server but it doesn't work for the clients. To fix this problem I created a player state class that contains the replication notify functions for changing the score and death apart which is on the health notify function is on the character class. The player state also handles the score and death addition as well as setting the killers name.
I'm making use of the replicated variable score and making my own replicated variables an int32 deaths variable and a FString variable that the players name will be passed into.
A problem I encounter with these steps for the HUD is that on the client machines the death message that says you were eliminated was really delayed as it didn't appear until the player was nearly back on their feet instead of appearing on death and it never disappeared after any amount of time. It also cause my character's health to not reset back to 100 when they respawned. I fixed the health problem by setting the health in the controller's virtual override function OnPossess. The death message problem was a doozy but I finally fixed it by calling the hide message function in begin play on the character which solves the problem of the message not disappearing when the player respawns. I also increased the net update frequency on the player state which fixes the delayed response on clients.
UI needs polish but will get round that eventually. Lots to do still.
Comments