gdscript_documentation_comments.rst 15 KB

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