Godot Script Enums

How Named Constants Clarify Your Code

Managing state with booleans is baby stuff. You’re better than that my humble apprentice. Allow me to introduce you to the magical world of enums!

In this issue of Godot Wizard...

An enum is a data type that consists of a set of named values. These values are constants and are used to represent a group of related items with human-readable names instead of numbers, making your code easier to read and maintain.

Advantages of Enums in Godot:

  1. Simplicity: Enums simplify the process of assigning and comparing states or types within your code. Instead of remembering arbitrary numbers for states, you can use descriptive names.

  2. Clarity: Using enums can make your code understandable at a glance, which is helpful for debugging and collaborative projects.

  3. Accuracy: Enums reduce errors caused by invalid values, as they restrict the input to the predefined names.

Best Use Cases for Enums in Godot:

  1. Game States Management: Enums are perfect for managing game states like MainMenu, Paused, Playing, etc. They make it clear what each state represents without needing to remember specific numbers or strings.

  2. Character Types or Attributes: If your game involves different types of characters or items, enums can be used to categorize them. For example, you might have an enum for character classes like Warrior, Mage, and Archer.

  3. Input Handling: Enums can be used to define different types of inputs or actions, making it easier to handle user interactions within the game logic.

  4. Difficulty Levels: You can define enums for difficulty levels such as Easy, Medium, and Hard, allowing you to adjust game settings dynamically based on the selected difficulty.

  5. Event Types: In event-driven programming, enums are useful to differentiate between types of events that can occur, such as Collision, TriggerEnter, or ScoreUpdate.

Using enums in these ways can enhance the readability and maintainability of your code, while also making it easier to manage and extend your game's logic.

In Godot, defining enums is straightforward. Here’s how you can define and use enums in your scripts:

Basic Enum Definition

To define a simple enum, you can use the enum keyword followed by the enum's name and the list of identifiers. Here is a basic example:

enum GameState {
    MainMenu,
    Playing,
    Paused,
    GameOver
}

In this example, GameState is an enum with four possible values. By default, Godot assigns integer values indexed from 0. So MainMenu will be 0, Playing will be 1, and so on.

Enum with Custom Values

You can also assign specific integer values to the enum items:

enum GameState {
    MainMenu = 1,
    Playing = 2,
    Paused = 3,
    GameOver = 4
}

This is useful when you need the enum values to match specific requirements or external systems.

Accessing Enum Values

To use enums in your code, simply refer to them by their name, prefixed by the enum name if needed:

var state = GameState.Playing

You can compare, assign, and check enum values just like regular integers:

if state == GameState.Playing:
    print("The game is currently playing.")

Using Enums without a Name

You can also define enums without giving them a name, especially when you're only going to use them once and don't need to group them semantically:

enum {Red, Green, Blue}
print(Red)  # This will output 0
print(Green) # This will output 1
print(Blue)  # This will output 2

Scope of Enums

Enum definitions in Godot are scoped within the class they are defined in. They can be accessed from outside the class by using the class name as a prefix:

# Inside another script or another part of the game
if player.state == Player.GameState.Playing:
    print("Player is in the playing state.")

Enums are a powerful feature of Godot that help you manage state and options, making your code cleaner and maintainable.

Learn More About ENUMS

Next Week…

The Ultimate Godot Quick Start Guide!