高级标签功能
解释 Pixi'VN 中的高级标签选项,例如 onLoadingLabel、onStepStart、onStepEnd 和动态步骤列表。
本节涵盖高级叙事节点功能。这些功能不是创建基本游戏所必需的,但可以帮助您构建更复杂、更具交互性的体验。
onLoadingLabel 是一个在 onStepStart 中执行的函数,当叙事步骤(step)索引为 0 时执行,也在用户加载存档文件时执行。
加载存档文件时,narration.labels.opened 的所有 onLoadingLabel 函数(当前叙事节点(label)以及堆栈中的所有叙事节点(labels))都会被执行。
例如,这对于确保在叙事节点(label)开始之前缓存其中使用的所有图像非常有用。
您可以通过将该函数传递给叙事节点(label)选项中的 onLoadingLabel 属性来设置此函数。
const startLabel = newLabel(
"start",
[
async () => {
await showImage("image1", "path/to/image1.png");
await showImage("image2", "path/to/image2.png");
},
],
{
onLoadingLabel: async (stepIndex, label) => {
await Assets.load("path/to/image1.png");
await Assets.load("path/to/image2.png");
},
},
);您也可以使用 Game.onLoadingLabel 来全局拦截所有叙事节点(labels)的加载。
import { Game } from "@drincs/pixi-vn";
Game.onLoadingLabel((_stepId, label) => {
Assets.backgroundLoadBundle(label.id);
}); onStepStart 是一个在叙事节点(label)的每个叙事步骤(step)之前执行的函数。通过叙事节点(label)选项中的 onStepStart 属性来设置它。
const startLabel = newLabel(
"start",
[
() => {
narration.dialogue = "Step 1";
},
() => {
narration.dialogue = "Step 2";
},
],
{
onStepStart: (stepIndex, label) => {
console.log(`Step ${stepIndex} started`);
},
},
);您也可以使用 Game.onStepStart 来拦截任意叙事节点(label)中任意叙事步骤(step)的开始。
import { Game } from "@drincs/pixi-vn";
Game.onStepStart((stepIndex, label) => {
console.log(`Step ${stepIndex} started`);
}); onStepEnd 是一个在叙事节点(label)的每个叙事步骤(step)之后执行的函数。通过叙事节点(label)选项中的 onStepEnd 属性来设置它。
const startLabel = newLabel(
"start",
[
() => {
narration.dialogue = "Step 1";
},
() => {
narration.dialogue = "Step 2";
},
],
{
onStepEnd: (stepIndex, label) => {
console.log(`Step ${stepIndex} ended`);
},
},
);您也可以使用 Game.onStepEnd 来拦截任意叙事节点(label)中任意叙事步骤(step)的结束。
import { Game } from "@drincs/pixi-vn";
Game.onStepEnd((stepIndex, label) => {
console.log(`Step ${stepIndex} ended`);
}); 创建新的叙事节点(label)时,您可以传入一个返回该叙事节点(label)叙事步骤(steps)的函数。这使您可以根据条件动态更改叙事步骤(step)列表。
import { storage, narration, newLabel } from "@drincs/pixi-vn";
export const startLabel = newLabel("start", () => {
let condition = storage.flags.get("condition");
if (condition) {
return [
() => {
narration.dialogue = "Step 2";
},
() => {
narration.dialogue = "Restart";
},
];
} else {
return [
() => {
narration.dialogue = "Step 1";
},
async (props, { labelId }) => {
storage.flags.set("condition", true);
return await narration.jump(labelId, props);
},
];
}
});