09. Hud

Introduction

Now that the game is somewhat playable – bullets destroy rocks and the player can lose, we need to add a scoring system and a score display.

Adding Text to the Hud

The Hud Entity that we have created is already part of the GameScreen. We created it when first making the game skeleton. To verify, your project should have an object called HudInstance under the GameScreen.

Therefore we just need to add a Text object to the Hud entity and it should show up. Also, we want to make sure that the game play objects (like rocks) don’t cover the Entity, so we’ll add the Hud Entity to a Layer.

Adding a Text object to the Hud

To add a Text object to the Hud Entity:

  1. Expand the Hud Entity in Glue.
  2. Right-click on the Objects item
  3. Select “Add Object”
  4. Enter the name “TextObject”
  5. Change the TextObject’s SourceType to “FlatRedBallType”
  6. Change the TextObject’s SourceClassType to “Text”

Next we’ll adjust the position of the Text object:

  1. Select the TextObject in the Hud Entity
  2. Change the X to -375
  3. Change the Y to 275

Finally we’ll add a variable which will allow the GameScreen to change the score displayed by the Hud:

  1. Right-click on the Variables item under the Hud Entity
  2. Select “Add Variable”
  3. Select “Tunnel a variable in a contained object”
  4. Select “TextObject” as the Object
  5. Select “DisplayText” as the Variable
  6. Set “Score” as the Alternative Name
  7. Select “int” as the Variable Type

What does Variable Type do? The Variable Type property allows variables to be created which are of a different type than their underlying type. The DisplayText property on the Text object is a string, however the only value we are ever going to set on our Hud is an integer value for the player’s score. Therefore, we can mark the variable as an “int”.

Where to store player score

Now that the Hud can display a score, we need to actually keep track of the score so we can display it. We need to decide where to keep the score. As outlined in this article information such as the player’s score should not be tied to any Screen or Entity. Rather, it should be stored independently and globally as an object contained in GlobalData.

We’ll create two classes to store our data:

  1. PlayerData
  2. GlobalData

To add PlayerData to your game:

  1. Create a “Data” folder in your Visual Studio project
  2. Add a PlayerData class to the Data folderPlayerDataInDataFolder.PNG
  3. Press CTRL+SHIFT+S in Visual Studio to save the project. This will notify Glue that the Project has changed, and that it needs to reload it.
  4. Modify PlayerData so the class looks as follows:

Next add GlobalData to your game:

  1. Add a GlobalData class in your Visual Studio project in the Data folder
  2. Press CTRL+SHIFT+S in Visual Studio to save the project.
  3. Modify GlobalData so the class looks as follows:

Notice that the GlobalData class has an Initialize function. We’ll call this before our game starts up. To do this:

  1. Open the Game1.cs file in Visual Studio
  2. Find the Initialize function
  3. Add the following line *before* FlatRedBall.Screens.ScreenManager.Start is called inside Initialize:

Now we have a well-organized, globally accessible location for our score. For simplicity all players will share the same score, so we’ve only created one PlayerData in GlobalData.

Updating the Score and Hud

Finally we need to update the Score in GlobalData as well as the Hud whenever a player destroys a rock. To keep things simple we’ll award points whenever a rock is shot – whether it has actually been destroyed or whether it has been broken up into smaller pieces.

To do this:

  1. Open GameScreen.cs
  2. Find BulletVsRockCollisionActivity
  3. Add the lines in the “new code” section so your code looks like:

As Visual Studio will indicate, PointsWorth is a variable that has not yet been defined. We will need to add a PointsWorth variable to Rock. To do this:

  1. Find the Rock Entity in Glue
  2. Right-click on the Variables item under Rock
  3. Select “Add Variable”
  4. Select the “Create a new variable” option
  5. Select “int” as the Type
  6. Enter “PointsWorth” as the Name
  7. Set the default value to the newly-created PointsWorth variable to 10

Conclusion

RockBlasterWithScore.png

Now the game includes a Score HUD that updates as the player progresses through the game. The next tutorial will add support for multiple players.

<- 08. Rock States10. Multiple Players ->