Adding Player Stats

 

stats

In Summary Screen I would like to show player stats like:

  • Accuracy,
  • Best combo,
  • Enemies killed,

And I would like to be able to add more stats in the future. This tutorial will be really simple.

This Tutorial has been created using Unreal Engine 4.10
Make sure you are working on the same version of the engine.

Managing Stats

If you are doing multiplayer game and you want to share the stats with other players you should keep them in PlayerState. Shooter Tutorial isn’t multiplayer game so I will keep the data in Actor Component attached to GameState. Why in GameState and not in GameplayCharacter? Because combo level and score were implemented there. They both (stats, combo and score) should be at PlayerState if you want to go with Unreal Architecture. That was my mistake and during refactoring I will move them to PlayerState.

Creating Stat Component

Let’s start by creating component which have all stats and will manage them.

Create new blueprint extending from Actor Component named StatComponent and add these variables:

Var Name Var Type Description
BulletsMiss int Storing information about how many bullets player miss enemies.
BulletsHit int The same but for bullets that hit enemies.
BestCombo int
EnemiesKilled int

And now let’s add some functions.

GetAccuracy:

GetAccuracy

UpdateAccuracy:

UpdateAccuracy

UpdateBestCombo:

UpdateBestCombo

GetBestCombo:

GetBestCombo

GetEnemiesKilled:

GetEnemiesKilled

IncreaseEnemiesKilled:

IncreaseEnemiesKilled

And that’s all here. Why I’m doing stats in separate component? As I would be easier to add more stats later. Don’t try to put everything in GameState/Character/etc – use UE4 features to make your blueprints more object oriented and component based.

Now go to your GameplayGameState and just add StatComponent in Components View.

Updating Accuracy

In BP_BaseEnemy we had created couple of months ago there is a function named CalculatePoints. It’s checking if there was enemy hit or not. We will use that function to update stat component.

CalculatePointsUpdate

I have marked added nodes.

Updating Enemies Killed

Go to BP_BaseEnemy and open Die function. Just increase enemies killed in our stat component at the end of this function.

DieUpdate

Updating Best Combo

Open GameplayGameState and find IncreaseComboBar function. Find place where CurrentComboLevel int is increased.

IncreaseComboUpdate

Easy as that!

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.