improve description of enums

This commit is contained in:
Hana - Piralein
2023-05-19 18:19:33 +02:00
parent 8e45b3c141
commit f337bb0f82

View File

@@ -908,26 +908,38 @@ Enums
Enums are basically a shorthand for constants, and are pretty useful if you
want to assign consecutive integers to some constant.
If you pass a name to the enum, it will put all the keys inside a constant
dictionary of that name.
.. important:: In Godot 3.1 and later, keys in a named enum are not registered
as global constants. They should be accessed prefixed by the
enum's name (``Name.KEY``); see an example below.
::
enum {TILE_BRICK, TILE_FLOOR, TILE_SPIKE, TILE_TELEPORT}
# Is the same as:
const TILE_BRICK = 0
const TILE_FLOOR = 1
const TILE_SPIKE = 2
const TILE_TELEPORT = 3
If you pass a name to the enum, it will put all the keys inside a constant
:ref:`Dictionary <class_Dictionary>` of that name. This means all constant methods of
a dictionary can also be used with a named enum.
.. important:: Keys in a named enum are not registered
as global constants. They should be accessed prefixed
by the enum's name (``Name.KEY``).
::
enum State {STATE_IDLE, STATE_JUMP = 5, STATE_SHOOT}
# Is the same as:
const State = {STATE_IDLE = 0, STATE_JUMP = 5, STATE_SHOOT = 6}
# Access values with State.STATE_IDLE, etc.
func _ready():
# Access values with Name.KEY, prints '5'
print(State.STATE_JUMP)
# Use constant dictionary functions
# prints '["STATE_IDLE", "STATE_JUMP", "STATE_SHOOT"]'
print(State.keys())
Functions