Browse Source

Explain enums and mention ternary-if in GDScript reference

Bojidar Marinov 9 years ago
parent
commit
f11f60e21f
1 changed files with 44 additions and 1 deletions
  1. 44 1
      reference/gdscript.rst

+ 44 - 1
reference/gdscript.rst

@@ -75,6 +75,11 @@ here's a simple example of how GDScript looks.
     const answer = 42
     const thename = "Charly"
 
+    # enums (Godot 2.2+)
+
+    enum {UNIT_NEUTRAL, UNIT_ENEMY, UNIT_ALLY}
+    enum Named {THING_1, THING_2, ANOTHER_THING = -1}
+
     # built-in vector types
 
     var v2 = Vector2(1, 2)
@@ -180,6 +185,8 @@ keywords are reserved words (tokens), they can't be used as identifiers.
 +------------+---------------------------------------------------------------------------------------------------------------+
 | const      | Defines a constant.                                                                                           |
 +------------+---------------------------------------------------------------------------------------------------------------+
+| enum       | Defines an enum. (Godot 2.2+)                                                                                 |
++------------+---------------------------------------------------------------------------------------------------------------+
 | var        | Defines a variable.                                                                                           |
 +------------+---------------------------------------------------------------------------------------------------------------+
 | onready    | Initializes a variable once the Node the script is attached to and its children are part of the scene tree.   |
@@ -232,6 +239,8 @@ The following is the list of supported operators and their precedence
 +---------------------------------------------------------------+-----------------------------------------+
 | ``or`` ``||``                                                 | Boolean OR                              |
 +---------------------------------------------------------------+-----------------------------------------+
+| ``if x else``                                                 | Ternary if/else (Godot 2.2+)            |
++---------------------------------------------------------------+-----------------------------------------+
 | ``=`` ``+=`` ``-=`` ``*=`` ``/=`` ``%=`` ``&=`` ``|=``        | Assignment, Lowest Priority             |
 +---------------------------------------------------------------+-----------------------------------------+
 
@@ -516,6 +525,34 @@ expressions and must be assigned on initialization.
     const e = [1, 2, 3, 4][0]  # constant expression: 1
     const f = sin(20)  # sin() can be used in constant expressions
     const g = x + 20  # invalid; this is not a constant expression!
+    
+Enums
+^^^^^
+
+*Note, only available in Godot 2.2 or higher.*
+
+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 would also put all the values inside a
+constant dictionary of that name.
+
+::
+
+    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
+
+    enum State {STATE_IDLE, STATE_JUMP = 5, STATE_SHOOT}
+    # Is the same as:
+    const STATE_IDLE = 0
+    const STATE_JUMP = 5
+    const STATE_SHOOT = 6
+    const State = {STATE_IDLE = 0, STATE_JUMP = 5, STATE_SHOOT = 6}
+
 
 Functions
 ~~~~~~~~~
@@ -613,6 +650,13 @@ Short statements can be written on the same line as the condition::
         var x = 3 + 3
         return x
 
+Sometimes you might want to assign a different initial value based on a
+boolean expression. In this case ternary-if expressions come in handy
+(Godot 2.2+)::
+
+    var x = [true-value] if [expression] else [false-value]
+    y += 3 if y < 10 else -1
+
 while
 ^^^^^
 
@@ -650,7 +694,6 @@ in the loop variable.
     for i in range(2,8,2):
         statement  # similar to [2, 4, 6] but does not allocate an array
 
-
 Classes
 ~~~~~~~