Cannot use enum as variable

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

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?

This is really weird to have this problem; It suddenly started giving me this problem :frowning: … 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

lowpolygon | 2021-02-20 00:44

:bust_in_silhouette: Reply From: lowpolygon

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

:bust_in_silhouette: Reply From: dirtsea

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

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

lpancho | 2020-10-12 17:05

Glad it helped!

dirtsea | 2020-10-19 10:43

:bust_in_silhouette: Reply From: darkcanvas

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 
:bust_in_silhouette: Reply From: Nenja

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, 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 from within 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, then you need to preload the autoload script similar to one of the following examples, depending on your use case.

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


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

Hope this helps save others some time and frustration

:bust_in_silhouette: Reply From: dip000

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

In version 4.2 I could fix this by using const keyword instead of the var keyword, so instead of

var AnyEnum = preload("res://")

I used

const AnyEnum = preload("res://")