This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+8 votes

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
in Engine by (373 points)

I suppose this explains everything, thanks :)

2 Answers

+16 votes
Best answer

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
by (122 points)
selected by

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

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)

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

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

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.

+9 votes

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: https://github.com/godotengine/godot/issues/2966

by (1,608 points)

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, Thanks for the suggestion, seems like I forgot that this time :). Anyway, added it to the question.

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

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 ;)

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

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 :)

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.