Using Variables in Your Game

Adding Variables

Your game at this point has functioned without including variables, however when you want to start including mechanics that involve values changing over time, such as a score system or a health bar, you’re going to quickly realize that you need to begin using variables.

Now that you understand collision detection and have created a basic physics engine for your game, the next step is to connect that to a player health bar. This opens up several more advanced options within your game, such as introducing enemies that are able to inflict a variable amount of damage on the player and items that provide health boosts. These are just two examples of several that are possible once variable usage is understood.

Activity: Create a Health Variable (Local)

Up to this point, the characters haven’t had any repercussions for their interactions. Now that you’ve created collision detection, it’s time to connect it to the life of the players.

Steps:

  1. The first step is to determine the name and max value of your player’s health. This is something to consider based on the intended difficulty of your game. Tweaking the max health and the amount of damage inflicted by enemies is one way to balance difficulty level.
  2. Once you’ve determined that, click on the player sprite and select the Data category and create a new variable.
  3. Enter the name for this, such as playerHealth and define it to have a local scope.
  4. Notice that there is now a basic graphic representation of the variable on the project preview. You can move this by dragging it.
  5. If you want to hide the variable, go ahead and uncheck the box to the left of the variable.
  6. You have now created your first variable!

Activity: Integrating the Variable Into the Game

The health variable is going to be linked to the collision detection that you’ve already created in your project. This is going to be demonstrated through the usage of a local scope playerHealth variable for your hero sprite. Keep this in mind while working through the steps- notice that the collisions will all be coded from the hero sprite’s “perspective” since other sprites won’t be able to change the variable’s value due to the scope.

Steps:

This example will demonstrate how to include a variable within the collision between the player sprite and the health boosting object.

  1. Once the variable is created, you’ll see the default visual representation of it in the top left corner of your project. Since it’s local, it should say something like: “spriteName: playerHealth
  2. Remember, that since you’ve created a local variable the variable can only be modified by scripts contained within that sprite. Since this is the case, all of the collision detection will occur within this sprite rather than inside the health booster sprite.
  3. Take a look at this example:
    IntGames - Variables Project - Boost 1 The conditional statement contains some more advanced operators to narrow the condition, but the inclusion of the variable is the same as you should use.
  4. Find the “change [variableName] by [ ]” block in the Data category. Make sure to select the name of the variable that represents the player health.
  5. Set the value to be “1” This value can be anything that you want- in this example, 1 is the base number for health boosts and losses, but this is something that you can determine.

Activity: Create a Score Variable (Global)

Follow the same steps for creating the local variable, except this time choose “For all sprites” so that the scope is defined as global.

Steps:

  1. Create a variable and name it something such as “Score” or “Game Score.”
  2. Remember to define the scope as global!
  3. There are a few different options for implementing this:
    • Create a timer function that causes the score to increase as time passes. Hint: You’ll need to create some sort of loop and timer script!
    • Create a score system that increases whenever the player character interacts with a certain object. Think of gathering coins in Super Mario. If you’re doing this, you can combine it with the automated motion mechanic to create randomly appearing objects.

Related Posts