Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
Création de modèles de script
Godot fournit un moyen d'utiliser des modèles de script comme on le voit dans le Script Create Dialog lors de la création d'un nouveau script :
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.
Localisation des modèles
Il existe deux endroits où les modèles peuvent être gérés.
Modèles définis par l'éditeur
Ils sont disponibles à travers tout les projets. L'emplacement de ces modèles est déterminé pour chaque OS :
Windows :
C:\Users\[username]\AppData\Roaming\Godot\script_templatesLinux :
$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.
Modèles définis par le projet (templates)
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 aucun répertoire script_templates n'est trouvé dans un projet, il est simplement ignoré.
Template organization and naming
Both editor and project defined templates are organized in the following way:
template_path/node_type/file.extension
où :
template_pathis one of the 2 locations discussed in the previous two sections.node_typeis the node it will apply to (for example, Node, or CharacterBody3D), This is case-sensitive. If a script isn't in the propernode_typefolder, it won't be detected.fileis the custom name you can chose for the template (for example,platformer_movementorsmooth_camera).extensionindicates which language the template will apply to (it should begdfor GDScript orcsfor C#).
Par exemple :
script_templates/Node/smooth_camera.gdscript_templates/CharacterBody3D/platformer_movement.gd
Default behavior and overriding it
Par défaut :
the template's name is the same as the file name (minus the extension, prettyfied)
la description est vide
the space indent is set to 4
the template will not be set as the default for the given node
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
// 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:
Note
Les modèles de script ont la même extension que les fichiers de script ordinaires. Cela peut conduire à un problème d'analyseur de script traitant ces modèles comme des scripts réels dans un projet. Pour éviter cela, assurez-vous d'ignorer le répertoire qui les contient en créant un fichier vide .gdignore. Le répertoire ne sera plus visible dans le système de fichiers du projet, mais les modèles pourront être modifiés par un éditeur de texte externe à tout moment.
Astuce
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.
Modèle par défaut
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.
Les modèles Default pour les nœuds de base, à la fois en GDScript et en C#, sont présentés ici afin que vous puissiez les utiliser comme base pour créer d'autres modèles :
# 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
// meta-description: Base template for Node with default Godot cycle methods
using _BINDINGS_NAMESPACE_;
using System;
public partial class _CLASS_ : _BASE_
{
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
}
// 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 CharacterBody2D and
CharacterBody3D and plugin for
EditorPlugin.
Liste des modèles d'espaces réservés
Voici la liste complète des modèles d'espaces réservés intégrés qui sont actuellement implémenté.
Espaces réservés de base
Substitut temporaire |
Description |
|---|---|
|
The name of the Godot namespace (used in C# only). |
|
The name of the new class. |
|
The name of the new class as |
|
Le type de base dont hérite un nouveau script. |
|
Indentation placeholder. The exact type and number
of whitespace characters used for indentation is
determined by the |
Type d'espaces réservés
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:
intStringArray[String]floatvoid:=sera transformé en=