Skip to content

Commit 4e12a73

Browse files
committed
GDScript: Update annotation description and development guidelines
1 parent 7a7ac7d commit 4e12a73

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

contributing/development/core_and_modules/scripting_development.rst

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,57 @@ GDScript
99
Annotation guidelines
1010
~~~~~~~~~~~~~~~~~~~~~
1111

12-
..
13-
This description intentionally avoids mention of implementation and
14-
compilation details because these are often inconsistent between annotations
12+
In Godot 3 we had a lot of keywords (including ``tool``, ``export``, ``onready``).
13+
This had the following disadvantages:
1514

16-
Create annotations for modifiers that act on the script or its code.
17-
Additionally, create annotations for behavior that is specific to the Godot
18-
engine and editor; if the primary purpose is to affect the way that the engine
19-
or editor treats or interacts with the script, implement the token as an
20-
annotation.
15+
- Overcomplication of the language grammar.
16+
- Possible conflicts with user identifiers.
17+
- Need to touch the parser in a major way to add a new keyword.
2118

22-
Do not create annotations for general programming language features.
19+
So in Godot 4 we introduced the concept of annotations. An annotation is a modifier
20+
(an attribute, a marker) that can be applied to an entire script, a declaration,
21+
a statement, or a location in the source code. Annotations can optionally take
22+
additional arguments, listed in parentheses. Annotation arguments must be constant
23+
expressions or string literals (in a few special cases where the argument value
24+
must be resolved in the parser rather than in the analyzer).
2325

24-
::
26+
Currently, an annotation target can be a combination of the following
27+
``GDScriptParser::AnnotationInfo::TargetKind`` flags:
2528

26-
# Affects how the editor treats this script.
27-
@icon("res://path/to/class/icon.svg")
28-
29-
# Affects how the engine interacts with this script.
30-
@onready var character_name = $Label
29+
+-----------------+----------------------------------------------+-----------------------------+
30+
| **Target Kind** | **Description** | **Example** |
31+
+=================+==============================================+=============================+
32+
| ``SCRIPT`` | The entire script. | ``@tool`` |
33+
+-----------------+----------------------------------------------+-----------------------------+
34+
| ``CLASS`` | A class (both the outermost and inner ones). | ``@abstract`` |
35+
+-----------------+----------------------------------------------+-----------------------------+
36+
| | ``VARIABLE`` | Other types of class members. | ``@export`` |
37+
| | ``CONSTANT`` | | |
38+
| | ``SIGNAL`` | | |
39+
| | ``FUNCTION`` | | |
40+
+-----------------+----------------------------------------------+-----------------------------+
41+
| ``STATEMENT`` | Statements inside a function. | ``@warning_ignore`` |
42+
+-----------------+----------------------------------------------+-----------------------------+
43+
| ``STANDALONE`` | Some place inside a class or a line | | ``@export_category`` |
44+
| | in the source code. | | ``@warning_ignore_start`` |
45+
| | | |
46+
| | 1. Class pseudo-members. | |
47+
| | 2. Markers for the start and end | |
48+
| | of a warning-ignoring region. | |
49+
+-----------------+----------------------------------------------+-----------------------------+
3150

32-
# static is a general programming language feature.
33-
static var num_players = 2
51+
**When to use a keyword and when an annotation to implement a new feature?**
3452

35-
For historical reasons, some existing annotations and keywords do not strictly
36-
follow these guidelines. Choosing between implementing a feature as an
37-
annotation or as a language keyword is a nuanced decision that should be made
38-
through discussion with other GDScript developers.
53+
- **General rule:** Do not use keywords unless they are required by the language grammar
54+
and the parser. Do not use annotations for independent syntactic units.
55+
- Use keywords for class member declarators (``var``, ``const``, ``func``).
56+
- Use annotations for class member modifiers (``@export``, ``@onready``, ``@abstract``).
57+
- Use keywords for operators within expressions (``and``), for delimiters in statements
58+
(``in``, ``when``) and declarations (``extends``).
59+
60+
.. note::
61+
62+
For historical reasons, some existing annotations and keywords do not strictly
63+
follow these guidelines. Choosing between implementing a feature as an annotation
64+
or as a language keyword is a nuanced decision that should be made through discussion
65+
with other GDScript developers.

tutorials/scripting/gdscript/gdscript_basics.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,9 @@ GDScript also supports :ref:`format strings <doc_gdscript_printf>`.
449449
Annotations
450450
-----------
451451

452-
Annotations are special tokens in GDScript that act as modifiers to a script or
453-
its code and may affect how the script is treated by the Godot engine or
454-
editor.
452+
Annotations are special tokens in GDScript that act as modifiers to an entire script,
453+
a declaration, a statement, or a location in the source code. Annotations may affect
454+
how the script is treated by the Godot editor and the GDScript compiler.
455455

456456
Every annotation starts with the ``@`` character and is specified by a name. A
457457
detailed description and example for each annotation can be found inside the

0 commit comments

Comments
 (0)