Variable
2025/12/27
- Type
- Learning Resource
- Format
- Glossary Article
- Version
- General
- Subject Tags
- Created
- Updated
- 2026/02/16
- 2025/12/27
The exact definition of a variable in computing is close to how they're used in mathematics, but not exactly the same.
In maths, you might write let x be 10.
In GDScript, you would write:
var x = 10Contrary to maths, the value assigned to x can be changed throughout the lifetime of the program, at runtime.
For example, later, you might write:
x = 20To change the value to 20. Variables in programming are different from variables in algebra, which represent unknown values.
The act of writing var is called a "variable declaration". It tells GDScript you will now use that name to refer to a specific value.
A variable can be written without a value:
var ageIn which case, the value of the variable age is null (nothing) by default.
Later, you can assign a value to the variable:
func _ready() -> void:
age = 20Here, you assigned the value 20 to the variable age. This is like appending a label called "age" to the value 20; or, as some people say, putting the value 20 in a box called age.
For convenience, you can declare a variable with a value directly:
var age = 20You can also specify the type of the variable:
var age: int = 10In this example, only values of type int will be allowed to be assigned to the variable. For convenience, GDScript allows us to skip writing the type explicitly when it can infer the type:
var age := 10In this example, the variable age will be of type int too, because Godot can guess the type.
Variables must be named with a valid identifier. Identifiers in GDScript:
_, which is allowedBoth obey the same rules as variables, and it is possible to use the word variable to refer to them.
Variables are scoped; that means a variable is only accessible in the area where it's defined.
For example, the Player script below uses 2 variables: health and amount.
var health := 10
func take_damage(amount) -> void:
health -= amountWhile health is accessible to every function in the script, the parameter amount is only accessible in the function take_damage().
health is also accessible to other scripts as a member variable; other scripts can use player.health to assign it or read it.
Don't stop here. Step-by-step tutorials are fun but they only take you so far.
Try one of our proven study programs to become an independent Gamedev truly capable of realizing the games you’ve always wanted to make.
Get help from peers and pros on GDQuest's Discord server!
20,000 membersJoin ServerThere are multiple ways you can join our effort to create free and open source gamedev resources that are accessible to everyone!
Sponsor this library by learning gamedev with us onGDSchool
Learn MoreImprove and build on assets or suggest edits onGithub
Contributeshare this page and talk about GDQUest onRedditYoutubeTwitter…