Creating Script Templates - RST
Creating Script Templates - RST
_doc_creating_script_templates:
.. image:: img/script_create_dialog_templates.webp
A set of built-in script templates are provided with the editor, but it is
also possible to create new ones and set them by default, both per project
and at editor scope.
Templates are linked to a specific node type, so when you create a script
you will only see the templates corresponding to that particular node, or
one of its parent types.
For example, if you are creating a script for a CharacterBody3D, you will
only see templates defined for CharacterBody3Ds, Node3Ds or Nodes.
Editor-defined templates
~~~~~~~~~~~~~~~~~~~~~~~~
These are available globally throughout any project. The location of these
templates are determined per each OS:
- Windows: ``%APPDATA%\Godot\script_templates\``
- Linux: ``$HOME/.config/godot/script_templates/``
- macOS: ``$HOME/Library/Application Support/Godot/script_templates/``
If you're getting Godot from somewhere other than the official website, such
as Steam, the folder might be in a different location. You can find it using
the Godot editor. Go to ``Editor > Open Editor Data/Settings Folder`` and it
will open a folder in your file browser, inside that folder is the
``script_templates`` folder.
Project-defined templates
~~~~~~~~~~~~~~~~~~~~~~~~~
Both editor and project defined templates are organized in the following way:
::
template_path/node_type/file.extension
where:
* ``file`` is the custom name you can chose for the template (for example:
``platformer_movement`` or ``smooth_camera``)
* ``extension``: will indicate which language the template will apply to (it should
be ``gd`` for GDScript or ``cs`` for C#)
For example:
- ``template_scripts/Node/smooth_camera.gd``
- ``template_scripts/CharacterBody3D/platformer_movement.gd``
By default:
* the template's name is the same as the file name (minus the extension,
prettyfied)
* the template will not be set as the default for the given node
.. tabs::
.. code-tab:: csharp
.. image:: img/script_create_dialog_custom_templates.webp
.. note:: The script templates have the same extension as the regular script
files. This may lead to an issue of a script parser treating those
templates as
actual scripts within a project. To avoid this, make sure to ignore the
directory containing them by creating an empty ``.gdignore`` file. The
directory won't be
visible throughout the project's filesystem anymore, yet the templates
can be
modified by an external text editor anytime.
.. tip::
Default template
----------------
Only one template can be set as default at the same time for the same node type.
The ``Default`` templates for basic Nodes, for both GDScript and C#, are shown here
so you can
use these as the base for creating other templates:
.. tabs::
# meta-description: Base template for Node with default Godot cycle methods
extends _BASE_
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
.. code-tab:: csharp
// meta-description: Base template for Node with default Godot cycle methods
using _BINDINGS_NAMESPACE_;
using System;
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
}
The Godot editor provides a set of useful built-in node-specific templates, such as
``basic_movement`` for both :ref:`CharacterBody2D <class_CharacterBody2D>` and
:ref:`CharacterBody3D <class_CharacterBody3D>` and ``plugin`` for
:ref:`EditorPlugin <class_EditorPlugin>`.
Base placeholders
~~~~~~~~~~~~~~~~~
+--------------------------+----------------------------------------------------+
| Placeholder | Description |
+==========================+====================================================+
| ``_BINDINGS_NAMESPACE_`` | The name of the Godot namespace (used in C# only). |
+--------------------------+----------------------------------------------------+
| ``_CLASS_`` | The name of the new class. |
+--------------------------+----------------------------------------------------+
| ``_BASE_`` | The base type a new script inherits from. |
+--------------------------+----------------------------------------------------+
| ``_TS_`` | Indentation placeholder. The exact type and number |
| | of whitespace characters used for indentation is |
| | determined by the ``text_editor/indent/type`` and |
| | ``text_editor/indent/size`` settings in the |
| | :ref:`EditorSettings <class_EditorSettings>` |
| | respectively. Can be overridden by the |
| | ``meta-space-indent`` header on the template. |
+--------------------------+----------------------------------------------------+
Type placeholders
~~~~~~~~~~~~~~~~~
There used to be, in Godot 3.x, placeholders for GDScript type hints that
would get replaced whenever a template was used to create a new script, such as:
``%INT_TYPE%``, ``%STRING_TYPE%``, ``%FLOAT_TYPE%`` or ``%VOID_RETURN%``.
The placeholders no longer work for Godot 4.x, but if the setting
``text_editor/completion/add_type_hints`` from
:ref:`EditorSettings <class_EditorSettings>` is disabled, type hints
for parameters and return types will be automatically removed for a few
base types:
* ``int``
* ``String``
* ``Array[String]``
* ``float``
* ``void``
* ``:=`` will be transformed into ``=``