Menu

Godot Enum: How to Use Enums in GDScript

How enums work in Godot 4: named and unnamed enums, custom values, printing an enum's name, looping over keys, typed enum variables, exporting enums and bit flags.

This page includes runnable editors. Edit, run, and see output instantly.

An enum is a set of named constants. It replaces magic numbers like state = 2 with words like state = State.JUMP:

0 1 2
In the air

Values start at 0 and count up. Enum names use PascalCase and their keys UPPER_SNAKE_CASE.

Custom Values

Give a key a value with =. Keys after it continue counting from that value:

1 5 6 20

Getting the Name of an Enum Value

An enum value is an int, so print(state) prints a number. To show the name, look it up in the enum. A named enum is a read-only dictionary of names to values:

1
RUN
RUN
{ "IDLE": 0, "RUN": 1, "JUMP": 2 }

find_key() always works. keys()[value] only works when the values are 0, 1, 2 and so on with no gaps, because it indexes the list of names.

Looping Over an Enum

Because the enum is a dictionary, the usual dictionary methods work on it:

SWORD = 0
BOW = 1
STAFF = 2
3 weapons
true [0, 1, 2]

Unnamed Enums

An enum without a name puts its keys straight into the script as constants:

2
true

They are shorter to type but cannot be used as a type and have no keys(). Prefer named enums.

Enums with match

match reads well with enums, one branch per state. This is the core of a simple state machine:

standing still
running
jumping

See match for patterns, guards and wildcards.

Exporting an Enum

An exported variable with an enum type shows a drop-down in the Inspector:

extends CharacterBody2D

enum Team { RED, BLUE, GREEN }

@export var team: Team = Team.RED
@export_enum("Warrior", "Mage", "Rogue") var job: int

@export_enum gives the same drop-down without declaring an enum. It stores the index (0, 1, 2) in an int, or the text itself if you type the variable as String.

Enums from Another Script

An enum belongs to its class. Other scripts reach it through the class name, so a Player script with class_name Player and enum State is used as Player.State.RUN:

true
LOCKED

Enums as Bit Flags

Give each key a power of two and combine them with |. Test one with &:

5
false
true

For flags edited in the Inspector, use @export_flags("Fire", "Ice", "Poison") var resist := 0.

Frequently Asked Questions

How do I declare an enum in Godot?

Write enum Name { A, B, C } at the top level of a script, outside any function. The values count up from 0, so Name.A is 0, Name.B is 1 and Name.C is 2. You can set values yourself: enum Rarity { COMMON = 1, RARE = 5 }.

How do I get the name of an enum value as a string in Godot?

Use find_key(): State.find_key(state) gives "RUN" for the value 1. When the values start at 0 and have no gaps, State.keys()[state] gives the same result.

How do I export an enum in Godot 4?

Give an exported variable the enum's type: @export var state: State = State.IDLE. The Inspector then shows a drop-down with the enum's names. For a drop-down without an enum, use @export_enum("Warrior", "Mage") var job: int.

Can I use an enum from another script?

Yes, through the script's class name. If player.gd has class_name Player and enum State { IDLE, RUN }, another script can write var s: Player.State = Player.State.RUN.

Coddy programming languages illustration

Learn to code with Coddy

Get started