@@ -9,30 +9,57 @@ GDScript
9
9
Annotation guidelines
10
10
~~~~~~~~~~~~~~~~~~~~~
11
11
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:
15
14
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.
21
18
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).
23
25
24
- ::
26
+ Currently, an annotation target can be a combination of the following
27
+ ``GDScriptParser::AnnotationInfo::TargetKind `` flags:
25
28
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
+ +-----------------+----------------------------------------------+-----------------------------+
31
50
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? **
34
52
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.
0 commit comments