Class reference writing guidelines

This page explains how to write the class reference. You will learn where to write new descriptions for the classes, methods, and properties for Godot’s built-in node types.

参见

To learn to submit your changes to the Godot project using the Git version control system, see 为类参考手册贡献.

The reference for each class is contained in an XML file like the one below:

  1. <class name="Node2D" inherits="CanvasItem" version="4.0">
  2. <brief_description>
  3. A 2D game object, inherited by all 2D-related nodes. Has a position, rotation, scale, and Z index.
  4. </brief_description>
  5. <description>
  6. 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.
  7. </description>
  8. <tutorials>
  9. <link title="Custom drawing in 2D">https://docs.godotengine.org/en/latest/tutorials/2d/custom_drawing_in_2d.html</link>
  10. <link title="All 2D Demos">https://github.com/godotengine/godot-demo-projects/tree/master/2d</link>
  11. </tutorials>
  12. <methods>
  13. <method name="apply_scale">
  14. <return type="void">
  15. </return>
  16. <argument index="0" name="ratio" type="Vector2">
  17. </argument>
  18. <description>
  19. Multiplies the current scale by the [code]ratio[/code] vector.
  20. </description>
  21. </method>
  22. [...]
  23. <method name="translate">
  24. <return type="void">
  25. </return>
  26. <argument index="0" name="offset" type="Vector2">
  27. </argument>
  28. <description>
  29. Translates the node by the given [code]offset[/code] in local coordinates.
  30. </description>
  31. </method>
  32. </methods>
  33. <members>
  34. <member name="global_position" type="Vector2" setter="set_global_position" getter="get_global_position">
  35. Global position.
  36. </member>
  37. [...]
  38. <member name="z_index" type="int" setter="set_z_index" getter="get_z_index" default="0">
  39. Z index. Controls the order in which the nodes render. A node with a higher Z index will display in front of others.
  40. </member>
  41. </members>
  42. <constants>
  43. </constants>
  44. </class>

It starts with brief and long descriptions. In the generated docs, the brief description is always at the top of the page, while the long description lies below the list of methods, variables, and constants. You can find methods, member variables, constants, and signals in separate XML nodes.

For each, you want to learn how they work in Godot’s source code. Then, fill their documentation by completing or improving the text in these tags:

  • <brief_description>

  • <description>

  • <constant>

  • <method> (in its <description> tag; return types and arguments don’t take separate documentation strings)

  • <member>

  • <signal> (in its <description> tag; arguments don’t take separate documentation strings)

  • <constant>

Write in a clear and simple language. Always follow the writing guidelines to keep your descriptions short and easy to read. Do not leave empty lines in the descriptions: each line in the XML file will result in a new paragraph, even if it is empty.

如何编辑类XML

Edit the file for your chosen class in doc/classes/ to update the class reference. The folder contains an XML file for each class. The XML lists the constants and methods you will find in the class reference. Godot generates and updates the XML automatically.

注解

For some modules in the engine’s source code, you’ll find the XML files in the modules/<module_name>/doc_classes/ directory instead.

Edit it using your favorite text editor. If you use a code editor, make sure that it doesn’t change the indent style: you should use tabs for the XML and four spaces inside BBCode-style blocks. More on that below.

To check that the modifications you’ve made are correct in the generated documentation, navigate to the doc/ folder and run the command make rst. This will convert the XML files to the online documentation’s format and output errors if anything’s wrong.

Alternatively, you can build Godot and open the modified page in the built-in code reference. To learn how to compile the engine, read the compilation guide.

We recommend using a code editor that supports XML files like Vim, Atom, Visual Studio Code, Notepad++, or another to comfortably edit the file. You can also use their search feature to find classes and properties quickly.

改进BBCode风格标签的格式

Godot的类参考支持类似BBCode的标签. 它们为文本添加了漂亮的格式. 下面是可用标签的列表:

标签

效果

用法

结果

[Class]

链接到一个类

