Variablesare used to represent an unknown value. The value of a variable will be determined by your game code. For a more indepth explantion of variables check out PHP Variables, Constants, and Datatypes.
A variables'sdatatypedefines the nature of the data stored within. The main datatypes featured in GDScript are:
int: This is an integer. Integers are whole numbers and they can be possitive or negative.
float: A float is a decimal. If you want fractional amounts to be included in the value, you will use the float datatype.
bool: A bool or boolean variable has a value of true or false.
string: A string's value is equal to text. When declaring a variable, string values are always put inside quotation marks.
Video: Variables, Dynamic Typing and Indentation Blocks
Now let's look at some GDScript code examples. If you are new to computer programming the following examples will make the concepts much more clear.
Mouse Hover over the ➼ blue text in the description to highlight the code.
# Integer Variablevar number = 666# Float Variablevar num = 6.66# Boolean Variablevar winner = true# String Variablevar king = "Hatnix"
➼ Integer - The variable is named number and its value is equal to 666 ➼ Float - The variable is named num and its value is 6.66 ➼ Boolen - The variable is named winner and its value is true. ➼ String - The variable is named king and its value is Hatnix. Notice Hatnix is put in quotes.
Dynamic Typing
The examples above are called variants. GDScript is a scripting language based on Python and just like Python, GDScript uses dynamic typing.
Typing a variable means when you declare the variable you also declare its datatype. Dynamic typing means you can either type or not type your variables.
Variables that are not typed, such as the examples above, are called variants.
Variable Typing
There are two different syntaxes for typing your variables:
(1) Explicit (2) Inferred
➼ Explict Typing - The variable queen explictly states that the variable is a string. ➼ Inferred Typing - The variable flower does not explicitly say it is a string, but it is still typed. The data typed is infered by the data type of Daisy
In the examples above, the values of queen and flower must always be strings. Otherwise, your code will return errors. This is the advantage of typing.
If you have a bug in your logic that sets a variable equal to the wrong datatype you will get an error message prompting you to fix your logic.
Integers and Floats
The advantage of variable typing is most clearly seen with float vs integer. Let's say you have a variable called score which holds a player's current score.
You will probably want score's value to be an integer. It is fairly easy to accidently formulate a scoring function that returns a float. Typing your score variable will pervent this kind of error.
var score: int = 0
Varients sometimes return unexpected results.
Homework
Declare the following variables in GDScript:
A string variable that holds a player's name.
A float variable that holds the distance a player drives.
An integer containing a player's current level
A variable that counts the number of living enemies.
A variable that indicates whether or not a level is completed.