2
0

gdextension_docs_system.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. .. _doc_godot_cpp_docs_system:
  2. Adding documentation
  3. ====================
  4. .. note::
  5. Adding documentation for GDExtensions is only possible with Godot 4.3 and later.
  6. The GDExtension documentation system works in a similar manner to the built-in engine documentation: It uses
  7. :ref:`XML files <doc_class_reference_primer>` (one per class) to document the exposed constructors, properties, methods,
  8. constants, signals, and more.
  9. To get started, identify your project's test project folder, which should contain a Godot project with your extension
  10. installed and working. If you are using `godot-cpp-template <https://github.com/godotengine/godot-cpp-template>`__, your
  11. GDExtension project already has a ``project`` folder. Alternatively, you can add one by following the steps
  12. described in :ref:`doc_godot_cpp_getting_started`.
  13. Inside the ``project`` folder, run the following terminal command:
  14. .. code-block:: shell
  15. # Replace "godot" with the full path to a Godot editor binary
  16. # if Godot is not installed in your `PATH`.
  17. godot --doctool ../ --gdextension-docs
  18. This command instructs Godot to generate documentation via the ``--doctool`` and ``--gdextension-docs`` commands.
  19. The ``../`` argument specifies the base path of your GDExtension.
  20. After running this command, you should find XML files for your registered GDExtension classes inside the ``doc_classes``
  21. folder in your GDExtension project. You could edit them now, but for this tutorial, the empty files will suffice.
  22. Now that you have XML files containing your documentation, the next step is to include them in your GDExtension binary.
  23. Assuming you are using SCons as your build system, you can add the following lines to your ``SConstruct`` file. If you
  24. are using `godot-cpp-template <https://github.com/godotengine/godot-cpp-template>`__, your file already contains code
  25. for this.
  26. .. code-block:: py
  27. if env["target"] in ["editor", "template_debug"]:
  28. doc_data = env.GodotCPPDocData("src/gen/doc_data.gen.cpp", source=Glob("doc_classes/*.xml"))
  29. sources.append(doc_data)
  30. The ``if`` statement avoids adding the documentation to release builds of your GDExtension, where it is not needed.
  31. SCons then loads all the XML files inside the ``doc_classes`` directory, and appends the resulting targets
  32. to the ``sources`` array, to be included in your GDExtension build.
  33. After building, launch your Godot project again. You can open the documentation of one of your extension
  34. classes either using :kbd:`Ctrl + Click` on a class name in the script editor, or inside by finding it in the Editor
  35. help dialog. If everything went well, you should see something like this:
  36. .. image:: img/gdextension_docs_generation.webp
  37. Writing and styling documentation
  38. ---------------------------------
  39. The format of the class reference XML files is the same as the one used by Godot. Is is documented in
  40. :ref:`doc_class_reference_primer`.
  41. If you are looking for pointers to write high quality documentation, feel free to refer to Godot's
  42. `documentation guidelines <https://contributing.godotengine.org/en/latest/documentation/guidelines/index.html>`__.
  43. Publishing documentation online
  44. -------------------------------
  45. You may want to publish an online reference for your GDExtension, similar to this website.
  46. The most important step is to build reStructuredText (``.rst``) files from your XML class reference:
  47. .. code-block:: shell
  48. # You need a version.py file, so download it first.
  49. curl -sSLO https://raw.githubusercontent.com/godotengine/godot/refs/heads/master/version.py
  50. # Edit version.py according to your project before proceeding.
  51. # Then, run the rst generator. You'll need to have Python installed for this command to work.
  52. curl -sSL https://raw.githubusercontent.com/godotengine/godot/master/doc/tools/make_rst.py | python3 - -o "docs/classes" -l "en" doc_classes
  53. Your ``.rst`` files will now be available in ``docs/classes/``. From here, you can use
  54. any documentation builder that supports reStructuredText syntax to create a website from them.
  55. `godot-docs <https://github.com/godotengine/godot-docs>`_ uses `Sphinx <https://www.sphinx-doc.org/en/master/>`_.
  56. You can use the repository as a basis to build your own documentation system.
  57. The following guide describes the basic steps, but they are not exhaustive:
  58. you will need a bit of personal insight to make it work.
  59. 1. Add `godot-docs <https://github.com/godotengine/godot-docs>`_ as a submodule to your ``docs/`` folder.
  60. 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``.
  61. 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.
  62. 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``.
  63. Once you have completed all these steps, your documentation should be available at ``<repo-name>.readthedocs.io``.