Move the [Sprite2D].

Move the Sprite.

[method methodname]

链接到此类中的方法

调用[method hide].

Call hide.

[method Class.methodname]

链接到另一个类的方法

Call [method Node3D.hide].

Call hide.

[member membername]

链接到这个类的成员

获得[member scale].

Get scale.

[member Class.membername]

链接到另一个类的成员

获得 [member Node2D.scale].

Get scale.

[signal signalname]

链接到此类中的信号

发射 [signal renamed].

Emit renamed.

[signal Class.signalname]

链接到另一个类的信号

发射 [signal Node.renamed].

Emit renamed.

[b] [/b]

粗体

一些[b]粗体[/b]文字.

一些 粗体 文字.

[i] [/i]

斜体

一些[i]斜体[/i]文字.

一些 斜体 文字.

[code] [/code]

等宽字体

一些[code]等宽字体[/code] 文本.

一些 等宽字体 文本.

[kbd] [/kbd]

键盘和鼠标快捷键

一些[kbd]Ctrl + C[/kbd]键.

一些 Ctrl + C 键.

[codeblock] [/codeblock]

多行预格式化块

见下文.

见下文.

[codeblocks] [/codeblocks]

[codeblock] for multiple languages

见下文.

见下文.

[gdscript] [/gdscript]

GDScript codeblock tab in codeblocks

见下文.

见下文.

[csharp] [/csharp]

C# codeblock tab in codeblocks

见下文.

见下文.

Use [codeblock] for pre-formatted code blocks. Inside [codeblock], always use four spaces for indentation. The parser will delete tabs. For example:

  1. [codeblock]
  2. func _ready():
  3. var sprite = get_node("Sprite2D")
  4. print(sprite.get_pos())
  5. [/codeblock]

将显示为:

  1. func _ready():
  2. var sprite = get_node("Sprite2D")
  3. print(sprite.get_pos())

If you need to have different code version in GDScript and C#, use [codeblocks] instead. If you use [codeblocks], you also need to have at least one of the language-specific tags, [gdscript] and [csharp].

Always write GDScript code examples first! You can use this experimental code translation tool to speed up your workflow.

  1. [codeblocks]
  2. [gdscript]
  3. func _ready():
  4. var sprite = get_node("Sprite2D")
  5. print(sprite.get_pos())
  6. [/gdscript]
  7. [csharp]
  8. public override void _Ready()
  9. {
  10. var sprite = GetNode("Sprite2D");
  11. GD.Print(sprite.GetPos());
  12. }
  13. [/csharp]
  14. [/codeblocks]

The above will display as:

GDScript

C#

  1. func _ready():
  2. var sprite = get_node("Sprite2D")
  3. print(sprite.get_pos())
  1. public override void _Ready()
  2. {
  3. var sprite = GetNode("Sprite2D");
  4. GD.Print(sprite.GetPos());
  5. }

要表示重要信息, 请在描述末尾添加一段以 “[b]注:[/b]“ 开头的内容:

  1. [b]Note:[/b] Only available when using the Vulkan renderer.

为了表示如果不仔细遵循可能导致安全问题或数据丢失的关键信息, 请在描述末尾添加一段以 “[b]警告:[/b]“ 开头的内容:

  1. [b]Warning:[/b] If this property is set to [code]true[/code], it allows clients to execute arbitrary code on the server.

对于不推荐使用的属性, 请添加以 “[i]deprecated.[/i]“ 开头的段落. 注意使用斜体代替粗体:

  1. [i]Deprecated.[/i] This property has been replaced by [member other_property].

在上面描述的所有段落中, 确保标点符号是BBCode标签的一部分, 以保持一致性.

我不知道这个方法干什么用!

没问题. 将其留下, 并在你请求提取更改时列出跳过的方法. 别的编写者会处理它.

You can still look at the methods’ implementation in Godot’s source code on GitHub. If you have doubts, feel free to ask on the Q&A website and Godot Contributors Chat.