A constant is a variable whose value can never change. It's a tool to give a name to a specific value, which in turn can make the code easier to read and the value easier to reuse in different places.
In computer code, we often work with values that have a different meaning depending on the context. For example, the number 100
could be the player movement speed, their maximum health, or the number of coins they need to collect to gain an extra life.
By defining a constant, if this value never changes in the game, you can make the purpose of this value clear.
In the GDScript language, you can create a constant with the const
keyword. By convention, we write the name of constants with uppercase words separated by underscores. When reading long code files, it helps to instantly recognize constants. Here are three examples:
const SPEED := 100
const MAX_HEALTH := 100
const COINS_FOR_EXTRA_LIFE := 100
See Also
Related terms in the Glossary