11. Scoring HUD Logic

Introduction

Now that we have a visible HUD, we need to add logic to have it update when players score. This tutorial will add code to the ScoreHud to update the displayed text according to public properties which will be set by GameScreen whenever a goal is scored.

Adding Team Score Properties

First we’ll be adding properties to the Hud object for the scores of each of the players. We could do this purely in code, but Glue provides a convenient way to set the score values to integers.

We will be combining two Glue features for this:

  • Tunneling variables – Creating a variable in an entity which is tied to the variable of a contained object. In this case we’ll be creating variables which are tied to each of the score Texts’ DisplayText variable.
  • Built-in casting of the variable type – although DisplayText is a string, we’ll tell Glue that we intend to use it as an integer.

To do this:

  1. Expand ScoreHud’s Objects in Glue
  2. Drag+drop the Team1Score onto the Variables item
  3. Select DisplayText as the variable
  4. Set the Alternative Name to Score1
  5. Set the Variable Type to int
  6. Click OK

TunnelConvertedScoreBeefball.gif

Repeat the steps above, but this time use Team2Score, and create a variable called “Score2”.

Updating the HUD

Now that the ScoreHud can react to score changes, the ReactToNewScore can be modified to update the HUD. To do this, modify the ReactToNewScore method in GameScreen as follows:

If we run the game now we’ll notice that scores start out at 0 and increments whenever a player scores a goal.

Troubleshooting

If you are seeing a conversion error similar to  Cannot implicitly convert type 'int' to 'string' , then you may need to modify the variables for Score1 and Score2. See the steps above where the Variable Type is set to int.

If you’ve already created the variables, you can set the variable conversion:

  1. Select the Score1 variable
  2. Select the Properties tab
  3. Change OverridingPropertyType to int

Conclusion

Well, that was easy! Now we have scoring working. At this point we could call the game done. The next tutorial will be adding some extra finishing touches to the controls to make it more competitive and make the game play deeper.

<- Scoring HUDAdding Dashing ->