gdextension_docs_system.rst 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. .. _doc_gdextension_docs_system:
  2. GDExtension documentation system
  3. ================================
  4. .. note::
  5. Adding documentation for GDExtensions is only possible for Godot 4.3 and later. The support can be integrated into your project
  6. regardless because the snippet will check if you use the appropriate godot-cpp version.
  7. If you set the ``compatability_minimum`` to 4.2 and you load a project with the extension through a 4.2 editor, the
  8. documentation page for that class will be empty. The extension itself will still work.
  9. The GDExtension documentation system works in a similar manner to the built-in engine documentation. It uses a series of
  10. XML files (one per class) to document the exposed constructors, properties, methods, constants, signals, and theme items of each class.
  11. .. note::
  12. We are assuming you are using the project files explained in the :ref:`GDExtension C++ Example <doc_gdextension_cpp_example>`
  13. with the following structure:
  14. .. code-block:: none
  15. gdextension_cpp_example/ # GDExtension directory
  16. |
  17. +--demo/ # game example/demo to test the extension
  18. | |
  19. | +--main.tscn
  20. | |
  21. | +--bin/
  22. | |
  23. | +--gdexample.gdextension
  24. |
  25. +--godot-cpp/ # C++ bindings
  26. |
  27. +--src/ # source code of the extension we are building
  28. | |
  29. | +--register_types.cpp
  30. | +--register_types.h
  31. | +--gdexample.cpp
  32. | +--gdexample.h
  33. Inside the Godot demo project directory of your GDExtension directory, run the following terminal command:
  34. .. code-block:: none
  35. # Replace "godot" with the full path to a Godot editor binary
  36. # if Godot is not installed in your `PATH`.
  37. godot --doctool ../ --gdextension-docs
  38. This command calls upon the Godot editor binary to generate documentation via the ``--doctool``
  39. and ``--gdextension-docs`` commands. The ``../`` addition is to let Godot know where the GDExtension
  40. SConstruct file is located. By calling this command, Godot generates a ``doc_classes`` directory inside the
  41. project directory in which it generates XML files for the GDExtension classes. Those files
  42. can then be edited to add information about member variables, methods, signals, and more.
  43. To add the now edited documentation to the GDExtension and let the editor load it,
  44. you need to add the following lines to your SConstruct file:
  45. .. code-block:: py
  46. if env["target"] in ["editor", "template_debug"]:
  47. try:
  48. doc_data = env.GodotCPPDocData("src/gen/doc_data.gen.cpp", source=Glob("doc_classes/*.xml"))
  49. sources.append(doc_data)
  50. except AttributeError:
  51. print("Not including class reference as we're targeting a pre-4.3 baseline.")
  52. The if-statement checks if we are compiling the GDExtension library with the ``editor`` and ``template_debug``
  53. flags. SCons then tries to load all the XML files inside the ``doc_classes`` directory and appends them
  54. to the ``sources`` variable which already includes all the source files of your extension. If it fails
  55. it means we are currently trying to compile the library when the ``godot_cpp`` is set to a version before 4.3.
  56. After loading the extension in a 4.3 Godot editor or later and open the documentation of your extension class
  57. either by :kbd:`Ctrl + Click` in the script editor or the Editor help dialog you will see something like this:
  58. .. image:: img/gdextension_docs_generation.webp
  59. Documentation styling
  60. ---------------------
  61. To style specific parts of text you can use BBCode tags similarly to how they can be used in :ref:`RichTextLabels <doc_bbcode_in_richtextlabel>`.
  62. You can set text as bold, italic, underlined, colored, codeblocks etc. by embedding them in tags like this:
  63. .. code-block:: none
  64. [b]this text will be shown as bold[/b]
  65. Currently they supported tags for the GDExtension documentation system are:
  66. .. list-table::
  67. :class: wrap-normal
  68. :width: 100%
  69. :widths: 60 40
  70. * - Tag
  71. - Example
  72. * - | **b**
  73. | Makes ``{text}`` use the bold (or bold italics) font of ``RichTextLabel``.
  74. - ``[b]{text}[/b]``
  75. * - | **i**
  76. | Makes ``{text}`` use the italics (or bold italics) font of ``RichTextLabel``.
  77. - ``[i]{text}[/i]``
  78. * - | **u**
  79. | Makes ``{text}`` underlined.
  80. - ``[u]{text}[/u]``
  81. * - | **s**
  82. | Makes ``{text}`` strikethrough.
  83. - ``[s]{text}[/s]``
  84. * - | **kbd**
  85. | Makes ``{text}`` use a grey beveled background, indicating a keyboard shortcut.
  86. - ``[kbd]{text}[/kbd]``
  87. * - | **code**
  88. | Makes inline ``{text}`` use the mono font and styles the text color and background like code.
  89. - ``[code]{text}[/code]``
  90. * - | **codeblocks**
  91. | Makes multiline ``{text}`` use the mono font and styles the text color and background like code.
  92. | The addition of the ``[gdscript]`` tag highlights the GDScript specific syntax.
  93. - | ``[codeblocks]``
  94. | ``[gdscript]``
  95. | ``{text}``
  96. | ``[/gdscript]``
  97. | ``[/codeblocks]``
  98. * - | **center**
  99. | Makes ``{text}`` horizontally centered.
  100. | Same as ``[p align=center]``.
  101. - ``[center]{text}[/center]``
  102. * - | **url**
  103. | Creates a hyperlink (underlined and clickable text). Can contain optional ``{text}`` or display ``{link}`` as is.
  104. - | ``[url]{link}[/url]``
  105. | ``[url={link}]{text}[/url]``
  106. * - | **img**
  107. | Inserts an image from the ``{path}`` (can be any valid :ref:`class_Texture2D` resource).
  108. | If ``{width}`` is provided, the image will try to fit that width maintaining
  109. the aspect ratio.
  110. | If both ``{width}`` and ``{height}`` are provided, the image will be scaled
  111. to that size.
  112. | Add ``%`` to the end of ``{width}`` or ``{height}`` value to specify it as percentages of the control width instead of pixels.
  113. | If ``{valign}`` configuration is provided, the image will try to align to the
  114. surrounding text, see :ref:`doc_bbcode_in_richtextlabel_image_and_table_alignment`.
  115. | Supports configuration options, see :ref:`doc_bbcode_in_richtextlabel_image_options`.
  116. - | ``[img]{path}[/img]``
  117. | ``[img={width}]{path}[/img]``
  118. | ``[img={width}x{height}]{path}[/img]``
  119. | ``[img={valign}]{path}[/img]``
  120. | ``[img {options}]{path}[/img]``
  121. * - | **color**
  122. | Changes the color of ``{text}``. Color must be provided by a common name (see
  123. :ref:`doc_bbcode_in_richtextlabel_named_colors`) or using the HEX format (e.g.
  124. ``#ff00ff``, see :ref:`doc_bbcode_in_richtextlabel_hex_colors`).
  125. - ``[color={code/name}]{text}[/color]``
  126. Publishing documentation online
  127. -------------------------------
  128. You may want to publish an online reference for your GDExtension, similar to this website.
  129. The most important step is to build reStructuredText (``.rst``) files from your XML class reference:
  130. .. code-block:: bash
  131. # You need a version.py file, so download it first.
  132. curl -sSLO https://raw.githubusercontent.com/godotengine/godot/refs/heads/master/version.py
  133. # Edit version.py according to your project before proceeding.
  134. # Then, run the rst generator. You'll need to have Python installed for this command to work.
  135. curl -sSL https://raw.githubusercontent.com/godotengine/godot/master/doc/tools/make_rst.py | python3 - -o "docs/classes" -l "en" doc_classes
  136. Your ``.rst`` files will now be available in ``docs/classes/``. From here, you can use
  137. any documentation builder that supports reStructuredText syntax to create a website from them.
  138. `godot-docs <https://github.com/godotengine/godot-docs>`_ uses `Sphinx <https://www.sphinx-doc.org/en/master/>`_. You can use the repository as a basis to build your own documentation system. The following guide describes the basic steps, but they are not exhaustive: You will need a bit of personal insight to make it work.
  139. 1. Add `godot-docs <https://github.com/godotengine/godot-docs>`_ as a submodule to your ``docs/`` folder.
  140. 2. Copy over its ``conf.py``, ``index.rst``, ``.readthedocs.yaml`` files into ``/docs/``. You may later decide to copy over and edit more of godot-docs' files, like ``_templates/layout.html``.
  141. 3. Modify these files according to your project. This mostly involves adjusting paths to point to the ``godot-docs`` subfolder, as well as strings to reflect it's your project rather than Godot you're building the docs for.
  142. 4. Create an account on `readthedocs.org <http://readthedocs.org>`_. Import your project, and modify its base ``.readthedocs.yaml`` file path to ``/docs/.readthedocs.yaml``.
  143. Once you have completed all these steps, your documentation should be available at ``<repo-name>.readthedocs.io``.