What is the best practice to manage a global constants gdscript file?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By digitalcyclops

I want to have a constants file that has a bunch of enums, ints, vectors, and preloads that I can use in all gdscript files. If I want to export some of these enums, the only way I know is to preload this constants file in all the scripts, which seems pretty inconvenient and potentially redundant loading (not sure if preload is handled intelligently across different gdscript files). Is there an equivalent of python import or C++ #include in gdscript?

imagine the following scenario:

# constants.gd

enum Item = { CONSUMABLE, WEAPON, ARMOR, KEYITEM };
enum Obj = { BUILDING, NPC, OTHER };```

\# spell.gd
```const constants = preload("res://constants.gd");
export (constants.Spell) var Type;```

\# item.gd
```const constants = preload("res://constants.gd");
export (constants.Item) var Type;```

\# obj.gd
```const constants = preload("res://constants.gd");
export (constants.Obj) var Type;```


I'm not sure if calling preload() on the same resource will consume more memory, imagine hundreds or thousands of obj/item/spell classes. Is there is a better way at handling this situation. I think it's a very common practice to have a separate constants file. But in practice, it's much trickier than it should be.

Perhaps we can take AutoLoad to the next level with AutoPreload in the next version?

[wrap=comment]
For my project, I have another global variables gd file (let's call it globals.gd) that contains non-constants.

All my classes potentially need access to both constants.gd and globals.gd.
globals.gd makes reference to constants.gd
This creates dangerous cyclic references. What is the best way to handle multi-layered inclusion (inheritance)?
[wrap=footnote]digitalcyclops | 2020-05-20 00:42[/wrap]
[/wrap]

[wrap=comment]
Each Resource is preloaded only once so multiple preloads will not consume extra memory.
[wrap=footnote]hilfazer | 2020-05-20 08:10[/wrap]
[/wrap]

[wrap=comment]
I would say that the way things are is ok, but not ideal. The AutoPreload is just an idea since we have AutoLoads. I think having something like AutoPreload (if it's not too technically consuming) will resolve this more gracefully.
[wrap=footnote]digitalcyclops | 2020-05-20 23:06[/wrap]
[/wrap]
:bust_in_silhouette: Reply From: kuzey

You can simply autoload your constants.gd file and call it from anywhere like that:

var mySpell: int = Constants.spell

Your constants.gd file can look like that:

Node

const spell: int = 100

To add constants.gd file, go to project settings and click “AutoLoad” tab:

And find and add your constants.gd file from “Path” input.

See docs: Singletons (Autoload) — Godot Engine (latest) documentation in English