Error when accessing global enum

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

I am storing an enum in TileType.gd as follows:

extends Node

enum TileType {
	EMPTY,
	SOLID
}

When I try to access a value in the enum from another file, RoomGenerator.gd, I get this error:

 res://scenes/RoomGenerator.gd:72 - Invalid get index 'SOLID' (on base: 'GDScript').

In this file, the enum is declared like this:

const TileType = preload("res://scripts/TileType.gd")

According to other Q/A’s I’ve seen online (see below), this seems to be the way to use a global enum. Am I missing something?

Thanks!

https://forum.godotengine.org/40827/how-to-declare-a-global-named-enum

:bust_in_silhouette: Reply From: hilfazer

These days enum values require to be prepended with the name of enum, like this:

TileType.TileType.SOLID

Thank you, that worked for me.

I suppose this makes sense as I’m loading in a class with an enum inside it, but it still feels bloated. Is there a way to use the syntax “Enum.Value” in multiple scripts?

Ben Humphries | 2020-06-02 10:14

I don’t think it’s possible. What you can do is not use an enum but a couple of constants:

const EMPTY = 0
const SOLID = 1

As a bonus you won’t be able to accidentally delete these constants. Sadly, enum’s contents can be deleted.

hilfazer | 2020-06-02 11:31

Great idea. Thanks again!

Ben Humphries | 2020-06-02 12:14

:bust_in_silhouette: Reply From: sairam123

Just declare the enums without any names.

Like this:

enum {
    EMPTY,
    SOLID
}

then you can access like this

TileType.EMPTY
:bust_in_silhouette: Reply From: Pdcl

I run into this behaviour when autoloading a script file that contains an enum. To fix this behaviour i usually assign each enum value an index number, so it would be:

extends Node

enum TileType {
    EMPTY = 0,
    SOLID = 1
}

If I omit the index number, then I get that error.

:bust_in_silhouette: Reply From: Nenja

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.

Global.gd

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.

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 others!