gdscript_documentation_comments.rst 10 KB

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