Numbers
Part of the Fundamentals section of Coddy's GDScript journey. Lesson 5 of 92.
Variables store data in your program. Each variable has a name and a value, and you use the name to get the value back later.
In GDScript, you create a variable with the keyword var, followed by the name, an equals sign and the value:
var variable_name = valueGDScript has two types of numbers:
int: a whole number, such as3or-15float: a number with a decimal point, such as13.2or0.98
Here coins holds the int value 3 and speed holds the float value 13.2:
var coins = 3
var speed = 13.2
print(coins)
print(speed)This prints 3 and then 13.2. The decimal point decides the type: 0.0 is a float even though its value is zero.
The keyword var is written once, when the variable is created. To change the value later, assign it without var: coins = 10.
Note: a variable name cannot start with a digit. Use underscores to separate words, as in player_score.
Challenge
BeginnerCreate two variables under the comment:
coinswith theintvalue25speedwith thefloatvalue3.5
Do not change the print() lines. When your variables are correct, the program prints:
coins = 25
speed = 3.5Try it yourself
extends Node
func _ready():
# Create the variables coins and speed below
# Don't change the lines below
print("coins = ", coins)
print("speed = ", speed)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators10Functions
Declare a FunctionArgumentsReturnReturn TypesRecap - Sigma FunctionRecap - Validation FunctionDefault Values5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary OperatorPractice on your own: Online GDScript compiler