0 votes

I have two files in my project.
1. GameConstants.gd which is autoload.
2. CharacterConstants.gd which is reference (not autoload).


extends Node

const WEAPON_TYPE_SWORD = "Sword"
const WEAPON_TYPE_SPEAR = "Spear"


class_name CharacterConstants
extends Reference

     GameConstants.WEAPON_TYPE_SWORD: 0.2,
     GameConstants.WEAPON_TYPE_SPEAR: 0.1

If I run the editor I have error:
- Error: invalid index "WEAPONTYPESWORD" in constant expression

If I add new line to GameConstants.gd and save the file then everything is fine. And the game runs in both situations without any error. Why I have this error? Is it possible to disable them?

Godot version 3.5.1
in Engine by (15 points)

I found something similar.
Now I know why :)

1 Answer

+1 vote
Best answer

autoload is only useful for non-constant variables, that You want to change and keep those changes when replacing whole scene trees.
If You want a place to keep variables, that are always constant, never changed while project is running - use one or few class scripts extending a node - just like your charactercontants class. This class is now loaded BEFORE project is running, which means BEFORE autoload script is loaded. So You can't expect this class to refer to autoload without errors.

by (8,099 points)
selected by

Thanks. Do you know when Reference is loaded? I changed my constants classes extends to Reference and now it is working fine as well. Is there any class/objects/autoload lifecycle (I don't know how to name it) documentation?

I don't really know it from documenation, but it is kind of intuitive. Classes are loaded with all built-in features of Godot, whenever editor is running. All scripts exist, they have their code, but this code is not resolved, no signals received, they are not rooted to scene tree. But You can call their code in the editor, by using tool keyname script. Another keywords const and enumexpose variables, so they can be easily referred and autocompleted in other scripts. There is also static keyword, that exposes a whole function for this purposes. Thanks to this, You can make const variables by combinations of other consts, enums and static functions. And all of this happens before project is even run. Only after You run project, a scene tree and autoload are created, and all scenes are childed to the tree emitting their ready signals.

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 Frequently asked questions and 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 [email protected] with your username.