gdscript_documentation_comments.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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.
  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) and must
  20. have the format ``@``, followed by the keyword, and finishing with a colon.
  21. Tags
  22. ~~~~
  23. +-------------------+--------------------------------------------------------+
  24. | Brief description | No tag and lives at the very beginning of |
  25. | | the documentation section. |
  26. +-------------------+--------------------------------------------------------+
  27. | Description | Use one blank line to separate the description from |
  28. | | the brief. |
  29. +-------------------+--------------------------------------------------------+
  30. | Tutorial | | ``@tutorial:`` |
  31. | | | ``@tutorial(The Title Here):`` |
  32. +-------------------+--------------------------------------------------------+
  33. For example:
  34. ::
  35. extends Node2D
  36. ## A brief description of the class's role and functionality.
  37. ##
  38. ## The description of the script, what it can do,
  39. ## and any further detail.
  40. ##
  41. ## @tutorial: https://the/tutorial1/url.com
  42. ## @tutorial(Tutorial2): https://the/tutorial2/url.com
  43. .. warning::
  44. If there is any space in between the tag name and colon, for example
  45. ``@tutorial :``, it won't be treated as a valid tag and will be ignored.
  46. .. note::
  47. When the description spans multiple lines, the preceding and trailing white
  48. spaces will be stripped and joined with a single space. To preserve the line
  49. break use ``[br]``. See also `BBCode and class reference`_ below.
  50. Documenting script members
  51. --------------------------
  52. Documentation of a script member must immediately precede the member or its
  53. annotations if it has any. The exception to this is enum values whose description should
  54. be on the same line as the enum for readability.
  55. The description can have more than one line but every line must start
  56. with the double hash symbol ``##`` to be considered as part of the documentation.
  57. The script documentation will update in the editor help window every time the
  58. script is updated. If any member variable or function name starts with an
  59. underscore, it will be treated as private. It will not appear in the documentation and
  60. will be ignored in the help window.
  61. Members that are applicable for documentation:
  62. - Inner class
  63. - Constant
  64. - Function
  65. - Signal
  66. - Variable
  67. - Enum
  68. - Enum value
  69. Example
  70. -------
  71. ::
  72. extends Node2D
  73. ## A brief description of the class's role and functionality.
  74. ##
  75. ## The description of the script, what it can do,
  76. ## and any further detail.
  77. ##
  78. ## @tutorial: https://the/tutorial1/url.com
  79. ## @tutorial(Tutorial2): https://the/tutorial2/url.com
  80. ## The description of a constant.
  81. const GRAVITY = 9.8
  82. ## The description of a signal.
  83. signal my_signal
  84. ## This is a description of the below enums. Note below that
  85. ## the enum values are documented on the same line as the enum.
  86. enum Direction {
  87. UP = 0, ## Direction up.
  88. DOWN = 1, ## Direction down.
  89. LEFT = 2, ## Direction left.
  90. RIGHT = 3, ## Direction right.
  91. }
  92. ## The description of a constant.
  93. const GRAVITY = 9.8
  94. ## The description of the variable v1.
  95. var v1
  96. ## This is a multiline description of the variable v2.[br]
  97. ## The type information below will be extracted for the documentation.
  98. var v2: int
  99. ## If the member has any annotation, the annotation should
  100. ## immediately precede it.
  101. @export
  102. var v3 := some_func()
  103. ## As the following function is documented, even though its name starts with
  104. ## an underscore, it will appear in the help window.
  105. func _fn(p1: int, p2: String) -> int:
  106. return 0
  107. # The below function isn't documented and its name starts with an underscore
  108. # so it will treated as private and will not be shown in the help window.
  109. func _internal() -> void:
  110. pass
  111. ## Documenting an inner class.
  112. ##
  113. ## The same rules apply here. The documentation must
  114. ## immediately precede the class definition.
  115. ##
  116. ## @tutorial: https://the/tutorial/url.com
  117. class Inner:
  118. ## Inner class variable v4.
  119. var v4
  120. ## Inner class function fn.
  121. func fn(): pass
  122. BBCode and class reference
  123. --------------------------
  124. The editor help window which renders the documentation supports :ref:`bbcode <doc_bbcode_in_richtextlabel>`.
  125. As a result it's possible to align and format the documentation. Color texts, images, fonts, tables,
  126. URLs, animation effects, etc. can be added with the :ref:`bbcode <doc_bbcode_in_richtextlabel>`.
  127. Godot's class reference supports BBCode-like tags. They add nice formatting to the text which could also
  128. be used in the documentation. See also :ref:`class reference bbcode <doc_class_reference_bbcode>`.
  129. Whenever you link to a member of another class, you need to specify the class name.
  130. For links to the same class, the class name is optional and can be omitted.
  131. Here's the list of available tags:
  132. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  133. | Tag and Description | Example | Result |
  134. +======================================+=========================================+======================================================================+
  135. | | ``[Class]`` | ``Move the [Sprite2D].`` | Move the :ref:`class_Sprite2D`. |
  136. | | Link to class | | |
  137. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  138. | | ``[annotation Class.name]`` | ``See [annotation @GDScript.@export].`` | See :ref:`@GDScript.@export <class_@GDScript_annotation_@export>`. |
  139. | | Link to annotation | | |
  140. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  141. | | ``[constant Class.name]`` | ``See [constant @GlobalScope.KEY_F1].`` | See :ref:`@GlobalScope.KEY_F1 <class_@GlobalScope_constant_KEY_F1>`. |
  142. | | Link to constant | | |
  143. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  144. | | ``[enum Class.name]`` | ``See [enum Mesh.ArrayType].`` | See :ref:`Mesh.ArrayType <enum_Mesh_ArrayType>`. |
  145. | | Link to enum | | |
  146. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  147. | | ``[method Class.name]`` | ``Call [method Node3D.hide].`` | Call :ref:`Node3D.hide() <class_Node3D_method_hide>`. |
  148. | | Link to method | | |
  149. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  150. | | ``[member Class.name]`` | ``Get [member Node2D.scale].`` | Get :ref:`Node2D.scale <class_Node2D_property_scale>`. |
  151. | | Link to member | | |
  152. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  153. | | ``[signal Class.name]`` | ``Emit [signal Node.renamed].`` | Emit :ref:`Node.renamed <class_Node_signal_renamed>`. |
  154. | | Link to signal | | |
  155. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  156. | | ``[theme_item Class.name]`` | ``See [theme_item Label.font].`` | See :ref:`Label.font <class_Label_theme_font_font>`. |
  157. | | Link to theme item | | |
  158. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  159. | | ``[param name]`` | ``Takes [param size] for the size.`` | Takes ``size`` for the size. |
  160. | | Formats a parameter name (as code) | | |
  161. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  162. | | ``[br]`` | | ``Line 1.[br]`` | | Line 1. |
  163. | | Line break | | ``Line 2.`` | | Line 2. |
  164. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  165. | | ``[b]`` ``[/b]`` | ``Some [b]bold[/b] text.`` | Some **bold** text. |
  166. | | Bold | | |
  167. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  168. | | ``[i]`` ``[/i]`` | ``Some [i]italic[/i] text.`` | Some *italic* text. |
  169. | | Italic | | |
  170. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  171. | | ``[kbd]`` ``[/kbd]`` | ``Some [kbd]Ctrl + C[/kbd] key.`` | Some :kbd:`Ctrl + C` key. |
  172. | | Keyboard/mouse shortcut | | |
  173. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  174. | | ``[code]`` ``[/code]`` | ``Some [code]monospace[/code] text.`` | Some ``monospace`` text. |
  175. | | Monospace | | |
  176. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  177. | | ``[codeblock]`` ``[/codeblock]`` | *See below.* | *See below.* |
  178. | | Multiline preformatted block | | |
  179. +--------------------------------------+-----------------------------------------+----------------------------------------------------------------------+
  180. .. note::
  181. 1. Currently only :ref:`class_@GDScript` has annotations.
  182. 2. ``[code]`` disables BBCode until the parser encounters ``[/code]``.
  183. 3. ``[codeblock]`` disables BBCode until the parser encounters ``[/codeblock]``.
  184. .. warning::
  185. Use ``[codeblock]`` for pre-formatted code blocks. Inside ``[codeblock]``,
  186. always use **four spaces** for indentation (the parser will delete tabs).
  187. ::
  188. ## Do something for this plugin. Before using the method
  189. ## you first have to [method initialize] [MyPlugin].[br]
  190. ## [color=yellow]Warning:[/color] Always [method clean] after use.[br]
  191. ## Usage:
  192. ## [codeblock]
  193. ## func _ready():
  194. ## the_plugin.initialize()
  195. ## the_plugin.do_something()
  196. ## the_plugin.clean()
  197. ## [/codeblock]
  198. func do_something():
  199. pass