index.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. .. _doc_class_reference_primer:
  2. Class reference primer
  3. ======================
  4. This page explains how to write the class reference. You will learn where to
  5. write new descriptions for the classes, methods, and properties for Godot's
  6. built-in node types.
  7. .. seealso::
  8. To learn to submit your changes to the Godot project using the Git version
  9. control system, see `Class reference contribution documentation <https://contributing.godotengine.org/en/latest/documentation/class_reference.html>`__.
  10. The reference for each class is contained in an XML file like the one below:
  11. .. code-block:: xml
  12. <class name="Node2D" inherits="CanvasItem" version="4.0">
  13. <brief_description>
  14. A 2D game object, inherited by all 2D-related nodes. Has a position, rotation, scale, and Z index.
  15. </brief_description>
  16. <description>
  17. A 2D game object, with a transform (position, rotation, and scale). All 2D nodes, including physics objects and sprites, inherit from Node2D. Use Node2D as a parent node to move, scale and rotate children in a 2D project. Also gives control of the node's render order.
  18. </description>
  19. <tutorials>
  20. <link title="Custom drawing in 2D">https://docs.godotengine.org/en/latest/tutorials/2d/custom_drawing_in_2d.html</link>
  21. <link title="All 2D Demos">https://github.com/godotengine/godot-demo-projects/tree/master/2d</link>
  22. </tutorials>
  23. <methods>
  24. <method name="apply_scale">
  25. <return type="void">
  26. </return>
  27. <argument index="0" name="ratio" type="Vector2">
  28. </argument>
  29. <description>
  30. Multiplies the current scale by the [code]ratio[/code] vector.
  31. </description>
  32. </method>
  33. [...]
  34. <method name="translate">
  35. <return type="void">
  36. </return>
  37. <argument index="0" name="offset" type="Vector2">
  38. </argument>
  39. <description>
  40. Translates the node by the given [code]offset[/code] in local coordinates.
  41. </description>
  42. </method>
  43. </methods>
  44. <members>
  45. <member name="global_position" type="Vector2" setter="set_global_position" getter="get_global_position">
  46. Global position.
  47. </member>
  48. [...]
  49. <member name="z_index" type="int" setter="set_z_index" getter="get_z_index" default="0">
  50. Z index. Controls the order in which the nodes render. A node with a higher Z index will display in front of others.
  51. </member>
  52. </members>
  53. <constants>
  54. </constants>
  55. </class>
  56. It starts with brief and long descriptions. In the generated docs, the brief
  57. description is always at the top of the page, while the long description lies
  58. below the list of methods, variables, and constants. You can find methods,
  59. member variables, constants, and signals in separate XML nodes.
  60. For each, you want to learn how they work in Godot's source code. Then, fill
  61. their documentation by completing or improving the text in these tags:
  62. - `<brief_description>`
  63. - `<description>`
  64. - `<constant>`
  65. - `<method>` (in its `<description>` tag; return types and arguments don't take separate
  66. documentation strings)
  67. - `<member>`
  68. - `<signal>` (in its `<description>` tag; arguments don't take separate documentation strings)
  69. - `<constant>`
  70. Write in a clear and simple language. Always follow the `writing guidelines
  71. <https://contributing.godotengine.org/en/latest/documentation/guidelines/docs_writing_guidelines.html>`__
  72. to keep your descriptions short and easy to read.
  73. **Do not leave empty lines** in the descriptions: each line in the XML file will
  74. result in a new paragraph, even if it is empty.
  75. .. _doc_class_reference_editing_xml:
  76. How to edit class XML
  77. ---------------------
  78. Edit the file for your chosen class in ``doc/classes/`` to update the class
  79. reference. The folder contains an XML file for each class. The XML lists the
  80. constants and methods you will find in the class reference. Godot generates and
  81. updates the XML automatically.
  82. .. note:: For some modules in the engine's source code, you'll find the XML
  83. files in the ``modules/<module_name>/doc_classes/`` directory instead.
  84. Edit it using your favorite text editor. If you use a code editor, make sure
  85. that it doesn't change the indent style: you should use tabs for the XML and
  86. four spaces inside BBCode-style blocks. More on that below.
  87. To check that the modifications you've made are correct in the generated
  88. documentation, navigate to the ``doc/`` folder and run the command ``make rst``.
  89. This will convert the XML files to the online documentation's format and output
  90. errors if anything's wrong.
  91. Alternatively, you can build Godot and open the modified page in the built-in
  92. code reference. To learn how to compile the engine, read the :ref:`compilation
  93. guide <toc-devel-compiling>`.
  94. We recommend using a code editor that supports XML files like Vim, Atom, Visual Studio Code,
  95. Notepad++, or another to comfortably edit the file. You can also use their
  96. search feature to find classes and properties quickly.
  97. .. tip::
  98. If you use Visual Studio Code, you can install the
  99. `vscode-xml extension <https://marketplace.visualstudio.com/items?itemName=redhat.vscode-xml>`__
  100. to get linting for class reference XML files.
  101. .. _doc_class_reference_bbcode:
  102. Improve formatting with BBCode style tags
  103. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  104. Godot's XML class reference supports BBCode-like tags for linking as well as formatting text and code.
  105. In the tables below you can find the available tags, usage examples and the results after conversion to reStructuredText.
  106. Linking
  107. """""""
  108. Whenever you link to a member of another class, you need to specify the class name.
  109. For links to the same class, the class name is optional and can be omitted.
  110. +--------------------------------+-----------------------------------------+--------------------------------------------------------------+
  111. | Tag and Description | Example | Result |
  112. +================================+=========================================+==============================================================+
  113. | | ``[Class]`` | ``Move the [Sprite2D].`` | Move the :ref:`class_Sprite2D`. |
  114. | | Link to class | | |
  115. +--------------------------------+-----------------------------------------+--------------------------------------------------------------+
  116. | | ``[annotation Class.name]`` | ``See [annotation @GDScript.@rpc].`` | See :ref:`@GDScript.@rpc <class_@GDScript_annotation_@rpc>`. |
  117. | | Link to annotation | | |
  118. +--------------------------------+-----------------------------------------+--------------------------------------------------------------+
  119. | | ``[constant Class.name]`` | ``See [constant Color.RED].`` | See :ref:`Color.RED <class_Color_constant_RED>`. |
  120. | | Link to constant | | |
  121. +--------------------------------+-----------------------------------------+--------------------------------------------------------------+
  122. | | ``[enum Class.name]`` | ``See [enum Mesh.ArrayType].`` | See :ref:`Mesh.ArrayType <enum_Mesh_ArrayType>`. |
  123. | | Link to enum | | |
  124. +--------------------------------+-----------------------------------------+--------------------------------------------------------------+
  125. | | ``[member Class.name]`` | ``Get [member Node2D.scale].`` | Get :ref:`Node2D.scale <class_Node2D_property_scale>`. |
  126. | | Link to member | | |
  127. +--------------------------------+-----------------------------------------+--------------------------------------------------------------+
  128. | | ``[method Class.name]`` | ``Call [method Node3D.hide].`` | Call :ref:`Node3D.hide() <class_Node3D_method_hide>`. |
  129. | | Link to method | | |
  130. +--------------------------------+-----------------------------------------+--------------------------------------------------------------+
  131. | | ``[constructor Class.name]`` | ``Use [constructor Color.Color].`` | Use :ref:`Color.Color <class_Color_constructor_Color>`. |
  132. | | Link to built-in constructor | | |
  133. +--------------------------------+-----------------------------------------+--------------------------------------------------------------+
  134. | | ``[operator Class.name]`` | ``Use [operator Color.operator *].`` | Use :ref:`Color.operator * <class_Color_operator_mul_int>`. |
  135. | | Link to built-in operator | | |
  136. +--------------------------------+-----------------------------------------+--------------------------------------------------------------+
  137. | | ``[signal Class.name]`` | ``Emit [signal Node.renamed].`` | Emit :ref:`Node.renamed <class_Node_signal_renamed>`. |
  138. | | Link to signal | | |
  139. +--------------------------------+-----------------------------------------+--------------------------------------------------------------+
  140. | | ``[theme_item Class.name]`` | ``See [theme_item Label.font].`` | See :ref:`Label.font <class_Label_theme_font_font>`. |
  141. | | Link to theme item | | |
  142. +--------------------------------+-----------------------------------------+--------------------------------------------------------------+
  143. | | ``[param name]`` | ``Takes [param size] for the size.`` | Takes ``size`` for the size. |
  144. | | Parameter name (as code) | | |
  145. +--------------------------------+-----------------------------------------+--------------------------------------------------------------+
  146. .. note::
  147. Currently only :ref:`class_@GDScript` has annotations.
  148. Formatting text
  149. """""""""""""""
  150. +--------------------------------+----------------------------------------------+------------------------------------+
  151. | Tag and Description | Example | Result |
  152. +================================+==============================================+====================================+
  153. | | ``[br]`` | | ``Line 1.[br]`` | | Line 1. |
  154. | | Line break | | ``Line 2.`` | | Line 2. |
  155. +--------------------------------+----------------------------------------------+------------------------------------+
  156. | | ``[lb]`` ``[rb]`` | ``[lb]b[rb]text[lb]/b[rb]`` | [b]text[/b] |
  157. | | ``[`` and ``]`` respectively | | |
  158. +--------------------------------+----------------------------------------------+------------------------------------+
  159. | | ``[b]`` ``[/b]`` | ``Do [b]not[/b] call this method.`` | Do **not** call this method. |
  160. | | Bold | | |
  161. +--------------------------------+----------------------------------------------+------------------------------------+
  162. | | ``[i]`` ``[/i]`` | ``Returns the [i]global[/i] position.`` | Returns the *global* position. |
  163. | | Italic | | |
  164. +--------------------------------+----------------------------------------------+------------------------------------+
  165. | | ``[u]`` ``[/u]`` | ``[u]Always[/u] use this method.`` | .. raw:: html |
  166. | | Underline | | |
  167. | | | <u>Always</u> use this method. |
  168. +--------------------------------+----------------------------------------------+------------------------------------+
  169. | | ``[s]`` ``[/s]`` | ``[s]Outdated information.[/s]`` | .. raw:: html |
  170. | | Strikethrough | | |
  171. | | | <s>Outdated information.</s> |
  172. +--------------------------------+----------------------------------------------+------------------------------------+
  173. | | ``[url]`` ``[/url]`` | | ``[url]https://example.com[/url]`` | | https://example.com |
  174. | | Hyperlink | | ``[url=https://example.com]Website[/url]`` | | `Website <https://example.com>`_ |
  175. +--------------------------------+----------------------------------------------+------------------------------------+
  176. | | ``[center]`` ``[/center]`` | ``[center]2 + 2 = 4[/center]`` | .. raw:: html |
  177. | | Horizontal centering | | |
  178. | | | <center>2 + 2 = 4</center> |
  179. +--------------------------------+----------------------------------------------+------------------------------------+
  180. | | ``[kbd]`` ``[/kbd]`` | ``Press [kbd]Ctrl + C[/kbd].`` | Press :kbd:`Ctrl + C`. |
  181. | | Keyboard/mouse shortcut | | |
  182. +--------------------------------+----------------------------------------------+------------------------------------+
  183. | | ``[code]`` ``[/code]`` | ``Returns [code]true[/code].`` | Returns ``true``. |
  184. | | Inline code fragment | | |
  185. +--------------------------------+----------------------------------------------+------------------------------------+
  186. .. note::
  187. 1. Some supported tags like ``[color]`` and ``[font]`` are not listed here because they are not recommended in the engine documentation.
  188. 2. ``[kbd]`` disables BBCode until the parser encounters ``[/kbd]``.
  189. 3. ``[code]`` disables BBCode until the parser encounters ``[/code]``.
  190. Formatting code blocks
  191. """"""""""""""""""""""
  192. There are two options for formatting code blocks:
  193. 1. Use ``[codeblock]`` if you want to add an example for a specific language.
  194. 2. Use ``[codeblocks]``, ``[gdscript]``, and ``[csharp]`` if you want to add the same example for both languages, GDScript and C#.
  195. By default, ``[codeblock]`` highlights GDScript syntax. You can change it using
  196. the ``lang`` attribute. Currently supported options are:
  197. - ``[codeblock lang=text]`` disables syntax highlighting;
  198. - ``[codeblock lang=gdscript]`` highlights GDScript syntax;
  199. - ``[codeblock lang=csharp]`` highlights C# syntax (only in .NET version).
  200. .. note::
  201. ``[codeblock]`` disables BBCode until the parser encounters ``[/codeblock]``.
  202. .. warning::
  203. Use ``[codeblock]`` for pre-formatted code blocks. Since Godot 4.5,
  204. **tabs** should be used for indentation.
  205. For example:
  206. .. code-block:: none
  207. [codeblock]
  208. func _ready():
  209. var sprite = get_node("Sprite2D")
  210. print(sprite.get_pos())
  211. [/codeblock]
  212. Will display as:
  213. .. code-block:: gdscript
  214. func _ready():
  215. var sprite = get_node("Sprite2D")
  216. print(sprite.get_pos())
  217. If you need to have different code version in GDScript and C#, use
  218. ``[codeblocks]`` instead. If you use ``[codeblocks]``, you also need to have at
  219. least one of the language-specific tags, ``[gdscript]`` and ``[csharp]``.
  220. Always write GDScript code examples first! You can use this `experimental code
  221. translation tool <https://github.com/HaSa1002/codetranslator>`_ to speed up your
  222. workflow.
  223. .. code-block:: none
  224. [codeblocks]
  225. [gdscript]
  226. func _ready():
  227. var sprite = get_node("Sprite2D")
  228. print(sprite.get_pos())
  229. [/gdscript]
  230. [csharp]
  231. public override void _Ready()
  232. {
  233. var sprite = GetNode("Sprite2D");
  234. GD.Print(sprite.GetPos());
  235. }
  236. [/csharp]
  237. [/codeblocks]
  238. The above will display as:
  239. .. tabs::
  240. .. code-tab:: gdscript GDScript
  241. func _ready():
  242. var sprite = get_node("Sprite2D")
  243. print(sprite.get_pos())
  244. .. code-tab:: csharp
  245. public override void _Ready()
  246. {
  247. var sprite = GetNode("Sprite2D");
  248. GD.Print(sprite.GetPos());
  249. }
  250. Formatting notes and warnings
  251. """""""""""""""""""""""""""""
  252. To denote important information, add a paragraph starting with "[b]Note:[/b]" at
  253. the end of the description:
  254. .. code-block:: none
  255. [b]Note:[/b] Only available when using the Vulkan renderer.
  256. To denote crucial information that could cause security issues or loss of data
  257. if not followed carefully, add a paragraph starting with "[b]Warning:[/b]" at
  258. the end of the description:
  259. .. code-block:: none
  260. [b]Warning:[/b] If this property is set to [code]true[/code], it allows clients to execute arbitrary code on the server.
  261. In all the paragraphs described above, make sure the punctuation is part of the
  262. BBCode tags for consistency.
  263. Marking API as deprecated/experimental
  264. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  265. To mark an API as deprecated or experimental, you need to add the corresponding XML attribute. The attribute value must be a message
  266. explaining why the API is not recommended (BBCode markup is supported) or an empty string (the default message will be used).
  267. If an API element is marked as deprecated/experimental, then it is considered documented even if the description is empty.
  268. .. code-block:: xml
  269. <class name="Parallax2D" inherits="Node2D" experimental="This node is meant to replace [ParallaxBackground] and [ParallaxLayer]. The implementation may change in the future." xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
  270. [...]
  271. </class>
  272. <constant name="RESPONSE_USE_PROXY" value="305" enum="ResponseCode" deprecated="Many clients ignore this response code for security reasons. It is also deprecated by the HTTP standard.">
  273. HTTP status code [code]305 Use Proxy[/code].
  274. </constant>
  275. <member name="auto_translate" type="bool" setter="set_auto_translate" getter="is_auto_translating" deprecated="Use [member Node.auto_translate_mode] instead.">
  276. Toggles if any text should automatically change to its translated version depending on the current locale.
  277. </member>
  278. <method name="get_method_call_mode" qualifiers="const" deprecated="Use [member AnimationMixer.callback_mode_method] instead.">
  279. <return type="int" enum="AnimationPlayer.AnimationMethodCallMode" />
  280. <description>
  281. Returns the call mode used for "Call Method" tracks.
  282. </description>
  283. </method>