and I have another script attached to a test sprite with the following code
extends KinematicBody2D
export (Global.ShipType) var ship_type
Global is the name of the script in autoload, but I am getting
Parse Error: invalid index ‘ShipType’ in constant expression
I did something similar in another project and it works fine. Am I missing something?
This is really weird to have this problem; It suddenly started giving me this problem … actually preloading the script worked but that’s not a clean way of doing it !
wdaza | 2021-02-19 19:38
It actually still happens to me. And it also happens when I add new item into existing enums where I wouldn’t be able to choose the new item in the inspector (as public variable ) until I close and restart godot
Ok, I am not sure what is going on. But I closed the project and then re open it and it just worked. No error. Just posted here is someone is having similar issue
I answered this/a similar question but figured I’d also copy my answer here.
Note: I’m using v3.3.2
I had the same issue. For me, the actual problem was preloading - in an autoload script - a script which referenced another autoload script. See the following simplified example of the problem and then the solution afterward.
Autoscripts (in order) Config.gd
enum TILE_TYPE {NONE, FLOOR, WALL}
Global.gd
var Tile = preload("res://Tile.tscn") # <-- This caused the error
var tile = Tile.instance()
# do stuff with tile
Non-Autoscripts Tile.gd
export(Config.TILE_TYPE) var type = Config.TILE_TYPE.NONE
By removing the preload of Tile.tscn from Global.gd, the error went away. For me, this is an acceptable solution because I’m realizing in hindsight that instancing Tiles elsewhere is more appropriate.
I also verified that referencing enums in Config.gd from within Global.gd works. E.g.
If you need to preload - in an autoload script - a scene which references enums in another autoload script, similar to what I was doing in Global.gd, then you need to preload the autoload script similar to one of the following examples, depending on your use case.
Tile.gd
export (preload("res://Game/Config.gd").TILE_TYPE) var type
OR
const TileTypes = preload("res://Game/Config.gd").TILE_TYPE
Hope this helps save others some time and frustration
V3.5
In my case i fixed this by just moving the autoloaded script (or Singleton or whatever) to the top of the AutoLoad tab so it loads first than any other stuff. Damn the facepalm i got