(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.
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.
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 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.
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.
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.
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:
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.
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.
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:
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;
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:
- 2D game development with Monkey [article]
- Microsoft XNA 4.0 Game Development: Receiving Player Input [article]
- Flash Game Development: Making of Astro-PANIC! [article]