This is the seventh chapter of the “Everyone can Make games” series. This series of tutorials is aimed at absolute novices without any game development experience. It is not difficult to make a micro channel game, you can also do it.

The small game development tool used in the tutorial is wechat official small game production tool: wechat small game production tool

If you are interested in game development, please follow my wechat official account: Little Ant Game Development

In this section, we’ll look at the recorder in game development — variables.

Before we get started, a few questions:

  • What does a variable do?

  • What are the types of variables?

  • When do we use variables?

  • How do you decide which type of variable to use?

With these questions in mind, read on to find out.

Function of variables

I liken variables to game development recorders, helping to keep records. What are the things that need to be recorded in the game? Take the simplest masturbation mini-game as an example, in which you might want to record the following:

  • score

  • The amount of blood on your plane

  • The amount of health of enemy planes

  • Is the game over

Players need to know how many points they have scored in the game (one point for destroying an enemy plane), so a “score” variable is needed to help keep track.

Usually the main character of the game (your fighter) is required to have health, and every time it is hit, health is reduced by 1. When it reaches 0, the game is over. So we need a variable called “fighter health” to help keep track of health.

As the enemy also need to have health, the small enemy aircraft health is 1, a kill, the large enemy aircraft health is 2, need to attack twice to kill. So we need a “enemy health” variable to record.

Finally, also need a “game over” variables to record whether to end the game, because the game ended, appear to the end of the game, and after the game, the player can carry on the operation of the game should not be longer, so we need a record, to prompt the us, when will the game tips, when not allow players to operate.

Anything that needs to be recorded in a game can be recorded using “variables”, which is the role of “variables” in game development.

Type of variable

Variables are of two types: global and local.

A global variable is a variable that is recorded throughout the game, and a local variable is a variable that is recorded on some object in the game.

As an example, the game contains four variables: “score”, “fighter health”, “enemy health” and “whether the game is over”.

“Score” is a record of how many enemy planes the player destroyed over the course of the game, so “score” is a global variable.

“Fighter health” is a record of the player’s health, once it goes to zero, it means the game is over, so “fighter health” is also a global variable.

“Game over” is a record of the entire state of the game, so it is also a global variable.

“Enemy hit points” we mentioned above, small enemy hit points 1, large enemy hit points 2. So this health records the health of each enemy aircraft, the variable records the data of each enemy aircraft in the game, so it is a local variable.

To summarize, use “global variables” if you’re recording for the entire game, and “local variables” if you’re recording for a single object in the game.

Create a variable

Let’s learn how to create global and local variables in a project.

Go to the “data area” on the far right and click the “New Variable” button.

In the dialog box that pops up, fill in the name of the variable “score”, then select “Global” and click “OK”.

Looking at the data area, a global “score” variable appears. Now a global variable is created!

Next, create two other global variables, “fighter health” and “is the game over”. After creation, three global variables will appear in the data area.

Tip: if you select any global variable, a red circle with a minus sign will appear in front of it. Click to remove the variable.

Let’s go ahead and create the last local variable: “Enemy health”.

Select the red foxx in the hierarchy and click “New Variable” in the data area.

Enter the name of the variable “Enemy hit points” in the pop-up box, then select “Local” (note that the image displayed in the local is the image of the red enemy you selected) and click “OK”.

Looking at the data area, a local variable of enemy hitpoints appears under enemy aircraft.

Let’s set the value of the variable. Double-click the value 0 behind “enemy health” to modify the value. Here we set the red enemy health to 1.

Next, we add a “enemy health” local variable to the big blue enemy and set the value to 2, which looks like this.

Finally we set the value of the global variable, the score defaults to 0, because we have not destroyed any enemy planes! End game the default value is 0,0 indicates that the game is not over, and 1 indicates that the game is over. Fighter HP is set to 1, which means we’re only one hit away from death.

To summarize

In this section we learned about the role of variables in game development, recording data, and the two types of variables: global variables and local variables.

Use global variables when dealing with the entire game and local variables when dealing with an object in the game.

Finally, we learned how to create global and local variables in a project and edit their values.

practice

Think about the classic game space Invaders. What are the things that need to be recorded, global and local?