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...
导出包、补丁、Mod
使用案例
Oftentimes, one would like to add functionality to one's game after it has been deployed.
这样的例子包括……
可下载内容:向游戏添加功能和内容的能力。
补丁:修复已发布产品中存在的错误的能力。
Mod:授予其他人为自己的游戏创建内容的能力。
这些工具可帮助开发人员基于初始版本进行扩展开发。
Overview of PCK/ZIP files
Godot enables this via a feature called resource packs (PCK files,
with the .pck
extension, or ZIP files).
优势:
增量更新/补丁
提供 DLC
提供 mod 支持
Mod 不需要公开源代码
更加模块化的项目结构
用户无需替换整个游戏
The first part of using them involves exporting and delivering the project to players. Then, when one wants to add functionality or content later on, they just deliver the updates via PCK/ZIP files to the users.
PCK/ZIP files usually contain, but are not limited to:
脚本
场景
着色器
模型
纹理
音效
音乐
任何其他适合导入游戏的资产
The PCK/ZIP files can even be an entirely different Godot project, which the original game loads in at runtime.
It is possible to load both PCK and ZIP files as additional packs at the same time. See PCK 与 ZIP 打包文件格式对比 for a comparison of the two formats.
参见
If you want to load loose files at runtime (not packed in a PCK or ZIP by Godot), consider using 运行时文件加载和保存 instead. This is useful for loading user-generated content that is not made with Godot, without requiring users to pack their mods into a specific file format.
The downside of this approach is that it's less transparent to the game logic, as it will not benefit from the same resource management as PCK/ZIP files.
生成 PCK 文件
In order to pack all resources of a project into a PCK file, open the project and go to Project > Export and click on Export PCK/ZIP. Also, make sure to have an export preset selected while doing so.

Another method would be to export from the command line
with --export-pack
. The output file must with a .pck
or .zip
file extension. The export process will build that type of file for the
chosen platform.
备注
如果有人希望为他们的游戏支持 mod,他们将需要其用户创建类似的导出文件。假设原始游戏需要 PCK 资源的某种结构和/或其脚本具有特定的接口,那么有两种选择……
开发人员必须公开这些预期结构/接口的文档,期望模组制作者安装 Godot 引擎,然后,在为游戏构建 Mod 内容时,这些修改者也将遵守文档中定义的 API(这样它将起作用)。用户然后将如上所述,使用 Godot 的内置导出工具来创建 PCK 文件。
开发者使用 Godot 来构建 GUI 工具,用这个工具向项目中添加特定的 API 内容。这个 Godot 工具要么是在启用了工具构建的引擎上执行,要么就必须能够访问到这种版本的邀请(一同分发,或者加入到原版游戏的文件之中)。这样这个工具就可以使用 OS.execute() 通过命令行使用 Godot 可执行文件来导出 PCK 文件。游戏本体不应该使用工具构建的引擎(出于安全考虑),所以最好将 mod 工具和游戏分开。
Opening PCK or ZIP files at runtime
To load a PCK or ZIP file, one uses the ProjectSettings singleton. The following
example expects a mod.pck
file in the directory of the game's executable.
The PCK or ZIP file contains a mod_scene.tscn
test scene in its root.
func _your_function():
# This could fail if, for example, mod.pck cannot be found.
var success = ProjectSettings.load_resource_pack(OS.get_executable_path().get_base_dir().path_join("mod.pck"))
if success:
# Now one can use the assets as if they had them in the project from the start.
var imported_scene = load("res://mod_scene.tscn")
private void YourFunction()
{
// This could fail if, for example, mod.pck cannot be found.
var success = ProjectSettings.LoadResourcePack(OS.get_executable_path().get_base_dir().path_join("mod.pck));
if (success)
{
// Now one can use the assets as if they had them in the project from the start.
var importedScene = (PackedScene)ResourceLoader.Load("res://mod_scene.tscn");
}
}
警告
By default, if you import a file with the same file path/name as one you already have in your project, the imported one will replace it. This is something to watch out for when creating DLC or mods. You can solve this problem by using a tool that isolates mods to a specific mods subfolder.
However, it is also a way of creating patches for one's own game. A PCK/ZIP file of this kind can fix the content of a previously loaded PCK/ZIP (therefore, the order in which packs are loaded matters).
为了退出这个行为, 把 false
作为第二个参数传递给 ProjectSettings.load_resource_pack().
备注
对于C#项目, 你必需先构建DLL并把它放在项目目录中. 然后, 在加载资源包之前, 你需要按如下方法加载它的DLL:Assembly.LoadFile("mod.dll")
故障排除
If you are loading a resource pack and are not noticing any changes, it may be due to the pack being loaded too late. This is particularly the case with menu scenes that may preload other scenes using preload(). This means that loading a pack in the menu will not affect the other scene that was already preloaded.
To avoid this, you need to load the pack as early as possible.
To do so, create a new autoload script and
call ProjectSettings.load_resource_pack()
in the autoload script's _init()
function, rather than _enter_tree()
or _ready()
.
总结
本教程介绍如何向游戏添加模组、补丁或 DLC。最重要的是确定一个人计划如何为其游戏分发未来的内容,并开发为此目的定制的工作流程。无论开发人员选择哪种方法,Godot 都应该使该过程顺利进行。