Game Development Reference
In-Depth Information
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 cod-
ing 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 vari-
able. 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.
Search WWH ::




Custom Search