What are constants?
We’ve talked extensively about variables, but there is a second type of container to put data in – constants. A constant is a special kind of variable. It also stores any kind of data type, just like variables, but you are not able to change it during the execution of a program; it stays constant. Otherwise, it is exactly the same as a variable and has the same restrictions on its name.
Constants in GDScript
Let’s take a closer look at a constant in a script. To define a constant, you need to use the const
keyword, like so:
const MAX_NUMBER_OF_BULLETS = 100
Note that the name of this constant is MAX_NUMBER_OF_BULLETS
. Unlike normal variables, using all capitals is the convention for constants; this is known as screaming snake case, or SCREAMING_SNAKE_CASE. This way, we know that we will not be able to change them in our code.
Using a constant is exactly the same as using a variable. Try this out in your code:
const FIREBALL_DAMAGE...