Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document GDScript "when" and clarify pattern guard docs #9787

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions tutorials/scripting/gdscript/gdscript_basics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ in case you want to take a look under the hood.
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| match | See match_. |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| when | Used by `pattern guards <Pattern guards_>`_ in ``match`` statements. |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| break | Exits the execution of the current ``for`` or ``while`` loop. |
+------------+---------------------------------------------------------------------------------------------------------------------------------------------------+
| continue | Immediately skips to the next iteration of the ``for`` or ``while`` loop. |
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