Menu
CoddyTech

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 = value

GDScript has two types of numbers:

  • int: a whole number, such as 3 or -15
  • float: a number with a decimal point, such as 13.2 or 0.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 icon

Challenge

Beginner

Create two variables under the comment:

  • coins with the int value 25
  • speed with the float value 3.5

Do not change the print() lines. When your variables are correct, the program prints:

coins = 25
speed = 3.5

Try 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)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Online GDScript compiler