teachinTech


Global variables in Construct2

Global variables provide a way for games to keep track of and remember a number of different things. In this activity, we will create, alter the value of, and display the contents of global variables.

Turn In: GlobalVars-Testing.capx

Games remember the player names, keep track of the points earned, know how many lives are left, and know how much time is left in the round. One way Construct2 can keep track of those things is by using global variables.

Create a new project named Globalvars-Testing.capx to use in working through this activity.

Adding Global Variables

Click on the Event Sheet. Right click on the Event Sheet. Pick "Add Global Variable". Type in a name for the variable. Select either a number or text. Pick the initial value for the variable. An optional description can be provided.

Add the following six global variables to the GlobalVars-Testing.capx game using the instructions above and the information below.

1. Create varCountDown as a number with the initial value of 90. This is the countdown clock of how many minutes are left to attempt this level of the game.

2. intCountUp is a number with the initial value of 0. This is the running timer of how many seconds the player has been attempting this level.

3. Points as a number with the initial value of 0. This is the number of points that the player has accumulated. This could be total points or could be points for this level (those are different, would need a second variable to keep these separate).

4. Lives as a number with the initial value of 9 (three is more common but this may be a cat game). How many lives the player has in the game.

5. Level as a number with the initial value of 1. The level the player is currently on.

6. Player1Name as text with the initial value of Player1. The name of the first player in the game.

Note: The first two variable names have the prefix var and int. Some developers use a prefix so they can tell the difference between the object named Lives and the global variable named Lives. Construct2 knows the difference and some displays the difference is obvious while others it is not. This is illustrated in the next sections.

Changing Global Variables

There are four main ways to alter global variables ... add to, subtract from, set to, and reset all. Those actions are taken on the Event sheet using System.

On the event sheet, click on "Add Event", pick System, then pick "Every X seconds" using 1.0 seconds and click Done.

We are going to add several actions that happen every 1 second. First, we need to add 1 to the intCountUp variable since it is keeping track of the number of seconds the game has been running. Click on "Add action", pick System, select Add to (in the Global & local variables section) and click on Next. Pick the Variable intCountUp and leave the Value as 1. Click Done. You should see the action System -> Add 1 to intCountUp.

The varCountDown variable is similar only we are going to subtract 1 every 1 seconds. Click on the "Add action" in the "Every 1.0 seconds" event. Select System then "Subtract from" and choose the varCountDown variable with the Value of 1. Click on Done.

In this game, you earn 5 points for pressing the space bar. How will this be done? Try some things before reading on.

We need the Keyboard object so on the Layout 1 tab, we double-click on the layout and pick Keyboard to add that object. Back on the Event sheet 1, we add a new event, choose Keyboard then "On key pressed". Select the space bar and click Done. We then "Add action" and pick System then "Add to". Since we are adding 5 points, we choose the variable Points and use the value of 5. Should our game have an event where the player loses points, you would do the same thing only subtract the points.

To demonstrate the other two actions on global variables, we are going to change the number of lives to 3 when the 3 key is pressed and reset all of the global variables to their original values when the R key is pressed.

Create an event Keyboard->On 3 pressed. Click Add action for that event and pick System then "Set value" and click Next. Choose the variables Lives and the value 3 then click Done.

Create an event Keyboard->On R pressed. For the action pick System then "Reset global variables" and click Done.

Displaying Global Variables - Text with Expressions

Some of the time we don't need to see what is in a global variable, but other times we do need to see their contents.

A common and easy way to see the contents of a global variable is by putting text on the screen then using an expression to display both a phrase and the value of the global variable.

Before creating text objects, we are going to create a layer just for the text objects. Click on the Layers tab of the game project. Rename Layer 0 to Background. Create a new layer named Action. Then create a new layer called TextDisplay. Lock the Background and Action layers. Click on the TextDisplay layer to select it. Click on the Layout 1 to make that the active selection.

Text is an object type similar to Sprite except it is designed to display text. Right click somewhere in Layout 1 and pick "insert new object". Select the Text object (note Text is in the General category and is different than Text box). Type in txtCountDown for the Name of the Text then click on Insert then click where you want to place the Text. Once added you can move the Text around by clicking somewhere in the blue area and dragging it to the desired location. You can also grab the white handles and resize the Text area. Notice that the txtCountDown object says "Text". To change that, click on it to select it then in the Properties (on the left) find the Text property and change it from Text to CountDown:

Note: There are several other properties that you can use to position, size, and change the color/font/alignment of the text.

Double-click somewhere in Layout 1 to bring up the "Insert New Object" window. Select Text and name it txtCountUp then insert it and click to place it where you like. Change the Text property to CountUp:

Add Text objects with the names Points, Lives, Level, and Player1Name. The Text property for those should be ...

   Points: 0
   Lives: 9
   Level: 1
   Player1: Me

Run layout and make sure you can see all of the text that was added. Notice that there are no numbers after the count up and down labels. If we press the space bar, we are not getting our 5 points.

In order to see the contents of the variables, we need to update the Text objects with that information. Click on the Event sheet and find the System event that happens every 1 second. Add action to that event. Pick txtCountDown, click Next, pick "Set text", click Next, and type in the following for the Text (exactly as shown below) then click Done.

   "CountDown: " & varCountDown 

Add action for the "Every 1.0 seconds" event, pick txtCountUp, click Next, pick "Set Text", click Next, and put in the line below then click Done.

   "CountUp: " & intCountUp 

Run layout and verify that you see the count up and count down numbers. If you do not, make sure they are in the visible window and that the Text object is large enough to show both the text and the number.

The two items that you typed in are called expressions. An expression combines several things together. In those two examples, our expression had a string constant which is the item in quotes. The & character says to combine the item on the left with the item on the right. The item on the right is a global variable. Every second we are setting the text on each of those two Text objects to show the current value of the global variable.

You can combine a number of things together in an expression. Add action to the Keyboard->On Space pressed event. Set the Points Text to the following:

   "You have earned " & Points & " points"

Note: Like with the global variables, we named the count up and down Text objects with a prefix this time txt to represent text. We did not do that for the other Text objects so they have the same name as some of the global variables. Observe when typing in your expression there were two Points and the icons let you know if text or variable. Also observe the event sheet and notice how the points action looks. Whether you use prefixes or not is up to you. If it is confusing without them then use prefixes (and be consistent in what you choose).

Run layout and press the space bar a few times to trigger the space pressed event. If you don't see "You have earned 5 points" then make sure it is in the active window and stretched enought to show all of the text. Notice that had three items in our expression, two string constants ... one at the beginning and one at the end. We also had the Points variable in between.

Adding a Little More Action to our Game

While this game is not going to receive awards for its high action content, it is a good learning and testing tool. Below are some additional items to add to your game for additional practice and experience.

1. Create a Text object named GameMessage and set the Text property to ... Enjoy the Game

2. When the L key is pressed, change the level number to one higher.

3. When the countdown is 0 or smaller, change the varCountDown variable to the value 90, update the countdown display, and change the GameMessage to ... "Game over: Time is up."

4. When the countup is 120 or larger, change the intCountUp variable to the value 0 and change the the GameMessage to ... "It has been 2 minutes"

5. Add action to the Keyboard->On 3 pressed event to display the value of Lives.

Wrap Up

Test your game by hitting some of the different keys like the number 3, the space bar, and the R key. Notice the behavior on the game and compare that to the Event sheet to see what does and does not work the way you might think. You may make any adjustments and also may add any functionality that you like before (or after) submitting your game file.


Copyright © 2021 Eric Schumm. Permission granted to freely use this in your classroom.