VARIABLES

Variables are an important concept in programming, 

they are pieces of data that you can define, read, and change the value of 

when storing information. Variables can be thought of as boxes, with a name

and a value.

At any time we can find the box from its name and use the value in the box.

There are different types of variables but the main ones are: 

Whole numbers (1, 2, 3) (integer)

 Decimal numbers (0.1, 2.7, 3.6) (float)

 Text (“Hello World”) (String)

 Booleans (True / False) (bool)

To create a variable in GD Script, we first need to define it. When programming, we write the

variables at the top of the script, so we will define all our variables below the extends Node2D line.


To define a variable we will write the keyword var to specify this is a variable. This will be

followed by the name of the variable (eg: score), a colon and then the data type of the variable, then an equals sign, and then the default value of the variable, which we will use a value of 0.

Eg: var varName: dataType = value

var score: int = 0

To test a variable, we can log the value to the game’s output. 

To do this we will use the _ready function. We will discuss functions later on.

For now, just know functions are blocks of code that can be called at any time. 

The _ready function will be called at the start of the game. 

In the function, we will replace the pass line with our own code 

to print out the name variable.

Run this new scene

Click the Run Current Scene button

Or Press F6

Output

If you change the value of the variable and run the scene again, 

you will see the output has changed.