Skip to content

Commit

Permalink
Add documentation for GDScript when statement
Browse files Browse the repository at this point in the history
  • Loading branch information
tetrapod00 committed Aug 26, 2024
1 parent 5dd72ba commit 2b8bde5
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions tutorials/scripting/gdscript/gdscript_basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ in case you want to take a look under the hood.
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| as | Cast the value to a given type if possible. |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| when | Used by `pattern guards <Pattern guards_>`_ in match_ statements. |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| self | Refers to current class instance. |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| super | Resolves the scope of the parent method. See `Inheritance`_. |
Expand Down Expand Up @@ -1654,10 +1656,10 @@ Basic syntax

::

match <expression>:
match <test value>:
<pattern(s)>:
<block>
<pattern(s)> when <guard expression>:
<pattern(s)> when <pattern guard>:
<block>
<...>

Expand Down Expand Up @@ -1790,9 +1792,13 @@ The following pattern types are available:
Pattern guards
""""""""""""""

A *pattern guard* is an optional condition that follows the pattern list
and allows you to make additional checks before choosing a ``match`` branch.
Unlike a pattern, a pattern guard can be an arbitrary expression.

Only one branch can be executed per ``match``. Once a branch is chosen, the rest are not checked.
If you want to use the same pattern for multiple branches or to prevent choosing a branch with too general pattern,
you can specify a guard expression after the list of patterns with the ``when`` keyword::
you can specify a pattern guard after the list of patterns with the ``when`` keyword::

match point:
[0, 0]:
Expand All @@ -1808,9 +1814,9 @@ you can specify a guard expression after the list of patterns with the ``when``
[var x, var y]:
print("Point (%s, %s)" % [x, y])

- If there is no matching pattern for the current branch, the guard expression
- If there is no matching pattern for the current branch, the pattern guard
is **not** evaluated and the patterns of the next branch are checked.
- If a matching pattern is found, the guard expression is evaluated.
- If a matching pattern is found, the pattern guard is evaluated.

- If it's true, then the body of the branch is executed and ``match`` ends.
- If it's false, then the patterns of the next branch are checked.
Expand Down

0 comments on commit 2b8bde5

Please sign in to comment.