GETTING STARTED IN GODOT – PART 5 – VARIABLES

By Alex K.

 

In this guide, we will talk about variables.  Variables are simply a way to store a value and then use it later on. 

 

1.     Practice.  In the _ready function, remove the pass statement and add the code below.  Notice that a var can hold a number or a string.

 

 

2.     Run your game.  At the very start, it should output the value 5 and the string “hello”.

3.     Okay, we are done with this.  Remove everything from the _ready function and re-add the pass statement.

 

 

4.     Important note.  If you create a variable in the _ready() function, it does not exist in the _process function.  Variables created inside functions only exist during the execution of that function.

If you want a variable to continue to exist, you need to declare it above the functions.  (For Java programmers, this is similar to the idea of instance variables.)

 

5.     Above the _ready function, create the variable named speed and give it a value of 10.

 

 

 

6.     In the _process function, in the lines changing the Icon’s position, replace all of the 10 values by the variable speed.

 

7.     Run the game and the game play will be the exact same.  Sigh…  But try changing the speed variable’s value to say 20 and see what happens.

Good job! You can now change how fast the player moves with only a single value.

8.     Adding @export before var speed = 20 actually makes the variable visible in the inspector. 

Try it!  Pretty cool stuff!

RECAP

Adding the @export below adds Speed as a property in the Inspector (seen on the right).