Advanced label features
Explains advanced label options in Pixi’VN, such as onLoadingLabel, onStepStart, onStepEnd, and dynamic step lists.
This section covers advanced label features. These are not required to create a basic game, but can help you build more complex and interactive experiences.
onLoadingLabel is a function executed in onStepStart if the step index is 0, and also when the user loads a save file.
When a save file is loaded, all onLoadingLabel functions of the narration.labels.opened (the current label and all labels in the stack) are executed.
This is useful, for example, to ensure all images used in the label are cached before the label starts.
You can set this function by passing it to the onLoadingLabel property in the label options.
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");
},
},
);You can also use Game.onLoadingLabel to intercept the loading of all labels globally.
import { Game } from "@drincs/pixi-vn";
Game.onLoadingLabel((_stepId, label) => {
Assets.backgroundLoadBundle(label.id);
}); onStepStart is a function executed before each step of a label. Set it via the onStepStart property in the label options.
const startLabel = newLabel(
"start",
[
() => {
narration.dialogue = "Step 1";
},
() => {
narration.dialogue = "Step 2";
},
],
{
onStepStart: (stepIndex, label) => {
console.log(`Step ${stepIndex} started`);
},
},
);You can also use Game.onStepStart to intercept the start of any step in any label.
import { Game } from "@drincs/pixi-vn";
Game.onStepStart((stepIndex, label) => {
console.log(`Step ${stepIndex} started`);
}); onStepEnd is a function executed after each step of a label. Set it via the onStepEnd property in the label options.
const startLabel = newLabel(
"start",
[
() => {
narration.dialogue = "Step 1";
},
() => {
narration.dialogue = "Step 2";
},
],
{
onStepEnd: (stepIndex, label) => {
console.log(`Step ${stepIndex} ended`);
},
},
);You can also use Game.onStepEnd to intercept the end of any step in any label.
import { Game } from "@drincs/pixi-vn";
Game.onStepEnd((stepIndex, label) => {
console.log(`Step ${stepIndex} ended`);
}); When creating a new label, you can pass a function that returns the steps for that label. This allows you to change the step list dynamically based on a condition.
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);
},
];
}
});