+8 votes

I really don't know what it is and what I can do with that.

in Engine by (671 points)

Note that the enumerations cannot be of type float, while the list of constants:

const State = {
STATEIDLE = 0.5,
STATE
PATROL = 1.3,
STATE_ATTACK = 2.5
}

Yes, it can be a float type.

I suppose that is rather a constant dictionary, not a dictionary of constants; you can modify the dictionary at runtime, including modifying or deleting keys. Further, semantically, an enum type should probably be used primarily as a set of unique keys, not as list of constants.

2 Answers

+19 votes
Best answer

An enum is a keyword used in gdscript for making a bunch of related constants.

enum {CONST1, CONST2, SO_ON}

# Would be the same as
const CONST1 = 0
const CONST2 = 1
const SO_ON = 2

As you can see, the constants' default values are incremented numbers starting from zero. Some of these constants can be given a different value on assignment.

enum {DEFAULT, CUSTOM = 20}

# Would be the same as
const DEFAULT = 0
const CUSTOM = 20

They can also be assigned with another word to create a dictionary holding multiple constants.

enum State {STATE_IDLE, STATE_PATROL, STATE_ATTACK}

# Would be the same as
const State = {
    STATE_IDLE = 0,
    STATE_PATROL = 1,
    STATE_ATTACK = 2
}

*Note: as of Godot 3.1, accessing enumerators like the last one would require prepending the name of the dictionary plus a . operator.

STATE_IDLE # Worked prior to Godot 3.1 alpha 3
State.STATE_IDLE # only working way as of mentioned version
by (3,891 points)
selected by

I realized that enums can be used to do states systems for the player character, for example.

+2 votes

Enums are for convenience. They simplify the process of creating multiple constants.

There are some good examples in the docs:
https://docs.godotengine.org/en/latest/getting_started/scripting/gdscript/gdscript_basics.html#enums

by (20,893 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.