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. 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. 8.
Adding
@export before var speed = 20 actually makes the variable visible in the
inspector.
|