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...
为专用服务器导出
如果要在没有 GPU 或没有显示服务器的机器上为项目运行专用服务器,你需要在运行 Godot 的时候使用 headless
显示服务器和 Dummy
音频驱动。
从Godot 4.0开始,这可以透过在任何平台上使用 --headless
命令行参数来执行Godot二进位档案或执行导出为专用服务器的项目来完成。与Godot 3.x不同,你不再需要使用专门的服务器二进位。
编辑器与导出模板
可以在无头模式下使用编辑器或导出模板(调试或发布)二进位。你应该使用哪一种取决于你的用例:
Export template(导出模板):不包含编辑器的功能,体积更小、优化更高,是在服务器平台下的最佳选择。
Editor(编辑器):包含编辑器功能的可执行文件,目的是用来导出项目。该可执行文件可以用来运行专用服务,但是因为其体积较大、优化程度较低,所以不建议将其作为专用服务器使用。
导出方法
针对服务器导出项目的方法有两种:
为服务器托管平台创建单独的导出预设,然后像往常一样导出项目。
仅导出PCK档案,最好是与将托管服务器的平台相配对的平台。将此PCK档案放在与导出模板二进位档案相同的文件夹中,将二进位档案重新命名为与PCK相同的名称(减去文件扩展名),然后执行该二进位档案。
两种方法的输出结果应该相同。本页其余部分将重点介绍第一种方法。
详见 导出项目 .
为专用服务器导出项目
如果你在以服务器为目标时像往常一样导出项目,你会注意到 PCK 文件与客户端一样大。这是因为它包含所有资源,其中包括服务器不需要的资源(例如纹理数据)。此外,不会自动使用无头模式;用户必须指定 --headless
以确保不会生成任何窗口。
许多资源(例如纹理)可以从PCK档案中剥离,以大大减少其大小。Godot提供了一种对纹理和材质执行此操作的方法,可保留场景或资源档案(内置或外部)中的参考。
首先,请确保你的服务器有专用的导出预设,然后选择它,转到其**Resources**标签并更改其导出模式:

在导出预设中选择**导出为专用服务器**导出模式
选择此导出模式后,dedicated_server
功能标记将自动新增至导出的项目。
备注
如果你不想使用该导出模式但仍想要功能标签,则可以在导出预设的功能选项卡中写入名称 dedicated_server
。这也会在运行导出的项目时强制使用 --headless
。
选择此导出模式后,你将看到项目中的资源列表:

