Describe the project you are working on:
Allow usage of the export keyword with const variables.
E.g: export const fish = "tainha"
Describe the problem or limitation you are having in your project:
As good practice immutable variables should be declared as const.
There is scenarios where the export of const variables are necessary,
like scripts that define const values that are exported for const mathematical
properties, dictionaries and etc.
Describe the feature / enhancement and how it helps to overcome the problem or limitation:
It allows the usage of const variables with export keyword.
Describe how your proposal will work, with code, pseudocode, mockups, and/or diagrams:
Allows the usage of export const fish = "tainha"
If this enhancement will not be used often, can it be worked around with a few lines of script?:
The variable will be declared as mutable, and this is a bad practice for const variables.
In globals.gd
export var fish = "tainha"
In a different script, someone crazy can do things like:
Globals.fish = "cardoso"
The other alternative is to declare the variable with a get method without a possible set
Is there a reason why this should be core and not an add-on in the asset library?:
This need to be done in the gdscript parser.
As of Godot 3.2, const in Godot is meant to be used for compile-time constants (like constexpr in C++). If an export statement can change a const's value, it's not a compile-time constant anymore (but just a run-time one).
That said, Godot 4.0 will support immutable variables using const in local scope (like const in C++). I don't know if the concept of compile-time constants will still be around in 4.0.
If an
exportstatement can change aconst's value, it's not a compile-time constant anymore (but just a run-time one).
That's something that is desired to avoid in this proposal and in the open PR.
At some point I wanted this feature, it's useful to have 'exportable consts' that can only be changed by the editor and at definition.
If you have a .gd script and want const to be editable from the inspector, where would the new value be stored?
If you expect it to be next to other exported variables, it means it's not a const, it would become something else, tied to instances of the script (so not global), rather than the script itself, as const currently is.
If you expect it to be on the script resource itself, that means you can't do that with a .gd script, as there is no place to store the value in that "format", other than simply editing said script.
exported for const mathematical properties
Mathematical constants should never be exported. There is no use case for changing the value of Tau for example.
Most helpful comment
As of Godot 3.2,
constin Godot is meant to be used for compile-time constants (likeconstexprin C++). If anexportstatement can change aconst's value, it's not a compile-time constant anymore (but just a run-time one).That said, Godot 4.0 will support immutable variables using
constin local scope (likeconstin C++). I don't know if the concept of compile-time constants will still be around in 4.0.