GameTests
experimental
WARNING
脚本API目前处于积极开发阶段,重大变更频繁。本文档基于Minecraft 1.20.40版本格式编写
GameTest框架允许我们创建单元测试("GameTests"),从而更轻松地测试游戏机制是否正常运行。
可通过/gametest命令使用GameTests功能:
/gametest runthis- 运行范围内最近的GameTest/gametest runthese- 运行范围内所有GameTest/gametest pos- 显示最近GameTest的相对坐标/gametest clearall [radius: int]- 移除指定半径内的所有GameTest/gametest run <testName: GameTestName> [rotationSteps: int]- 创建并运行指定的GameTest/gametest runset [tagTag: GameTestTag] [rotationSteps: int]- 创建并运行所有带有指定标签的GameTest/gametest create <testName: string> [width: int] [height: int] [depth: int]- 创建指定尺寸的空白GameTest区域/reload- 重新加载所有行为包中的函数和脚本文件(1.19+版本)
(1.19.40+版本)原版GameTests已从Minecraft游戏文件中移除,因此需要添加自定义行为包才能运行任何gametest。您可以在官方仓库中找到它们。
GameTest入门指南
开始前,您需要准备自己的行为包并具备一定的脚本和API知识。如果是初次接触,建议先阅读这篇入门文章。
使用GameTest框架需要@minecraft/server-gametest模块。该API模块同时依赖@minecraft/server模块,因此需要在manifest.json中添加以下依赖项:
BP/manifest.json/
json
"dependencies": [
{
"module_name": "@minecraft/server",
"version": "1.7.0-beta"
},
{
"module_name": "@minecraft/server-gametest",
"version": "1.0.0-beta"
}
]运行GameTest需要在行为包中包含结构文件,并通过register函数注册命令。
BP/scripts/Main.js
js
import * as GameTest from "@minecraft/server-gametest";
// 测试注册代码
GameTest.register(
"wiki", // 测试类名称
"simpleTest", // 本测试名称
(test) => {
// 测试实现
/**
* @type {import("@minecraft/server").Vector3}
* 测试中生成牛的位置坐标
*/
const location = { x: 0, y: 0, z: 0 };
const cow = test.spawn("minecraft:cow", location); // 返回实体实例
test.succeedWhen(() => {
test.assertEntityPresentInArea("minecraft:cow", true);
});
}
)
.maxTicks(410)
.structureName("mystructure:wiki"); /* 使用wiki.mcstructure文件 */命令注册后,测试函数将被锁定,这意味着测试函数无法再访问函数外部的变量。
如果遇到脚本API相关问题,可以参考微软学习平台的构建第一个GameTest教程,或加入Bedrock Add-Ons社区寻求帮助。更多资源可在实用链接页面找到!











