12. Health Part 2


The last tutorial created a functional HealthBar Entity and added instances of it to the Hud Entity dynamically based on the number of connected controllers. This tutorial will cover implementing health in the MainShip Entity and displaying health in the HealthBar.

Health Variable

First we’ll define a Health variable that will keep track of how much health each MainShip has left. Since each MainShip will have its own independent health (as opposed to all players sharing one health value) we will add the Health variable to the MainShip.

At this point you might expect that we will add the Health variable as a Glue variable in the MainShip Entity. However, the HealthVariable will be defined in the MainShip.cs code file rather than in Glue. Let’s take a moment and discuss why this variable should not be a Glue variable.

When to create Glue variables

So far we’ve created a large number of variables for Entities in RockBlaster. Variables belong in Glue if they are:

  • Variables which are used as coefficients in code which will never change throughout the life of the project – such as the MovementSpeed on the BulletEntity
  • Variables which are tunnel to variables on FlatRedBall types – such as the Score variable in the Hud Entity tunneling in to the DisplayText property on a Text object
  • Variables which need to be modified in Glue States or Events – such as the Texture of the Sprite on the Rock Entity. Notice that this also falls under the category of being a variable tunneling to a FlatRedBall type variable.
  • Variables which define the starting state of another variable. The “starting state variable” should not change; however, the variable that it sets may change after it is set.

Let’s look at each condition and see how it relates to the Health variable:

  • The Health variable is not a coefficient for logic. It will not be used to control the behavior of any game objects. Furthermore, the Health variable will change as the MainShip takes damage from rocks.
  • The Health variable is a new variable which is not directly tied to any FlatRedBall type variable.
  • We will not modify the Health variable through any states or events – it will only be modified when we detect collision.
  • The Health value will need a starting value, however it will change throughout the life of the project. However, we will need a starting value for the Health variable.

Creating StartingHealth

As indicated above, we will need a variable to define the starting value of the Health variable. To create this variable:

  1. Expand the MainShip Entity
  2. Right-click on the Variables item
  3. Select “Add Variable”
  4. Set the type to “int”
  5. Enter the name StartingHealth
  6. Click OK
  7. Set the value of StartingHealth to 6

Defining and setting Health

Now we can define the Health variable in the MainShip.cs file. To do this open MainShip.cs in Visual Studio and add the following code to the MainShip class:

Next we’ll need to set the Health value to the StartingHealth value. To do this, add the following code in CustomInitialize in the MainShip.cs file:

Modifying Health

Now we have a Health value which is functional – in other words it destroys the MainShip when it reaches 0. Next we’ll modify the Health value when the MainShip collides with a Rock. To do this, open GameScreen.cs and find the MainShipVsRockCollisionActivity function. Locate the following line in the deepest if-statement:

Remove this line and replace it with:

We’ll also want to remove the following line:

Replace it with:

You can also remove the comment about “Eventually we’ll want to do something like…”

If you now play the game you’ll notice that when the MainShip collides with a rock, the rock breaks apart. The player also takes damage and if 6 rocks are collided with the MainShip is destroyed. Keep in mind that additional rocks are created whenever a larger rock is destroyed, so the player may actually collide with multiple rocks and appear to be destroyed before 6 collisions have occurred.

Updating the HealthBar

Now that we have a Health value we need to update the HealthBar according to the current Health value. We have two choices here – we could “push” the values to the HealthBar whenever changes occur or the HealthBar could “pull” values from the appropriate MainShip. Although it’s slightly inefficient, continually pulling values (also referred to as “reactivity” in FlatRedBall) can greatly simplify coding on larger projects and can help eliminate bugs. We’ll use a reactive approach for updating the HealthBar.

For the HealthBar to update itself it will need a reference to the MainShip it is referencing. First, open HealthBar.cs and add the following property:

Next, add the following code to CustomActivity in HealthBar.cs:

Next, add the following code to CreateHealthBarInstances in Hud.cs inside the first for-loop after the healthBar instance is created:

Now the HealthBar (or HealthBars if multiple players are connected) will update appropriately when the player takes damage.



Our game is becoming more and more playable with each tutorial; however, there is a severe bug present in our game – an accumulation bug occurring due to Entities being dynamically created and not destroyed. If you play the game and survive long enough you will see the frame rate slowly drop. Fortunately this problem is easy to solve.

The next tutorial will show how to check for an accumulation of Entities and how to remove them.

<- 11. Health13. Destroying Entities ->