0 votes

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://godotengine.org/qa/40827/how-to-declare-a-global-named-enum

in Engine by (135 points)

4 Answers

+2 votes
Best answer

Just declare the enums without any names.

Like this:

enum {
    EMPTY,
    SOLID
}

then you can access like this

TileType.EMPTY
by (50 points)
selected by
+2 votes

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

TileType.TileType.SOLID
by (2,264 points)

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?

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.

Great idea. Thanks again!

0 votes

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.

by (14 points)
0 votes

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!

by (18 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 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 webmaster@godotengine.org with your username.