Introducing variables

5 min read

(For more resources related to this topic, see here.)

In order to store data, you have to store data in the right kind of variables. We can think of variables as boxes, and what you put in these boxes depends on what type of box it is.

In most native programming languages, you have to declare a variable and its type.

Number variables

Let’s go over some of the major types of variables. The first type is number variables. These variables store numbers and not letters. That means, if you tried to put a name in, let’s say “John Bura”, then the app simply won’t work.

Integer variables

There are numerous different types of number variables. Integer variables, called Int variables, can be positive or negative whole numbers—you cannot have a decimal at all. So, you could put -1 as an integer variable but not 1.2.

Real variables

Real variables can be positive or negative, and they can be decimal numbers. A real variable can be 1.0, -40.4, or 100.1, for instance.

There are other kinds of number variables as well. They are used in more specific situations. For the most part, integer and real variables are the ones you need to know—make sure you don’t get them mixed up. If you were to run an app with this kind of mismatch, chances are it won’t work.

String variables

There is another kind of variable that is really important. This type of variable is called a string variable. String variables are variables that comprise letters or words. This means that if you want to record a character’s name, then you will have to use a string variable. In most programming languages, string variables have to be in quotes, for example, “John Bura”. The quote marks tell the computer that the characters within are actually strings that the computer can use.

When you put a number 1 into a string, is it a real number 1 or is it just a fake number? It’s a fake number because strings are not numbers—they are strings. Even though the string shows the number 1, it isn’t actually the number 1. Strings are meant to display characters, and numbers are meant to do math. Strings are not meant to do math—they just hold characters. If you tried to do math with a string, it wouldn’t work (except in JavaScript, which we will talk about shortly).

Strings shouldn’t be used for calculations—they are meant to hold and display characters. If we have a string “1”, it will be recorded as a character rather than an integer that can be used for calculations.

Boolean variables

The last main type of variable that we need to talk about is Boolean variables. Boolean variables are either true or false, and they are very important when it comes to games. They are used where there can only be two options. The following are some examples of Boolean variables:

  • isAlive

  • isShooting

  • isInAppPurchaseCompleted

  • isConnectedToInternet

Most of these variables start off with the word is. This is usually done to signify that the variable that we are using is a Boolean. When you make games, you tend to use a lot of Boolean variables because there are so many states that game objects can be in. Often, these states have only two options, and the best thing to do is use a Boolean.

Sometimes, you need to use an integer instead of a Boolean. Usually, 0 equals false and 1 equals true.

Other variables

When it comes to game production, there are a lot of specific variables that differ from environment to environment. Sometimes, there are GameObject variables, and there can also be a whole bunch of more specific variables.

Declaring variables

If you want to store any kind of data in variables, you have to declare them first. In the backend of Construct 2, there are a lot of variables that are already declared for you. This means that Construct 2 takes out the work of declaring variables. The variables that are taken care of for you include the following:

  • Keyboard

  • Mouse position

  • Mouse angle

  • Type of web browser

Writing variables in code

When we use Construct 2, a lot of the backend busywork has already been done for us. So, how do we declare variables in code? Usually, variables are declared at the top of the coding document, as shown in the following code:

Int score; Real timescale = 1.2; Bool isDead; Bool isShooting = false; String name = "John Bura";

Let’s take a look at all of them. The type of variable is listed first. In this case, we have the Int, Real, Bool (Boolean), and String variables. Next, we have the name of the variable. If you look carefully, you can see that certain variables have an = (equals sign) and some do not. When we have a variable with an equals sign, we initialize it. This means that we set the information in the variable right away. Sometimes, you need to do this and at other times, you do not. For example, a score does not need to be initialized because we are going to change the score as the game progresses.

As you already know, you can initialize a Boolean variable to either true or false—these are the only two states a Boolean variable can be in. You will also notice that there are quotes around the string variable.

Let’s take a look at some examples that won’t work:

Int score = -1.2; Bool isDead = "false"; String name = John Bura;

There is something wrong with all these examples. First of all, the Int variable cannot be a decimal. Second, the Bool variable has quotes around it. Lastly, the String variable has no quotes. In most environments, this will cause the program to not work. However, in HTML5 or JavaScript, the variable is changed to fit the situation.


In this article, we learned about the different types of variables and even looked at a few correct and incorrect variable declarations. If you are making a game, get used to making and setting lots of variables. The best part is that Construct 2 makes handling variables really easy.

Resources for Article:

Further resources on this subject:


Please enter your comment!
Please enter your name here