Is there way to create enums in GDScript?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Daniel Lewan
:warning: Old Version Published before Godot 3 was released.

I’m asking because code editor highlights world “enum” and I failed to find any info abut this in documentation (both on website and build-in)

Edit: I see that this is possible to use exported value like this but I wonder is there any GDScript only solution.

# Editor will enumerate as 0, 1 and 2
export(int, "Warrior", "Magician", "Thief") var character_class

Related feature request: Add enums to GDScript · Issue #2966 · godotengine/godot · GitHub

Bojidar Marinov | 2016-03-13 18:54

I suppose this explains everything, thanks :slight_smile:

Daniel Lewan | 2016-03-13 19:01

:bust_in_silhouette: Reply From: Bojidar Marinov

Currently there are a few ways to do it is:

# Multiple constants
const CORNER_W  = 0
const CORNER_NW = 1
const CORNER_NE = 2
const CORNER_E  = 3
const CORNER_SE = 4
const CORNER_SW = 5

# Dictionary (mutliline, since it avoids the ugly quotation marks) (thanks @sal_vager)
const CORNER = {
    W = 0,
    NW = 1,
    NE = 2,
    E = 3,
    SE = 4,
    SW = 5
}
# Class (thanks @DriNeo)
class Corner:
    const W = 0
    #....
# Preload
const Corner = preload("corner.gd") # Assuming it has the same structure to the class above

There is a feature request on github about it though: Add enums to GDScript · Issue #2966 · godotengine/godot · GitHub

You could use a constant dictionary for this.

I.e.

const HEROES = {
    "Warrior":0,
    "Magician":1,
    "Thief":2
}

So HEROES[“Magician”] would return 1

sal_vager | 2016-03-14 11:40

@sal_vager, Thanks for the suggestion, seems like I forgot that this time :). Anyway, added it to the question.

Bojidar Marinov | 2016-03-14 12:58

And what do you think about this ? If it’s bad, feel free to answer.

class Corners:
 const W  = 0
 const NW = 1
 const NE = 2
 const E  = 3
 const SE = 4
 const SW = 5

DriNeo | 2016-03-14 13:37

It looks like the first and second combined, but I’m going to add it too :)… For others, you can comment to the issue itself if you think you have a very bright idea :wink:

Bojidar Marinov | 2016-03-14 14:56

Wow plenty of options here. Thank you, :slight_smile:
I like subclass solution, it seems elegant.

Daniel Lewan | 2016-03-14 16:13

I like the use of the class for this, I had read that static variables were not supported so I didn’t want to suggest a class that had to be initialized.

I didn’t realize that constants would work for this, thank you, I’ve learned something today :slight_smile:

sal_vager | 2016-03-16 17:42

:bust_in_silhouette: Reply From: Leonard Meagher

Don’t know if anyone is still looking for an answer to this, but I’m not sure when it was added or if it’s in stable, but doing

enum NAME{
  someEnum,
  otherEnum
}

works as far as I know
then accessing doing

NAME.someEnum

Thanks! It does work. I guess it’s in the new build.

NikMirza | 2016-10-19 15:06

enum was added in 2.2-alpha/3.0 (see GDScript basic documentation)

For random googlers, this is the right answer, but in historical context, accepted answer was the right one (the question & answer was asked before 2.1)

splite | 2016-11-02 18:02

I copy/paste this code and Godot say “Unexpected token: Identifier: enum” :S
I use Godot 2.1.1 x11 64Bits

Nicolas Gualda | 2016-11-20 17:49

It worked when compiled from source. I guess it wasn’t in the latest stable build.

NikMirza | 2016-11-22 14:19

About the historical context, what is funnier is that I made the enums PR to godot :D.

Anyway, it is actually in 2.2-alpha/3.0.

Bojidar Marinov | 2016-12-22 13:13