creating_script_templates.rst 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. .. _doc_creating_script_templates:
  2. Creating script templates
  3. =========================
  4. Godot provides a way to use script templates as seen in the
  5. ``Script Create Dialog`` while creating a new script:
  6. .. image:: img/script_create_dialog_templates.webp
  7. A set of built-in script templates are provided with the editor, but it is
  8. also possible to create new ones and set them by default, both per project
  9. and at editor scope.
  10. Templates are linked to a specific node type, so when you create a script
  11. you will only see the templates corresponding to that particular node, or
  12. one of its parent types.
  13. For example, if you are creating a script for a CharacterBody3D, you will
  14. only see templates defined for CharacterBody3Ds, Node3Ds or Nodes.
  15. Locating the templates
  16. ----------------------
  17. There are two places where templates can be managed.
  18. Editor-defined templates
  19. ~~~~~~~~~~~~~~~~~~~~~~~~
  20. These are available globally throughout any project. The location of these
  21. templates are determined per each OS:
  22. - Windows: ``%APPDATA%\Godot\script_templates\``
  23. - Linux: ``$HOME/.config/godot/script_templates/``
  24. - macOS: ``$HOME/Library/Application Support/Godot/script_templates/``
  25. If you're getting Godot from somewhere other than the official website, such
  26. as Steam, the folder might be in a different location. You can find it using
  27. the Godot editor. Go to ``Editor > Open Editor Data/Settings Folder`` and it
  28. will open a folder in your file browser, inside that folder is the
  29. ``script_templates`` folder.
  30. Project-defined templates
  31. ~~~~~~~~~~~~~~~~~~~~~~~~~
  32. The default path to search for templates is the
  33. ``res://script_templates/`` directory. The path can be changed by configuring
  34. the ``editor/script_templates_search_path`` setting in the
  35. :ref:`ProjectSettings <class_ProjectSettings>`, both via code and the editor.
  36. If no ``script_templates`` directory is found within a project, it is simply
  37. ignored.
  38. Template organization and naming
  39. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  40. Both editor and project defined templates are organized in the following way:
  41. ::
  42. template_path/node_type/file.extension
  43. where:
  44. * ``template_path`` is one of the 2 locations discussed in the previous two sections
  45. * ``node_type`` is the node it will apply to (for example, :ref:`Node <class_Node>`, or :ref:`CharacterBody3D <class_CharacterBody3D>`),
  46. casing doesn't matter for the folder name, however adding a ``_`` to a name will not work, for example ``Mesh_Instance3D`` does not work.
  47. if a script isn't in the proper ``node_type`` folder it will not be detected.
  48. * ``file`` is the custom name you can chose for the template (for example: ``platformer_movement`` or ``smooth_camera``)
  49. * ``extension``: will indicate which language the template will apply to (it should be ``gd`` for GDScript or ``cs`` for C#)
  50. For example:
  51. - ``template_scripts/Node/smooth_camera.gd``
  52. - ``template_scripts/CharacterBody3D/platformer_movement.gd``
  53. Default behaviour and overriding it
  54. -----------------------------------
  55. By default:
  56. * the template's name is the same as the file name (minus the extension, prettyfied)
  57. * the description is empty
  58. * the space indent is set to 4
  59. * the template will not be set as the default for the given node
  60. It is possible to customize this behaviour by adding meta headers at the start
  61. of your file, like this:
  62. .. tabs::
  63. .. code-tab:: gdscript GDScript
  64. # meta-name: Platformer movement
  65. # meta-description: Predefined movement for classical platformers
  66. # meta-default: true
  67. # meta-space-indent: 4
  68. .. code-tab:: csharp
  69. // meta-name: Platformer movement
  70. // meta-description: Predefined movement for classical platformers
  71. // meta-default: true
  72. // meta-space-indent: 4
  73. In this case, the name will be set to "Platformer movement", with the given custom description, and
  74. it will be set as the default template for the node in which directory it has been saved.
  75. This is an example of utilizing custom templates at editor and project level:
  76. .. image:: img/script_create_dialog_custom_templates.webp
  77. .. note:: The script templates have the same extension as the regular script
  78. files. This may lead to an issue of a script parser treating those templates as
  79. actual scripts within a project. To avoid this, make sure to ignore the
  80. directory containing them by creating an empty ``.gdignore`` file. The directory won't be
  81. visible throughout the project's filesystem anymore, yet the templates can be
  82. modified by an external text editor anytime.
  83. .. tip::
  84. By default, every C# file inside the project directory is included in the compilation.
  85. Script templates must be manually excluded from the C# project to avoid build errors.
  86. See `Exclude files from the build <https://learn.microsoft.com/en-us/visualstudio/msbuild/how-to-exclude-files-from-the-build>`_ in the Microsoft documentation.
  87. It is possible to create editor-level templates that have the same level as a project-specific
  88. templates, and also that have the same name as a built-in one, all will be shown on the new script
  89. dialog.
  90. Default template
  91. ----------------
  92. To override the default template, create a custom template at editor or project level inside a
  93. ``Node`` directory (or a more specific type, if only a subtype wants to be overridden) and start
  94. the file with the ``meta-default: true`` header.
  95. Only one template can be set as default at the same time for the same node type.
  96. The ``Default`` templates for basic Nodes, for both GDScript and C#, are shown here so you can
  97. use these as the base for creating other templates:
  98. .. tabs::
  99. .. code-tab:: gdscript GDScript
  100. # meta-description: Base template for Node with default Godot cycle methods
  101. extends _BASE_
  102. # Called when the node enters the scene tree for the first time.
  103. func _ready() -> void:
  104. pass # Replace with function body.
  105. # Called every frame. 'delta' is the elapsed time since the previous frame.
  106. func _process(delta: float) -> void:
  107. pass
  108. .. code-tab:: csharp
  109. // meta-description: Base template for Node with default Godot cycle methods
  110. using _BINDINGS_NAMESPACE_;
  111. using System;
  112. public partial class _CLASS_ : _BASE_
  113. {
  114. // Called when the node enters the scene tree for the first time.
  115. public override void _Ready()
  116. {
  117. }
  118. // Called every frame. 'delta' is the elapsed time since the previous frame.
  119. public override void _Process(double delta)
  120. {
  121. }
  122. }
  123. The Godot editor provides a set of useful built-in node-specific templates, such as
  124. ``basic_movement`` for both :ref:`CharacterBody2D <class_CharacterBody2D>` and
  125. :ref:`CharacterBody3D <class_CharacterBody3D>` and ``plugin`` for
  126. :ref:`EditorPlugin <class_EditorPlugin>`.
  127. List of template placeholders
  128. -----------------------------
  129. The following describes the complete list of built-in template placeholders
  130. which are currently implemented.
  131. Base placeholders
  132. ~~~~~~~~~~~~~~~~~
  133. +--------------------------+----------------------------------------------------+
  134. | Placeholder | Description |
  135. +==========================+====================================================+
  136. | ``_BINDINGS_NAMESPACE_`` | The name of the Godot namespace (used in C# only). |
  137. +--------------------------+----------------------------------------------------+
  138. | ``_CLASS_`` | The name of the new class. |
  139. +--------------------------+----------------------------------------------------+
  140. | ``_BASE_`` | The base type a new script inherits from. |
  141. +--------------------------+----------------------------------------------------+
  142. | ``_TS_`` | Indentation placeholder. The exact type and number |
  143. | | of whitespace characters used for indentation is |
  144. | | determined by the ``text_editor/indent/type`` and |
  145. | | ``text_editor/indent/size`` settings in the |
  146. | | :ref:`EditorSettings <class_EditorSettings>` |
  147. | | respectively. Can be overridden by the |
  148. | | ``meta-space-indent`` header on the template. |
  149. +--------------------------+----------------------------------------------------+
  150. Type placeholders
  151. ~~~~~~~~~~~~~~~~~
  152. There used to be, in Godot 3.x, placeholders for GDScript type hints that
  153. would get replaced whenever a template was used to create a new script, such as:
  154. ``%INT_TYPE%``, ``%STRING_TYPE%``, ``%FLOAT_TYPE%`` or ``%VOID_RETURN%``.
  155. The placeholders no longer work for Godot 4.x, but if the setting
  156. ``text_editor/completion/add_type_hints`` from
  157. :ref:`EditorSettings <class_EditorSettings>` is disabled, type hints
  158. for parameters and return types will be automatically removed for a few
  159. base types:
  160. * ``int``
  161. * ``String``
  162. * ``Array[String]``
  163. * ``float``
  164. * ``void``
  165. * ``:=`` will be transformed into ``=``