gdscript_documentation_comments.rst 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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:`` |
  31. | | | ``@tutorial(The Title Here):`` |
  32. +-------------------+--------------------------------------------------------+
  33. | Deprecated | ``@deprecated`` |
  34. +-------------------+--------------------------------------------------------+
  35. | Experimental | ``@experimental`` |
  36. +-------------------+--------------------------------------------------------+
  37. For example::
  38. extends Node2D
  39. ## A brief description of the class's role and functionality.
  40. ##
  41. ## The description of the script, what it can do,
  42. ## and any further detail.
  43. ##
  44. ## @tutorial: https://the/tutorial1/url.com
  45. ## @tutorial(Tutorial2): https://the/tutorial2/url.com
  46. ## @experimental
  47. .. warning::
  48. If there is any space in between the tag name and colon, for example
  49. ``@tutorial :``, it won't be treated as a valid tag and will be ignored.
  50. .. note::
  51. When the description spans multiple lines, the preceding and trailing white
  52. spaces will be stripped and joined with a single space. To preserve the line
  53. break use ``[br]``. See also `BBCode and class reference`_ below.
  54. Documenting script members
  55. --------------------------
  56. Members that are applicable for documentation:
  57. - Inner class
  58. - Constant
  59. - Function
  60. - Signal
  61. - Variable
  62. - Enum
  63. - Enum value
  64. Documentation of a script member must immediately precede the member or its annotations
  65. if it has any. The description can have more than one line but every line must start with
  66. the double hash symbol ``##`` to be considered as part of the documentation.
  67. Tags
  68. ~~~~
  69. +--------------+-------------------+
  70. | Description | No tag. |
  71. +--------------+-------------------+
  72. | Deprecated | ``@deprecated`` |
  73. +--------------+-------------------+
  74. | Experimental | ``@experimental`` |
  75. +--------------+-------------------+
  76. For example::
  77. ## The description of the variable.
  78. ## @deprecated
  79. var my_var
  80. Alternatively, you can use inline documentation comments::
  81. enum MyEnum { ## My enum.
  82. VALUE_A = 0, ## Value A.
  83. VALUE_B = 1, ## Value B.
  84. }
  85. const MY_CONST = 1 ## My constant.
  86. var my_var ## My variable.
  87. signal my_signal ## My signal.
  88. func my_func(): ## My func.
  89. pass
  90. class MyClass: ## My class.
  91. pass
  92. The script documentation will update in the editor help window every time the script is updated.
  93. If any member variable or function name starts with an underscore, it will be treated as private.
  94. It will not appear in the documentation and will be ignored in the help window.
  95. Complete script example
  96. -----------------------
  97. ::
  98. extends Node2D
  99. ## A brief description of the class's role and functionality.
  100. ##
  101. ## The description of the script, what it can do,
  102. ## and any further detail.
  103. ##
  104. ## @tutorial: https://the/tutorial1/url.com
  105. ## @tutorial(Tutorial2): https://the/tutorial2/url.com
  106. ## @experimental
  107. ## The description of a constant.
  108. const GRAVITY = 9.8
  109. ## The description of a signal.
  110. signal my_signal
  111. ## This is a description of the below enum.
  112. enum Direction {
  113. ## Direction up.
  114. UP = 0,
  115. ## Direction down.
  116. DOWN = 1,
  117. ## Direction left.
  118. LEFT = 2,
  119. ## Direction right.
  120. RIGHT = 3,
  121. }
  122. ## The description of the variable v1.
  123. var v1
  124. ## This is a multiline description of the variable v2.[br]
  125. ## The type information below will be extracted for the documentation.
  126. var v2: int
  127. ## If the member has any annotation, the annotation should
  128. ## immediately precede it.
  129. @export
  130. var v3 := some_func()
  131. ## As the following function is documented, even though its name starts with
  132. ## an underscore, it will appear in the help window.
  133. func _fn(p1: int, p2: String) -> int:
  134. return 0
  135. # The below function isn't documented and its name starts with an underscore
  136. # so it will treated as private and will not be shown in the help window.
  137. func _internal() -> void:
  138. pass
  139. ## Documenting an inner class.
  140. ##
  141. ## The same rules apply here. The documentation must
  142. ## immediately precede the class definition.
  143. ##
  144. ## @tutorial: https://the/tutorial/url.com
  145. ## @experimental
  146. class Inner:
  147. ## Inner class variable v4.
  148. var v4
  149. ## Inner class function fn.
  150. func fn(): pass
  151. ``@deprecated`` and ``@experimental`` tags
  152. ------------------------------------------
  153. You can mark a class or any of its members as deprecated or experimental.
  154. This will add the corresponding indicator in the built-in documentation viewer.
  155. This can be especially useful for plugin and library creators.
  156. .. image:: img/deprecated_and_experimental_marks.webp
  157. - **Deprecated** marks a non-recommended API that is subject to removal or incompatible change
  158. in a future major release. Usually the API is kept for backwards compatibility.
  159. - **Experimental** marks a new unstable API that may be changed or removed in the current
  160. major branch. Using this API is not recommended in production code.
  161. .. note::
  162. While technically you can use both ``@deprecated`` and ``@experimental`` tags on the same
  163. class/member, this is not recommended as it is against common conventions.
  164. BBCode and class reference
  165. --------------------------
  166. The editor help window which renders the documentation supports :ref:`bbcode <doc_bbcode_in_richtextlabel>`.
  167. As a result it's possible to align and format the documentation. Color texts, images, fonts, tables,
  168. URLs, animation effects, etc. can be added with the :ref:`bbcode <doc_bbcode_in_richtextlabel>`.
  169. Godot's class reference supports BBCode-like tags. They add nice formatting to the text which could also
  170. be used in the documentation. See also :ref:`class reference bbcode <doc_class_reference_bbcode>`.
  171. Whenever you link to a member of another class, you need to specify the class name.
  172. For links to the same class, the class name is optional and can be omitted.
  173. Here's the list of available tags:
  174. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  175. | Tag and Description | Example | Result |
  176. +======================================+=========================================+======================================================================+
  177. | | ``[Class]`` | ``Move the [Sprite2D].`` | Move the :ref:`class_Sprite2D`. |
  178. | | Link to class | | |
  179. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  180. | | ``[annotation Class.name]`` | ``See [annotation @GDScript.@export].`` | See :ref:`@GDScript.@export <class_@GDScript_annotation_@export>`. |
  181. | | Link to annotation | | |
  182. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  183. | | ``[constant Class.name]`` | ``See [constant @GlobalScope.KEY_F1].`` | See :ref:`@GlobalScope.KEY_F1 <class_@GlobalScope_constant_KEY_F1>`. |
  184. | | Link to constant | | |
  185. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  186. | | ``[enum Class.name]`` | ``See [enum Mesh.ArrayType].`` | See :ref:`Mesh.ArrayType <enum_Mesh_ArrayType>`. |
  187. | | Link to enum | | |
  188. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  189. | | ``[method Class.name]`` | ``Call [method Node3D.hide].`` | Call :ref:`Node3D.hide() <class_Node3D_method_hide>`. |
  190. | | Link to method | | |
  191. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  192. | | ``[member Class.name]`` | ``Get [member Node2D.scale].`` | Get :ref:`Node2D.scale <class_Node2D_property_scale>`. |
  193. | | Link to member | | |
  194. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  195. | | ``[signal Class.name]`` | ``Emit [signal Node.renamed].`` | Emit :ref:`Node.renamed <class_Node_signal_renamed>`. |
  196. | | Link to signal | | |
  197. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  198. | | ``[theme_item Class.name]`` | ``See [theme_item Label.font].`` | See :ref:`Label.font <class_Label_theme_font_font>`. |
  199. | | Link to theme item | | |
  200. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  201. | | ``[param name]`` | ``Takes [param size] for the size.`` | Takes ``size`` for the size. |
  202. | | Formats a parameter name (as code) | | |
  203. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  204. | | ``[br]`` | | ``Line 1.[br]`` | | Line 1. |
  205. | | Line break | | ``Line 2.`` | | Line 2. |
  206. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  207. | | ``[b]`` ``[/b]`` | ``Some [b]bold[/b] text.`` | Some **bold** text. |
  208. | | Bold | | |
  209. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  210. | | ``[i]`` ``[/i]`` | ``Some [i]italic[/i] text.`` | Some *italic* text. |
  211. | | Italic | | |
  212. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  213. | | ``[kbd]`` ``[/kbd]`` | ``Some [kbd]Ctrl + C[/kbd] key.`` | Some :kbd:`Ctrl + C` key. |
  214. | | Keyboard/mouse shortcut | | |
  215. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  216. | | ``[code]`` ``[/code]`` | ``Some [code]monospace[/code] text.`` | Some ``monospace`` text. |
  217. | | Monospace | | |
  218. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  219. | | ``[codeblock]`` ``[/codeblock]`` | *See below.* | *See below.* |
  220. | | Multiline preformatted block | | |
  221. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  222. .. note::
  223. 1. Currently only :ref:`class_@GDScript` has annotations.
  224. 2. ``[code]`` disables BBCode until the parser encounters ``[/code]``.
  225. 3. ``[codeblock]`` disables BBCode until the parser encounters ``[/codeblock]``.
  226. .. warning::
  227. Use ``[codeblock]`` for pre-formatted code blocks. Inside ``[codeblock]``,
  228. always use **four spaces** for indentation (the parser will delete tabs).
  229. ::
  230. ## Do something for this plugin. Before using the method
  231. ## you first have to [method initialize] [MyPlugin].[br]
  232. ## [color=yellow]Warning:[/color] Always [method clean] after use.[br]
  233. ## Usage:
  234. ## [codeblock]
  235. ## func _ready():
  236. ## the_plugin.initialize()
  237. ## the_plugin.do_something()
  238. ## the_plugin.clean()
  239. ## [/codeblock]
  240. func do_something():
  241. pass