gdscript_documentation_comments.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. .. _doc_gdscript_documentation_comments:
  2. GDScript documentation comments
  3. ===============================
  4. In GDScript, comments can be used to document your code and add descriptions to the
  5. members of a script. There are two differences between a normal comment and a documentation
  6. comment. Firstly, a documentation comment should start with double hash symbols
  7. ``##``. Secondly, it must immediately precede a script member, or for script descriptions,
  8. be placed at the top of the script. If an exported variable is documented,
  9. its description is used as a tooltip in the editor. This documentation can be
  10. generated as XML files by the editor.
  11. Documenting a script
  12. --------------------
  13. Comments documenting a script must come before any member documentation. A
  14. suggested format for script documentation can be divided into three parts.
  15. - A brief description of the script.
  16. - Detailed description.
  17. - Tutorials and deprecated/experimental marks.
  18. To separate these from each other, the documentation comments use special tags.
  19. The tag must be at the beginning of a line (ignoring preceding white space)
  20. and must have the format ``@``, followed by the keyword.
  21. Tags
  22. ~~~~
  23. +-------------------+--------------------------------------------------------+
  24. | Brief description | No tag. Lives at the very beginning of |
  25. | | the documentation section. |
  26. +-------------------+--------------------------------------------------------+
  27. | Description | No tag. Use one blank line to separate the description |
  28. | | from the brief. |
  29. +-------------------+--------------------------------------------------------+
  30. | Tutorial | | ``@tutorial: https://example.com`` |
  31. | | | ``@tutorial(The Title Here): https://example.com`` |
  32. +-------------------+--------------------------------------------------------+
  33. | Deprecated | | ``@deprecated`` |
  34. | | | ``@deprecated: Use [AnotherClass] instead.`` |
  35. +-------------------+--------------------------------------------------------+
  36. | Experimental | | ``@experimental`` |
  37. | | | ``@experimental: This class is unstable.`` |
  38. +-------------------+--------------------------------------------------------+
  39. For example::
  40. extends Node2D
  41. ## A brief description of the class's role and functionality.
  42. ##
  43. ## The description of the script, what it can do,
  44. ## and any further detail.
  45. ##
  46. ## @tutorial: https://example.com/tutorial_1
  47. ## @tutorial(Tutorial 2): https://example.com/tutorial_2
  48. ## @experimental
  49. .. warning::
  50. If there is any space in between the tag name and colon, for example
  51. ``@tutorial :``, it won't be treated as a valid tag and will be ignored.
  52. .. note::
  53. When the description spans multiple lines, the preceding and trailing white
  54. spaces will be stripped and joined with a single space. To preserve the line
  55. break use ``[br]``. See also `BBCode and class reference`_ below.
  56. Documenting script members
  57. --------------------------
  58. Members that are applicable for documentation:
  59. - Signal
  60. - Enum
  61. - Enum value
  62. - Constant
  63. - Variable
  64. - Function
  65. - Inner class
  66. Documentation of a script member must immediately precede the member or its annotations
  67. if it has any. The description can have more than one line but every line must start with
  68. the double hash symbol ``##`` to be considered as part of the documentation.
  69. Tags
  70. ~~~~
  71. +--------------+--------------------------------------------------+
  72. | Description | No tag. |
  73. +--------------+--------------------------------------------------+
  74. | Deprecated | | ``@deprecated`` |
  75. | | | ``@deprecated: Use [member another] instead.`` |
  76. +--------------+--------------------------------------------------+
  77. | Experimental | | ``@experimental`` |
  78. | | | ``@experimental: This method is incomplete.`` |
  79. +--------------+--------------------------------------------------+
  80. For example::
  81. ## The description of the variable.
  82. ## @deprecated: Use [member other_var] instead.
  83. var my_var
  84. Alternatively, you can use inline documentation comments::
  85. signal my_signal ## My signal.
  86. enum MyEnum { ## My enum.
  87. VALUE_A = 0, ## Value A.
  88. VALUE_B = 1, ## Value B.
  89. }
  90. const MY_CONST = 1 ## My constant.
  91. var my_var ## My variable.
  92. func my_func(): ## My func.
  93. pass
  94. class MyClass: ## My class.
  95. pass
  96. The script documentation will update in the editor help window every time the script is updated.
  97. If any member variable or function name starts with an underscore, it will be treated as private.
  98. It will not appear in the documentation and will be ignored in the help window.
  99. Complete script example
  100. -----------------------
  101. ::
  102. extends Node2D
  103. ## A brief description of the class's role and functionality.
  104. ##
  105. ## The description of the script, what it can do,
  106. ## and any further detail.
  107. ##
  108. ## @tutorial: https://example.com/tutorial_1
  109. ## @tutorial(Tutorial 2): https://example.com/tutorial_2
  110. ## @experimental
  111. ## The description of a signal.
  112. signal my_signal
  113. ## This is a description of the below enum.
  114. enum Direction {
  115. ## Direction up.
  116. UP = 0,
  117. ## Direction down.
  118. DOWN = 1,
  119. ## Direction left.
  120. LEFT = 2,
  121. ## Direction right.
  122. RIGHT = 3,
  123. }
  124. ## The description of a constant.
  125. const GRAVITY = 9.8
  126. ## The description of the variable v1.
  127. var v1
  128. ## This is a multiline description of the variable v2.[br]
  129. ## The type information below will be extracted for the documentation.
  130. var v2: int
  131. ## If the member has any annotation, the annotation should
  132. ## immediately precede it.
  133. @export
  134. var v3 := some_func()
  135. ## As the following function is documented, even though its name starts with
  136. ## an underscore, it will appear in the help window.
  137. func _fn(p1: int, p2: String) -> int:
  138. return 0
  139. # The below function isn't documented and its name starts with an underscore
  140. # so it will treated as private and will not be shown in the help window.
  141. func _internal() -> void:
  142. pass
  143. ## Documenting an inner class.
  144. ##
  145. ## The same rules apply here. The documentation must
  146. ## immediately precede the class definition.
  147. ##
  148. ## @tutorial: https://example.com/tutorial
  149. ## @experimental
  150. class Inner:
  151. ## Inner class variable v4.
  152. var v4
  153. ## Inner class function fn.
  154. func fn(): pass
  155. ``@deprecated`` and ``@experimental`` tags
  156. ------------------------------------------
  157. You can mark a class or any of its members as deprecated or experimental.
  158. This will add the corresponding indicator in the built-in documentation viewer.
  159. Optionally, you can provide a short message explaining why the API is not recommended.
  160. This can be especially useful for plugin and library creators.
  161. .. image:: img/deprecated_and_experimental_tags.webp
  162. - **Deprecated** marks a non-recommended API that is subject to removal or incompatible change
  163. in a future major release. Usually the API is kept for backwards compatibility.
  164. - **Experimental** marks a new unstable API that may be changed or removed in the current
  165. major branch. Using this API is not recommended in production code.
  166. .. note::
  167. While technically you can use both ``@deprecated`` and ``@experimental`` tags on the same
  168. class/member, this is not recommended as it is against common conventions.
  169. BBCode and class reference
  170. --------------------------
  171. Godot's class reference supports BBCode-like tags. They add nice formatting to the text which could also
  172. be used in the documentation. See also :ref:`class reference bbcode <doc_class_reference_bbcode>`.
  173. Note that this is slightly different from the ``RichTextLabel`` :ref:`BBCode <doc_bbcode_in_richtextlabel>`.
  174. Whenever you link to a member of another class, you need to specify the class name.
  175. For links to the same class, the class name is optional and can be omitted.
  176. Here's the list of available tags:
  177. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  178. | Tag and Description | Example | Result |
  179. +================================+==============================================+==============================================================+
  180. | | ``[Class]`` | ``Move the [Sprite2D].`` | Move the :ref:`class_Sprite2D`. |
  181. | | Link to class | | |
  182. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  183. | | ``[annotation Class.name]`` | ``See [annotation @GDScript.@rpc].`` | See :ref:`@GDScript.@rpc <class_@GDScript_annotation_@rpc>`. |
  184. | | Link to annotation | | |
  185. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  186. | | ``[constant Class.name]`` | ``See [constant Color.RED].`` | See :ref:`Color.RED <class_Color_constant_RED>`. |
  187. | | Link to constant | | |
  188. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  189. | | ``[enum Class.name]`` | ``See [enum Mesh.ArrayType].`` | See :ref:`Mesh.ArrayType <enum_Mesh_ArrayType>`. |
  190. | | Link to enum | | |
  191. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  192. | | ``[member Class.name]`` | ``Get [member Node2D.scale].`` | Get :ref:`Node2D.scale <class_Node2D_property_scale>`. |
  193. | | Link to member (property) | | |
  194. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  195. | | ``[method Class.name]`` | ``Call [method Node3D.hide].`` | Call :ref:`Node3D.hide() <class_Node3D_method_hide>`. |
  196. | | Link to method | | |
  197. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  198. | | ``[constructor Class.name]`` | ``Use [constructor Color.Color].`` | Use :ref:`Color.Color <class_Color_constructor_Color>`. |
  199. | | Link to built-in constructor | | |
  200. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  201. | | ``[operator Class.name]`` | ``Use [operator Color.operator *].`` | Use :ref:`Color.operator * <class_Color_operator_mul_int>`. |
  202. | | Link to built-in operator | | |
  203. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  204. | | ``[signal Class.name]`` | ``Emit [signal Node.renamed].`` | Emit :ref:`Node.renamed <class_Node_signal_renamed>`. |
  205. | | Link to signal | | |
  206. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  207. | | ``[theme_item Class.name]`` | ``See [theme_item Label.font].`` | See :ref:`Label.font <class_Label_theme_font_font>`. |
  208. | | Link to theme item | | |
  209. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  210. | | ``[param name]`` | ``Takes [param size] for the size.`` | Takes ``size`` for the size. |
  211. | | Parameter name (as code) | | |
  212. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  213. | | ``[br]`` | | ``Line 1.[br]`` | | Line 1. |
  214. | | Line break | | ``Line 2.`` | | Line 2. |
  215. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  216. | | ``[lb]`` ``[rb]`` | ``[lb]b[rb]text[lb]/b[rb]`` | [b]text[/b] |
  217. | | ``[`` and ``]`` respectively | | |
  218. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  219. | | ``[b]`` ``[/b]`` | ``Do [b]not[/b] call this method.`` | Do **not** call this method. |
  220. | | Bold | | |
  221. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  222. | | ``[i]`` ``[/i]`` | ``Returns the [i]global[/i] position.`` | Returns the *global* position. |
  223. | | Italic | | |
  224. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  225. | | ``[u]`` ``[/u]`` | ``[u]Always[/u] use this method.`` | .. raw:: html |
  226. | | Underline | | |
  227. | | | <u>Always</u> use this method. |
  228. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  229. | | ``[s]`` ``[/s]`` | ``[s]Outdated information.[/s]`` | .. raw:: html |
  230. | | Strikethrough | | |
  231. | | | <s>Outdated information.</s> |
  232. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  233. | | ``[color]`` ``[/color]`` | ``[color=red]Error![/color]`` | .. raw:: html |
  234. | | Color | | |
  235. | | | <span style="color:red;">Error!</span> |
  236. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  237. | | ``[font]`` ``[/font]`` | ``[font=res://mono.ttf]LICENSE[/font]`` | .. raw:: html |
  238. | | Font | | |
  239. | | | <span style="font-family:monospace;">LICENSE</span> |
  240. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  241. | | ``[img]`` ``[/img]`` | ``[img width=32]res://icon.svg[/img]`` | .. image:: img/icon.svg |
  242. | | Image | | :width: 32 px |
  243. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  244. | | ``[url]`` ``[/url]`` | | ``[url]https://example.com[/url]`` | | https://example.com |
  245. | | Hyperlink | | ``[url=https://example.com]Website[/url]`` | | `Website <https://example.com>`_ |
  246. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  247. | | ``[center]`` ``[/center]`` | ``[center]2 + 2 = 4[/center]`` | .. raw:: html |
  248. | | Horizontal centering | | |
  249. | | | <center>2 + 2 = 4</center> |
  250. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  251. | | ``[kbd]`` ``[/kbd]`` | ``Press [kbd]Ctrl + C[/kbd].`` | Press :kbd:`Ctrl + C`. |
  252. | | Keyboard/mouse shortcut | | |
  253. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  254. | | ``[code]`` ``[/code]`` | ``Returns [code]true[/code].`` | Returns ``true``. |
  255. | | Inline code fragment | | |
  256. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  257. | | ``[codeblock]`` | *See below.* | *See below.* |
  258. | | ``[/codeblock]`` | | |
  259. | | Multiline code block | | |
  260. +--------------------------------+----------------------------------------------+--------------------------------------------------------------+
  261. .. note::
  262. 1. Currently only :ref:`class_@GDScript` has annotations.
  263. 2. ``[kbd]`` disables BBCode until the parser encounters ``[/kbd]``.
  264. 3. ``[code]`` disables BBCode until the parser encounters ``[/code]``.
  265. 4. ``[codeblock]`` disables BBCode until the parser encounters ``[/codeblock]``.
  266. .. warning::
  267. Use ``[codeblock]`` for pre-formatted code blocks. Inside ``[codeblock]``,
  268. always use **four spaces** for indentation (the parser will delete tabs).
  269. ::
  270. ## Do something for this plugin. Before using the method
  271. ## you first have to [method initialize] [MyPlugin].[br]
  272. ## [color=yellow]Warning:[/color] Always [method clean] after use.[br]
  273. ## Usage:
  274. ## [codeblock]
  275. ## func _ready():
  276. ## the_plugin.initialize()
  277. ## the_plugin.do_something()
  278. ## the_plugin.clean()
  279. ## [/codeblock]
  280. func do_something():
  281. pass
  282. By default, ``[codeblock]`` highlights GDScript syntax. You can change it using
  283. the ``lang`` attribute. Currently supported options are:
  284. - ``[codeblock lang=text]`` disables syntax highlighting;
  285. - ``[codeblock lang=gdscript]`` highlights GDScript syntax;
  286. - ``[codeblock lang=csharp]`` highlights C# syntax (only in .NET version).