+2 votes

I have a script in autoload with the following codes

extends Node

enum ShipType{Corvette,Frigate,Destroyer,Crusier}

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?

in Engine by (175 points)

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 !

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

5 Answers

0 votes
Best answer

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

by (175 points)
+2 votes

To anyone who's still experiencing this in 2020 (for me 3.2.2), instead of waiting for a solution, you could try:
1. Restarting Godot (didn't work for me)
2. Preload the autoload script (Yeap...) by:

export (preload("Path_to_your_script").ENUM_NAME) var NAME
by (26 points)

Weird that you need to add preload but this works! Thanks!

Glad it helped!

+2 votes

In my case this happened when I tried to access enum from singleton object. You can get over this by defining some class_name for script as:

extends Node
class_name GlobalStatic

enum ShipType{Corvette,Frigate,Destroyer,Crusier}

and then use it as.

extends KinematicBody2D

export (GlobalStatic.ShipType) var ship_type 
by (18 points)
0 votes

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)



var Tile = preload("res://Tile.tscn") # <-- This caused the error
var tile = Tile.instance()
# do stuff with tile


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.


func IsAWall(pTileType) -> bool:
   return pTileType == Config.TILE_TYPE.WALL

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.


export (preload("res://Game/Config.gd").TILE_TYPE) var type


const TileTypes = preload("res://Game/Config.gd").TILE_TYPE

Hope this helps save others some time and frustration

by (18 points)
0 votes

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

by (14 points)
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.