Chapter 6. Keeping Score

In many games, having stats and scores are a way of showing the players how far along they've come. For some games, stats decide whether players win or lose the game or rounds they play. There are some games where stats create competition such as a high score table in a racing game, or a ranking system in a first-person shooter game. Stats can be used in many ways. They can influence a player to do things they normally wouldn't do, just to get that stat.

In this chapter, we will:

  • Create stats for the player
  • Implement those stats in our scripts
  • Create a stat tracker for the stats
  • Create an achievement system
  • Use PlayerPrefs to save our stats
  • Use GUI methods to show the stats and achievements
  • Create/assign the stats

Before we implement our stats, we need to figure out what stats we want to keep track of. This is a rudimentary yet an important step.

Prototype stats

Now let's figure out which stats we want to keep track of! In this book, the game we create will have a gladiator arena-styled gameplay. So we will have rounds where the player will fight enemies. To win a round, the player will need to kill all of the enemies; to lose a round, the enemies will have to kill the player.

Here's a list of stats that we want to track:

  • Kills
  • Deaths
  • Total gold
  • Current gold
  • Gold spent
  • Level
  • Rounds won
  • Rounds lost
  • Kill-death ratio
  • Win-lose ratio
  • Time played

Assigning the stats to the player

Now that we know what stats we want to track in our game, let's start our script. Create a new C# script and name it StatTracker. Next, let's add our variables to it; these will be the stats that we track:

int pKills = 0;
int pDeaths = 0;
int pTotalGold = 0;
int pCurrentGold = 0;
int pGoldSpent = 0;
int pLevel = 1;
int pRoundsWon = 0;
int pRoundsLost = 0;
float pKDR = 0.00f;
float pWLR = 0.00f;
float pTimePlayed = 0.00f;

As you can see, the variable names are preceded by the letter p, which, in this instance, will mean that these variables are for the player. Most of our stats are being tracked as int variables; the last few are float. These are the variables that we will modify, save, and reset in our script.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.217.220