Variables in Construct 2

Like most things in Construct 2, variables are really simple. Even though adding variables is fairly straightforward in other languages, Construct 2 makes it easy.

Adding a variable

Adding variables in games is really important. Without variables, you cannot store any data. This will make the game almost nonfunctional, and it limits you to small games.

To add a variable, go to your event sheet and right-click on it. This should bring up a cascading menu. Clicking on the Add event icon will let you add a variable:

Adding a variable

There are two types of variables in Construct 2: global and local variables. Variables can be seen in different places. Global variables will be seen throughout the entire game. Local variables you want only to be seen by a game object or a function.

Creating a global variable

Right-click on the event sheet and you can add a global variable. Go ahead and click on Add global variable.

Creating a global variable

As you can see in the preceding screenshot, there are four fields. You can name your variable something memorable and that makes sense. Naming your variable appropriately will make the development process much smoother. Variable names such as score, numberOfShots, and powerUpLevel are recommended. You cannot have spaces in your variables—the computer will not allow you to have spaces.

Tip

One thing that I strongly advise is that you do not capitalize the first word; instead, capitalize the other words in the variable. This will make it easier for you or anybody else to read it.

As you can also see here, you have two option types—either Number or Text. This can either be Real or String. The Initial value field is where you can initialize variables. Construct 2 automatically initializes this field for you; however, if you want to have a number or a name, you can put it here. Remember, if you make a text variable, you cannot do calculations with it; you can only calculate with numeric variables.

The Description box is optional, but make sure that you always input something in there; it will save you a lot of trouble down the road. All this does is put a little reminder about what the variable does.

Tip

When your game has more than 200 variables, you are going to wish you had put the descriptions in. In addition, if you come back to your game at a later time, you might forget what these variables do. Reminders are great and I highly recommend using them.

Creating a score variable

Let's make a Score variable that keeps track of the score. Again, variables store data. We need to set up a Score variable to store the score data. Whenever you need to store a small amount of information, you need to make a variable. The addition of a new global variable should look like the following screenshot:

Creating a score variable

Once you have something that looks like this, you can go ahead and click on OK.

Creating a score variable

In the preceding screenshot, you can see what the Score variable looks like in Construct 2. You can see that it is at the top of the event sheet—and it will stay at the top of the event sheet, just like the variables in other programming languages.

Adding a variable to a sprite

Let's add a local variable. Add a sprite to the game and select it. You will see that, in the Properties window on the left-hand side, there is something called Instance variables. These are local variables, which means that this game object is the only game object that uses these variables. Usually, these instance variables are variables such as health, shields, and other stats; each game object's variables will be different.

Adding a variable to a sprite

Go ahead and click on Instance variables. You should come up with a dialog box like the one shown in following screenshot:

Adding a variable to a sprite

Click on the plus sign (+) to add a variable. The New instance variable window will appear, as shown in the following screenshot:

Adding a variable to a sprite

Here, you can see that the dialog box is similar to the one for global variables. The only difference is that you can now add a Boolean variable as well. For the most part, all of these options are the same. I again highly recommend that you put a description whenever possible.

Creating a health variable

Let's go and create a health variable and give it an Initial value of 100 in the previous window. We can name variables whatever we want. We could even name it "banana" if we really wanted to. Instead, we give the variables names that can help us easily understand the data that the variable holds. By setting up the health variable, we now have a container for the health number. This way, we can add events that add to or subtract from the health value. We can even set up the game logic to move to a "game over" screen if the health is below zero. Game logic is simply the logic needed to make a game. It can also be called programming. Notice how health isn't capitalized. This is because local variables are usually not capitalized. While there is no direct rule that states this, most programmers tend to make global variables uppercase and local variables lowercase. The reason for this is that when you look at the screen, you will know which one is a global variable and which one is a local variable. Different programmers have different paradigms. So, if you are working with other programmers, make sure you know what they like and don't like in terms of naming variables. The addition of a new instance variable looks something like the following screenshot:

Creating a health variable

Once you have something like the preceding screenshot, you can click on OK.

Creating a health variable

Now you have an instance variable!

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

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