Hello and welcome to the GDScript fundamental tutorial series.
In this episode, I go over constant variables in Godot GDScript.
A constant - also referred to as a constant variable - is a value that cannot be changed or altered by the application during runtime.
Put into simpler terms, a constant is a value that can never change.
To declare a constant, all you have to do is use the const
keyword.
const neverChanges = 100
In this case, you have a constant value called neverChanges
, and it is assigned the value 100.
Like a variable, you can also declare a constant as a typed value. Still, it is redundant since the value can never change.
const neverChanges: int = 100
const neverChangesAgain := 100
If you ever try to change the value, an error will be thrown.
const neverChanges = 100
neverChanges = 99 # throws an error
Constant values give you three benefits:
You can use literals instead of constants. To the compiler/program, it doesn’t make a difference.
However, for the sake of readability, it is easier to use constants.
Here is an example:
var playerSpeed = 100
var laserSpeed = 100
var bomerangSpeed = 100
# imagine the speed value of 100 is in many many places
This may seem alright when dealing with a few lines of code. However, imagine that the speed with a value of 100 is in many places.
Now imagine yourself trying to find and edit your literal value 100 when you decide that the speed value should be changed to 99
.
It would be easier to use constants.
const SPEED = 100
var playerSpeed = SPEED
var laserSpeed = SPEED
var bomerangSpeed = SPEED
Editing one line of code, in this case, const speed = 100
, will update the values of many lines of code that uses the
speed constant value.
Use constants when you want to declare a value that you know will never change in your game or application’s entire life cycle.
Some examples of game values that may never change (depends on the game):
On top of that, ensure that the naming convention of constants' names is visually different from variables.
const SPEED = 100 # SNAKE_UPPERCASE
const NORMAL_SPEED = 50 # SNAKE_UPPERCASE
var player_health = 100 # snake_lowercase
In the sample script, notice how constant values are all snake case uppercase. That means all words are separated by underlines, and all letters are in the uppercase format.
Compare that to the variable naming convention of snake lowercase. Meaning all words are separated by underscores, and all letters are lowercase.
You are free to choose your naming conventions.
Just make sure that constant names look different than variable names.
This makes it easy for you to see which variable names are free to change and which constant variables are not open to change.
When it comes to small projects, you may find that their really is no difference between the two. However, as a projet grows bigger, and the more people are working on the same project, the more benefits their are to properly using the correct declaration type.
A constant variable is useful for showing through code that a value will never change.
A variable on the other hand lets you know that the value will change throughout the lifetime of the program.
Visibly, a constant variable should look different than a variable.
It is recommended that you use upper case for constants while using lowercase for variables.
It is the visual difference that is key here, especially when debugging. The capital constants make it easier to debug code.
Constants | Godot GDScript Tutorial | Ep 03 video & article by Godot Tutorials is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License .