选择要保留的资源、保留剥离的视觉效果或删除
勾选复选框可让你复写指定档案或文件夹的选项。复选框**不会**影响导出哪些档案;这是透过为每个复选框选择的选项来完成的。
预设情况下,提取文件夹中的档案将自动使用父级选项,这由选项名称的**(Inherited)**后缀表示(并且选项名称呈灰色)。若要变更目前继承选项的档案的选项,必须先勾选该档案旁的方块。
Strip Visuals:导出此资源,其中的视觉文件(纹理和材质)被占位符类替换。占位符类存储图像大小(有时用于在 2D 场景中定位元素),但不存储其他内容。
Keep:照常导出此资源,并保留完整的视觉文件。
Remove:该文件不包含在 PCK 中。这对于忽略只有客户端需要的场景和资源很有用。如果这样做,请确保服务器不会以任何方式引用这些仅限客户端的场景和资源。
一般建议尽可能使用 Strip Visuals,除非服务器需要访问图像数据(例如像素颜色)。例如,如果你的服务器根据图像的内容生成碰撞数据,则需要对该特定图像使用 Keep。
小技巧
要检查导出的 PCK 的文件结构,请使用导出 PCK/ZIP…按钮并指定 .zip
文件扩展名,然后在文件管理器中打开得到的 ZIP 文件。
警告
使用**Remove**模式时要小心,因为引用已删除档案的场景/资源将无法再成功加载。
如果想要移除特定的资源,并且希望场景仍然能够正常加载,你就必须移除场景文件中的相关引用,在脚本中使用 load()
为节点的属性加载这些文件。Godot 不支持将音频之类的资源替换为占位符,但可以用这种方法来剥离。
移除纹理通常会对 PCK 大小影响最大,因此建议先坚持使用 Strip Visuals。
使用上面的选项后,客户端的 PCK(正常导出所有资源)是这样的:
.
├── .godot
│ ├── exported
│ │ └── 133200997
│ │ └── export-78c237d4bfdb4e1d02e0b5f38ddfd8bd-scene.scn
│ ├── global_script_class_cache.cfg
│ ├── imported
│ │ ├── map_data.png-ce840618f399a990343bfc7298195a13.ctex
│ │ ├── music.ogg-fa883da45ae49695a3d022f64e60aee2.oggvorbisstr
│ │ └── sprite.png-7958af25f91bb9dbae43f35388f8e840.ctex
│ └── uid_cache.bin
├── client
│ ├── music.ogg.import
│ └── sprite.png.import
├── server
│ └── map_data.png.import
├── test
│ └── scene.gd
└── unused
│ └── development_test.gd
├── project.binary
├── scene.gd
├── scene.tscn.remap
服务器的 PCK 文件结构是这样的:
.
├── .godot
│ ├── exported
│ │ └── 3400186661
│ │ ├── export-78c237d4bfdb4e1d02e0b5f38ddfd8bd-scene.scn
│ │ ├── export-7958af25f91bb9dbae43f35388f8e840-sprite.res # Placeholder texture
│ │ └── export-fa883da45ae49695a3d022f64e60aee2-music.res
│ ├── global_script_class_cache.cfg
│ ├── imported
│ │ └── map_data.png-ce840618f399a990343bfc7298195a13.ctex
│ └── uid_cache.bin
├── client
│ ├── music.ogg.import
│ └── sprite.png.import # Points to placeholder texture
└── server
│ └── map_data.png.import
├── project.binary
├── scene.gd
├── scene.tscn.remap
启动专用服务器
如果你的客户端和服务器都是同一个 Godot 项目的一部分,则必须添加一种使用命令行参数直接启动服务器的方法。
如果你使用导出为专用服务器导出模式 导出了该项目(或已添加 dedicated_server
作为自定义功能标记),则可以使用 dedicated_server
功能标签来检测是否正在使用专用服务器 PCK:
# Note: Feature tags are case-sensitive.
if OS.has_feature("dedicated_server"):
# Run your server startup code here...
pass
// Note: Feature tags are case-sensitive.
if (OS.HasFeature("dedicated_server"))
{
// Run your server startup code here...
}
如果你还希望在使用内置的 --headless
命令行参数时托管服务器,可以在主场景(或自动加载)的 _ready()
方法中添加以下代码段来实现:
if DisplayServer.get_name() == "headless":
# Run your server startup code here...
#
# Using this check, you can start a dedicated server by running
# a Godot binary (editor or export template) with the `--headless`
# command-line argument.
pass
using System.Linq;
if (DisplayServer.GetName() == "headless")
{
// Run your server startup code here...
//
// Using this check, you can start a dedicated server by running
// a Godot binary (editor or export template) with the `--headless`
// command-line argument.
}
如果希望使用自定义命令行参数,可在主场景(或自动加载)的 _ready()
方法中添加以下代码段:
if "--server" in OS.get_cmdline_user_args():
# Run your server startup code here...
#
# Using this check, you can start a dedicated server by running
# a Godot binary (editor or export template) with the `--server`
# command-line argument.
pass
using System.Linq;
if (OS.GetCmdlineUserArgs().Contains("--server"))
{
// Run your server startup code here...
//
// Using this check, you can start a dedicated server by running
// a Godot binary (editor or export template) with the `--server`
// command-line argument.
}
最好新增至少一个上述命令列参数来启动服务器,因为它可用于从命令列测试服务器功能,而无需导出项目。
如果你的客户端和服务器是独立的Godot项目, 服务器通常应该配置成运行主场景时自启服务的方式.
下一步
On Linux, to make your dedicated server restart after a crash or system reboot,
you can
create a systemd service.
This also lets you view server logs in a more convenient fashion, with automatic
log rotation provided by systemd. When making your project hostable as a systemd service,
you should also enable the application/run/flush_stdout_on_print
project setting. This way, journald (the systemd logging service) can collect
logs while the process is running.
如果你有容器的经验, 可以考虑将专用服务器包装在一个 Docker 容器中. 这样, 在弹性配置中可以更容易地使用它(这不在本教程的范围内).