Chapter 3. Variables and Arrays

Variables and arrays are essential to computer programming and game design. Without them, games would not work and would not be fun to play.

In this chapter, we will cover the following topics:

  • What are variables?
  • Why are variables so important?
  • Different kinds of variables in Construct 2
  • Learning about arrays

Introducing variables

Computer programming is based on mathematical principles. After all, the first computer was made to actually calculate equations, and it was only later that applications (as we know them) were developed. You have probably heard of variables in science and math classes. In computers, these variables are necessary to make applications and they are very important in games. Even a small indie game might have hundreds of variables.

Variables are places where you can store small amounts of data. This data can be a name, a number, a date, a game object, or it can even store true or false information. Variables are essential to games because they can store items such as the following:

  • Score
  • Player name
  • Game objects
  • Mouse position
  • Keyboard input

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. We will cover using these variables in Construct 2 later in the chapter.

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.175.253