From f337bb0f825205eea97fd8418951db9f2fb89472 Mon Sep 17 00:00:00 2001 From: Hana - Piralein <48352564+Piralein@users.noreply.github.com> Date: Fri, 19 May 2023 18:19:33 +0200 Subject: [PATCH] improve description of enums --- .../scripting/gdscript/gdscript_basics.rst | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/tutorials/scripting/gdscript/gdscript_basics.rst b/tutorials/scripting/gdscript/gdscript_basics.rst index 95379c08822..4c7c02c274b 100644 --- a/tutorials/scripting/gdscript/gdscript_basics.rst +++ b/tutorials/scripting/gdscript/gdscript_basics.rst @@ -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 ` 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