Crear plantillas de scripts

Godot provee un modo de usar plantillas de scripts como puede verse en el Diálogo Añadir Script mientras se crea un nuevo script:

../../_images/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.

Localizando las plantillas

Hay dos lugares donde las plantillas pueden ser colocadas.

Plantillas definidas en el editor

Esas están disponibles globalmente para cualquier proyecto. La ubicación de esas plantillas está determinada por cada SO:

  • 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.

Plantillas definidas en el proyecto

The default path to search for templates is the res://script_templates/ directory. The path can be changed by configuring the project setting Editor > Script > Templates Search Path, both via code and the editor.

Si no se encuentra la carpeta script_templates en un proyecto, esta es ignorada.

Template organization and naming

Both editor and project defined templates are organized in the following way:

template_path/node_type/file.extension

where:

  • template_path is one of the 2 locations discussed in the previous two sections.

  • node_type is the node it will apply to (for example, Node, or CharacterBody3D), This is case-sensitive. If a script isn't in the proper node_type folder, it won't be detected.

  • file is the custom name you can chose for the template (for example, platformer_movement or smooth_camera).

  • extension indicates which language the template will apply to (it should be gd for GDScript or cs for C#).

Por ejemplo:

  • script_templates/Node/smooth_camera.gd

  • script_templates/CharacterBody3D/platformer_movement.gd

Default behavior and overriding it

Por defecto:

  • the template's name is the same as the file name (minus the extension, prettyfied)

  • la descripción está vacía

  • the space indent is set to 4

  • la plantilla no se asignará como por defecto para el nodo dado

It is possible to customize this behavior by adding meta headers at the start of your file, like this:

# meta-name: Platformer movement
# meta-description: Predefined movement for classical platformers
# meta-default: true
# meta-space-indent: 4

In this case, the name will be set to "Platformer movement", with the given custom description, and it will be set as the default template for the node in which directory it has been saved.

This is an example of utilizing custom templates at editor and project level:

../../_images/script_create_dialog_custom_templates.webp

Nota

Las plantillas de script tienen la misma extensión que los archivos de script regulares. Esto puede provocar un problema en el que un analizador de scripts trate esas plantillas como scripts reales dentro de un proyecto. Para evitar esto, asegúrate de ignorar el directorio que las contiene creando un archivo .gdignore vacío. El directorio ya no será visible en el sistema de archivos del proyecto, pero las plantillas aún podrán ser modificadas por un editor de texto externo en cualquier momento.

Truco

By default, every C# file inside the project directory is included in the compilation. Script templates must be manually excluded from the C# project to avoid build errors. See Exclude files from the build in the Microsoft documentation.

It is possible to create editor-level templates that have the same level as a project-specific templates, and also that have the same name as a built-in one, all will be shown on the new script dialog.

Plantilla por defecto

To override the default template, create a custom template at editor or project level inside a Node directory (or a more specific type, if only a subtype wants to be overridden) and start the file with the meta-default: true header.

Only one template can be set as default at the same time for the same node type.

Las plantillas Default para nodos básicos, tanto para GDScript como C#, están aquí para que se puedan usar como base para la creación de otras plantillas:

# 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

The Godot editor provides a set of useful built-in node-specific templates, such as basic_movement for both CharacterBody2D and CharacterBody3D and plugin for EditorPlugin.

Lista de plantillas placeholder

Lo siguiente describe la lista completa de placeholders de plantillas integradas que están actualmente implementados.

Placeholders base

Parámetros

Descripción

_BINDINGS_NAMESPACE_

The name of the Godot namespace (used in C# only).

_CLASS_

The name of the new class.

_CLASS_SNAKE_CASE_

The name of the new class as snake_case (used in GDScript only).

_BASE_

El tipo base del que el nuevo script hereda.

_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 EditorSettings respectively. Can be overridden by the meta-space-indent header on the template.

Placeholders de tipo

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 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 =