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...
创建脚本模板¶
Godot 提供了一种在创建新脚本时使用脚本模板的方法, 在 脚本创建对话框
中可以看到:
![../../_images/script_create_dialog_templates.webp](../../_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.
模板所在位置¶
有两个地方可以管理模板.
编辑器定义的模板¶
这些在任何项目中都可以全局使用. 这些模板的位置是根据操作系统而确定的:
Windows:
%APPDATA%\Godot\script_templates\
Linux:
$HOME/.config/godot/script_templates/
macOS:
$HOME/Library/Application Support/Godot/script_templates/
如果未检测到 script_templates
,Godot将自动创建一组默认的内置模板. 在默认模板被意外覆盖的时候可以用这个方法重置.
项目定义的模板¶
搜索模板的默认路径是 res://script_templates/
文件夹. 可以通过代码和编辑器在 项目设置 中配置 editor/script_templates_search_path
设置来更改路径.
如果在项目中未找到 script_templates
文件夹, 则将其忽略.
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 sectionsnode_type
is the node it will apply to (for example, Node, or CharacterBody3D)file
is the custom name you can chose for the template (for example:platformer_movement
orsmooth_camera
)extension
: will indicate 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
Default behaviour and overriding it¶
By default:
the template's name is the same as the file name (minus the extension, prettyfied)
the description is empty
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 behaviour 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:
![../../_images/script_create_dialog_custom_templates.webp](../../_images/script_create_dialog_custom_templates.webp)
备注
脚本模板与常规脚本文件具有相同的扩展名。脚本解析器将这些模板视为项目中的实际脚本,这可能会导致问题。为了避免这种情况,创建空的 .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.
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.
默认模板¶
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.
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:
# 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.
模板占位符¶
下面列出当前已实现的所有模板占位符.
基本占位符¶
占位符 |
描述 |
---|---|
|
The name of the Godot namespace (used in C# only). |
|
新建类的名称(只在 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
:=
will be transformed into=