What is "const" and numeric constants and how are they used?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Lindsay4047
:warning: Old Version Published before Godot 3 was released.

Sorry if this seems like a noob question, but in the documentation I see that there’s typically “numeric constants” at the top of each page. In some demo examples, I see similar code at the top of each script with similar formatting such as const SOME_CODE = 0.

What do the numbers mean and what does const do to the code?

Thanks a lot!

:bust_in_silhouette: Reply From: Zylann

const is an alternative to var, except that you cannot modify them. This way, you won’t accidentally change their value, and the fact they have their own naming convention (uppercase for example) make them spottable easily and help to understand the code.
It also allows GDScript to perform some optimizations, because in bytecode they are equivalent to raw values.

The value of these “numeric constants” depends on the context, I can’t answer this without seeing which ones you are talking about.

Thanks for the explanation! I was wondering about the need for capital letters.

I’m wondering on where the numbers from each numeric constant come from or what they even do. In this page, for example: RichTextLabel — Godot Engine (latest) documentation in English

There is a list there under numeric constants. ALIGN_LEFT = 0 ALIGN_CENTER = 1 ALIGN_RIGHT = 2

It’s obvious what the first half means, but I’m not sure where those numbers come from. It seems to be just counting them down, but why is there a need to do that?

This file has an example of what I mean: GitHub - henriquelalves/GodotTIE: A simple Text Interface Engine to control text output (like in a RPG dialogue) for Godot.

In one of the scripts, there’s a long list of constants, such as: const BUFF_DEBUG = 0 const BUFF_TEXT = 1 const BUFF_SILENCE = 2 const BUFF_BREAK = 3 const BUFF_INPUT = 4

What does assigning them to numbers do?

Lindsay4047 | 2016-09-05 03:22

Just as a complement, while the meaning of specific numeric constants depends on context, in general they are used to abstract away the meaning of the number itself in the code.

E.g.: if you have a character that can move in four cardinal directions, you can use 0,1,2,3 for the respective directions. A code with such numbers are hard to understand so you do const NORTH = 0 and now you replace the zeroes with the NORTHconstant, making the code much more readable.

vnen | 2016-09-05 03:38

So the numbers aren’t used again in the code? Is it more just to organize the constants?

Lindsay4047 | 2016-09-06 02:48

I think you are mixing up engine constants (the ones you see on the docs) with script constants (the ones you make on your own GDScript). They are not the same thing, in fact they must be different (you can’t reassign a value to an engine constant).

The engine constants are predefined and made to be used with specific functions. For instance, you linked the doc page for RichTextLabel which has a function push_align(int align). The argument must be an integer, so instead of doing push_align(1) you do push_align(ALIGN_CENTER) which does the same thing but it’s easier to understand.

Now script constants you can define any value you want and use as you wish. So, recalling my example, if you have a change_direction function that takes an integer value, you can do change_direction(NORTH) instead of change_direction(0).

Summing up, you don’t assign values to existing constants (after all, they are constant), you just create new constants if you need them. Assigning a value to a new constant do nothing more than creating a constant with such value. As Zylann said, it’s the same as a variable, except you can’t change the value later.

vnen | 2016-09-06 14:33

Are constants compile-time? Are there limitations of what things can be constant? I know that, for example, in C#, you can’t make a Vector2 be a constant.

Aaron Franke | 2019-11-21 17:38