创建脚本模板
Godot 提供了一种在创建新脚本时使用脚本模板的方法, 在 脚本创建对话框
中可以看到:

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.
模板所在位置
有两个地方可以管理模板.
编辑器定义的模板
这些模板是全局的,所有项目中均可使用。这些模板所在的位置取决于操作系统:
Windows:
%APPDATA%\Godot\script_templates\
Linux:
$HOME/.config/godot/script_templates/
macOS:
$HOME/Library/Application Support/Godot/script_templates/
如果你的 Godot 是从 Steam 等官方网站以外的地方获取的,那么这个文件夹可能位于其他位置。该文件夹可以使用 Godot 编辑器找到。请打开 编辑器 > 打开编辑器数据/设置文件夹
,这样就会在你的文件浏览器中打开一个文件夹,script_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.
如果在项目中未找到 script_templates
目录,则会将其忽略。
模板的组织和命名
编辑器和项目定义的模板使用的都是以下组织方式:
template_path/node_type/file.extension
其中:
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 propernode_type
folder, it won't be detected.file
is the custom name you can chose for the template (for example,platformer_movement
orsmooth_camera
).extension
indicates which language the template will apply to (it should begd
for GDScript orcs
for C#).
例如:
template_scripts/Node/smooth_camera.gd
template_scripts/CharacterBody3D/platformer_movement.gd
默认行为与覆盖
默认情况下:
模板名称与文件名一致(不含扩展名且经过美化)
描述为空
空格缩进为 4
不会将该模板设为给定节点的默认模板
在文件的开头添加元数据报头即可自定义该行为,类似于:
# 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
此处将名称设成了“Platformer movement”并给出了自定义描述,并且还将该模板设置成了所在目录对应节点的默认模板。
在编辑器和项目层面使用自定义模板的示例如下:

备注
脚本模板与常规脚本文件的扩展名相同。因此,脚本解析器可能将这些模板视为项目中的实际脚本,从而导致问题。为了避免这种情况,请在包含模板的目录中创建空的 .gdignore
文件,从而忽略这个目录。这样一来,该目录就不会再出现在项目的文件系统中,不过仍然可以随时使用外部文本编辑器对模板进行修改。
小技巧
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.
可以创建与项目特定模板级别相同的编辑器级模板,也可以创建与内置模板名称相同的编辑器级别模板,所有这些都将显示在新的脚本对话框中。
默认模板
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.
对于同一节点类型,同一时间只能将一个模板设置为默认模板。
默认
模板总是根据每种语言动态生成,并且不能配置也不能覆盖,但是你可以使用它们作为创建其他模板的基础:
# 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.
模板占位符
下面列出当前已实现的所有模板占位符.
基本占位符
占位符 |
描述 |
---|---|
|
Godot 命名空间的名称(仅在 C# 中使用)。 |
|
新建类的名称。 |
|
新建脚本继承的基类型。 |
|
Indentation placeholder. The exact type and number
of whitespace characters used for indentation is
determined by the |
类型占位符
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
:=
将被转换为=