0 votes

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!

in Engine by (40 points)

1 Answer

+3 votes

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.

by (27,962 points)

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: http://docs.godotengine.org/en/latest/classes/class_richtextlabel.html#numeric-constants

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: https://github.com/henriquelalves/GodotTIE

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?

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.

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

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.

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.