2
0

gdscript_documentation_comments.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. - Inner class
  60. - Constant
  61. - Function
  62. - Signal
  63. - Variable
  64. - Enum
  65. - Enum value
  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. enum MyEnum { ## My enum.
  86. VALUE_A = 0, ## Value A.
  87. VALUE_B = 1, ## Value B.
  88. }
  89. const MY_CONST = 1 ## My constant.
  90. var my_var ## My variable.
  91. signal my_signal ## My signal.
  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 constant.
  112. const GRAVITY = 9.8
  113. ## The description of a signal.
  114. signal my_signal
  115. ## This is a description of the below enum.
  116. enum Direction {
  117. ## Direction up.
  118. UP = 0,
  119. ## Direction down.
  120. DOWN = 1,
  121. ## Direction left.
  122. LEFT = 2,
  123. ## Direction right.
  124. RIGHT = 3,
  125. }
  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. The editor help window which renders the documentation supports :ref:`bbcode <doc_bbcode_in_richtextlabel>`.
  172. As a result it's possible to align and format the documentation. Color texts, images, fonts, tables,
  173. URLs, animation effects, etc. can be added with the :ref:`bbcode <doc_bbcode_in_richtextlabel>`.
  174. Godot's class reference supports BBCode-like tags. They add nice formatting to the text which could also
  175. be used in the documentation. See also :ref:`class reference bbcode <doc_class_reference_bbcode>`.
  176. Whenever you link to a member of another class, you need to specify the class name.
  177. For links to the same class, the class name is optional and can be omitted.
  178. Here's the list of available tags:
  179. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  180. | Tag and Description | Example | Result |
  181. +======================================+=========================================+======================================================================+
  182. | | ``[Class]`` | ``Move the [Sprite2D].`` | Move the :ref:`class_Sprite2D`. |
  183. | | Link to class | | |
  184. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  185. | | ``[annotation Class.name]`` | ``See [annotation @GDScript.@export].`` | See :ref:`@GDScript.@export <class_@GDScript_annotation_@export>`. |
  186. | | Link to annotation | | |
  187. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  188. | | ``[constant Class.name]`` | ``See [constant @GlobalScope.KEY_F1].`` | See :ref:`@GlobalScope.KEY_F1 <class_@GlobalScope_constant_KEY_F1>`. |
  189. | | Link to constant | | |
  190. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  191. | | ``[enum Class.name]`` | ``See [enum Mesh.ArrayType].`` | See :ref:`Mesh.ArrayType <enum_Mesh_ArrayType>`. |
  192. | | Link to enum | | |
  193. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  194. | | ``[method Class.name]`` | ``Call [method Node3D.hide].`` | Call :ref:`Node3D.hide() <class_Node3D_method_hide>`. |
  195. | | Link to method | | |
  196. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  197. | | ``[member Class.name]`` | ``Get [member Node2D.scale].`` | Get :ref:`Node2D.scale <class_Node2D_property_scale>`. |
  198. | | Link to member | | |
  199. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  200. | | ``[signal Class.name]`` | ``Emit [signal Node.renamed].`` | Emit :ref:`Node.renamed <class_Node_signal_renamed>`. |
  201. | | Link to signal | | |
  202. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  203. | | ``[theme_item Class.name]`` | ``See [theme_item Label.font].`` | See :ref:`Label.font <class_Label_theme_font_font>`. |
  204. | | Link to theme item | | |
  205. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  206. | | ``[param name]`` | ``Takes [param size] for the size.`` | Takes ``size`` for the size. |
  207. | | Formats a parameter name (as code) | | |
  208. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  209. | | ``[br]`` | | ``Line 1.[br]`` | | Line 1. |
  210. | | Line break | | ``Line 2.`` | | Line 2. |
  211. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  212. | | ``[b]`` ``[/b]`` | ``Some [b]bold[/b] text.`` | Some **bold** text. |
  213. | | Bold | | |
  214. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  215. | | ``[i]`` ``[/i]`` | ``Some [i]italic[/i] text.`` | Some *italic* text. |
  216. | | Italic | | |
  217. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  218. | | ``[kbd]`` ``[/kbd]`` | ``Some [kbd]Ctrl + C[/kbd] key.`` | Some :kbd:`Ctrl + C` key. |
  219. | | Keyboard/mouse shortcut | | |
  220. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  221. | | ``[code]`` ``[/code]`` | ``Some [code]monospace[/code] text.`` | Some ``monospace`` text. |
  222. | | Monospace | | |
  223. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  224. | | ``[codeblock]`` ``[/codeblock]`` | *See below.* | *See below.* |
  225. | | Multiline preformatted block | | |
  226. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  227. .. note::
  228. 1. Currently only :ref:`class_@GDScript` has annotations.
  229. 2. ``[code]`` disables BBCode until the parser encounters ``[/code]``.
  230. 3. ``[codeblock]`` disables BBCode until the parser encounters ``[/codeblock]``.
  231. .. warning::
  232. Use ``[codeblock]`` for pre-formatted code blocks. Inside ``[codeblock]``,
  233. always use **four spaces** for indentation (the parser will delete tabs).
  234. ::
  235. ## Do something for this plugin. Before using the method
  236. ## you first have to [method initialize] [MyPlugin].[br]
  237. ## [color=yellow]Warning:[/color] Always [method clean] after use.[br]
  238. ## Usage:
  239. ## [codeblock]
  240. ## func _ready():
  241. ## the_plugin.initialize()
  242. ## the_plugin.do_something()
  243. ## the_plugin.clean()
  244. ## [/codeblock]
  245. func do_something():
  246. pass