What is enum?

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

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

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

const State = {
STATE_IDLE = 0.5,
STATE_PATROL = 1.3,
STATE_ATTACK = 2.5
}

Yes, it can be a float type.

SynthED | 2021-06-29 00:23

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.

CardboardComputers | 2021-09-23 11:43

:bust_in_silhouette: Reply From: kidscancode

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

There are some good examples in the docs:

:bust_in_silhouette: Reply From: SIsilicon

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

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

JulioYagami | 2018-12-17 13:19