creating_script_templates.rst 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.png
  7. A set of script templates is provided by default, but it's also possible
  8. to modify existing and create new ones, both per project and editor-wide.
  9. Locating the templates
  10. ----------------------
  11. There are two places where templates can be managed.
  12. Editor-defined templates
  13. ~~~~~~~~~~~~~~~~~~~~~~~~
  14. These are available globally throughout any project. The location of these
  15. templates are determined per each OS:
  16. - Windows: ``%APPDATA%\Godot\script_templates\``
  17. - Linux: ``$HOME/.local/share/godot/script_templates/``
  18. - macOS: ``$HOME/Library/Application Support/Godot/script_templates/``
  19. If no ``script_templates`` is detected, Godot will create a default set of
  20. built-in templates automatically, so this logic can be used to reset the default
  21. templates in case you've accidentally overwritten them.
  22. Project-defined templates
  23. ~~~~~~~~~~~~~~~~~~~~~~~~~
  24. The default path to search for templates is the
  25. ``res://script_templates/`` directory. The path can be changed by configuring
  26. the ``editor/script_templates_search_path`` setting in the
  27. :ref:`ProjectSettings <class_ProjectSettings>`, both via code and the editor.
  28. If no ``script_templates`` directory is found within a project, it is simply
  29. ignored.
  30. Language support and overriding behavior
  31. ----------------------------------------
  32. Depending on whether a particular language implements a way to generate scripts
  33. out of templates, it's possible to create a template which can be recognized by
  34. that language according to template's file extension. For GDScript and C#, the
  35. extensions must be ``gd`` and ``cs`` respectively.
  36. .. note:: The script templates have the same extension as the regular script
  37. files. This may lead to an issue of a script parser treating those templates as
  38. actual scripts within a project. To avoid this, make sure to ignore the
  39. directory containing them by creating a ``.gdignore`` file. The directory won't be
  40. visible throughout the project's filesystem anymore, yet the templates can be
  41. modified by an external text editor anytime.
  42. The built-in editor templates are automatically shadowed by the project-specific
  43. templates given both scripts have the same filename.
  44. Default template
  45. ----------------
  46. The ``Default`` template is always generated dynamically per language and cannot
  47. be configured nor overridden, but you can use these as the base for creating
  48. other templates.
  49. .. tabs::
  50. .. code-tab:: gdscript GDScript
  51. extends %BASE%
  52. # Declare member variables here. Examples:
  53. # var a%INT_TYPE% = 2
  54. # var b%STRING_TYPE% = "text"
  55. # Called when the node enters the scene tree for the first time.
  56. func _ready()%VOID_RETURN%:
  57. pass # Replace with function body.
  58. # Called every frame. 'delta' is the elapsed time since the previous frame.
  59. #func _process(delta%FLOAT_TYPE%)%VOID_RETURN%:
  60. # pass
  61. .. code-tab:: csharp
  62. using Godot;
  63. using System;
  64. public class %CLASS% : %BASE%
  65. {
  66. // Declare member variables here. Examples:
  67. // private int a = 2;
  68. // private string b = "text";
  69. // Called when the node enters the scene tree for the first time.
  70. public override void _Ready()
  71. {
  72. }
  73. // // Called every frame. 'delta' is the elapsed time since the previous frame.
  74. // public override void _Process(float delta)
  75. // {
  76. //
  77. // }
  78. }
  79. List of template placeholders
  80. -----------------------------
  81. The following describes the complete list of built-in template placeholders
  82. which are currently implemented.
  83. Base placeholders
  84. ~~~~~~~~~~~~~~~~~
  85. +-------------+----------------------------------------------------------------+
  86. | Placeholder | Description |
  87. +=============+================================================================+
  88. | ``%CLASS%`` | The name of the new class (used in C# only). |
  89. +-------------+----------------------------------------------------------------+
  90. | ``%BASE%`` | The base type a new script inherits from. |
  91. +-------------+----------------------------------------------------------------+
  92. | ``%TS%`` | Indentation placeholder. The exact type and number of |
  93. | | whitespace characters used for indentation is determined by |
  94. | | the ``text_editor/indent/type`` and ``text_editor/indent/size``|
  95. | | settings in the :ref:`EditorSettings <class_EditorSettings>` |
  96. | | respectively. |
  97. +-------------+----------------------------------------------------------------+
  98. Type placeholders
  99. ~~~~~~~~~~~~~~~~~
  100. These are only relevant for GDScript with static typing. Whether these
  101. placeholders are actually replaced is determined by the
  102. ``text_editor/completion/add_type_hints`` setting in the
  103. :ref:`EditorSettings <class_EditorSettings>`.
  104. +-------------------+--------------+
  105. | Placeholder | Value |
  106. +===================+==============+
  107. | ``%INT_TYPE%`` | ``: int`` |
  108. +-------------------+--------------+
  109. | ``%STRING_TYPE%`` | ``: String`` |
  110. +-------------------+--------------+
  111. | ``%FLOAT_TYPE%`` | ``: float`` |
  112. +-------------------+--------------+
  113. | ``%VOID_RETURN%`` | ``-> void`` |
  114. +-------------------+--------------+