Introducing variables

Exclusive offer: get 50% off this eBook here
Construct 2 Game Development by Example

Construct 2 Game Development by Example — Save 50%

Learn how to make games for multiple platforms with Construct 2 book and ebook.

$26.99    $13.50
by John Bura | June 2014 | Games

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. In this article by John Bura, author of Construct 2 Game Development by Example, we will look at what a variable is, why it is needed, and the different types 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

(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.

Summary

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:


Construct 2 Game Development by Example Learn how to make games for multiple platforms with Construct 2 book and ebook.
Published: June 2014
eBook Price: $26.99
Book Price: $44.99
See more
Select your format and quantity:

About the Author :


John Bura

John Bura has been programming games since 1997 and teaching since 2002. He is the owner of the game development studio Mammoth Interactive. This company produces games for Xbox 360, iPhone, iPad, Android, HTML5, ad-games, and others. Mammoth Interactive recently sold a game to Nickelodeon! He has been contracted by many companies to provide game design, audio, programming, level design, and project management. To this day, he has contributed to 40 commercial games. Several of the games he has produced have risen to number one in Apple's App Store. In his spare time, he likes playing ultimate frisbee, cycling, and working out.

Books From Packt


 Learning Windows 8 Game Development
Learning Windows 8 Game Development

 Flash Game Development by Example
Flash Game Development by Example

 Learning ShiVa3D Game Development
Learning ShiVa3D Game Development

Unity 3.x Game Development Essentials
Unity 3.x Game Development Essentials

 Game Development with SlimDX
Game Development with SlimDX

HTML5 Game Development with GameMaker
HTML5 Game Development with GameMaker

XNA 4.0 Game Development by Example: Beginner's Guide – Visual Basic Edition
XNA 4.0 Game Development by Example: Beginner's Guide – Visual Basic Edition

Unity Android Game Development by Example Beginner's Guide
Unity Android Game Development by Example Beginner's Guide


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software