Manage narration flow with labels
Describes how to control the narrative flow in Pixi’VN using labels, including call, jump, and close labels function.
The narration flow is managed by functions such as call, jump, continue, back, and closeLabel, all available in the narration object.
Run a label
ink
You can use this method with the ink syntax. See more here.
To call a label, use the narration.call function.
import { narration } from "@drincs/pixi-vn";
narration.call(startLabel, {});When you call a label, the first step of that label is executed. If another label was running before the call, the remaining steps of that previous label will resume after the called label finishes.
For example, if the game is running step 5 of label A and you call label B, after all steps of label B are executed, the game continues with step 6 of label A.
narration.call returns the result of the first step of the called label.
If you execute narration.call inside a step, you should return the result of narration.call and use await. This ensures the history is managed correctly.
import { narration, newLabel } from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async (props) => {
return await narration.call(TestLabel, props);
},
async (props) => await narration.call(TestLabel, props),
]);To jump to a label, use the narration.jump function.
import { narration } from "@drincs/pixi-vn";
narration.jump(startLabel, {});When you jump to a label, the current label's steps are stopped and the new label's steps begin.
For example, if the game is running step 5 of label A, and you call label B, but then jump to label C, after label C finishes, the game continues with step 6 of label A. When you jump to label C, label B is closed.
narration.jump returns the result of the first step of the called label.
If you execute narration.jump inside a step, you should return the result of narration.jump and use await. This ensures the first step of the new label is awaited.
import { narration } from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async (props) => {
return await narration.jump(TestLabel, props);
},
async (props) => await narration.jump(TestLabel, props),
]);To start the game, use the Game.start function. This function is equivalent to calling a label, but it is recommended to use it for a cleaner startup.
import { Game } from "@drincs/pixi-vn";
Game.start(startLabel, gameProps);Continue and go back in steps
Continue
UI screen
You can find the example of "continue by clicking the screen" in the interface examples section.
To execute the next step, use the narration.continue function.
import { narration } from "@drincs/pixi-vn";
await narration.continue({});narration.continue is asynchronous, so you can use .then to, for example, disable a "Next" button until the step is complete.
import { narration } from "@drincs/pixi-vn";
// disable next button
narration.continue({}).then((result) => {
// enable next button
});If you call narration.continue inside a step, the "continue" request will be queued and executed when the queue is empty.
import { narration, newLabel } from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async (props) => {
narration.continue(props);
},
]);Here is an example to illustrate how the queue works:
import { narration, newLabel } from "@drincs/pixi-vn";
export const startLabel = newLabel("start", [
async (props) => {
await narration.call(label2, props);
console.log(1);
},
() => {
console.log(3);
},
]);
const label2 = newLabel("label_02", [
async (props) => {
await narration.continue(props);
console.log(2);
},
]);In this example, the output will be 2, 1, 3. The steps are:
await narration.call(label2, props)calls label2 and awaits its firststep. (1 item in the queue)- The first
stepof label2 executesawait narration.continue(props), but the continue request is queued because the queue is not empty. (2 items in the queue) console.log(2)runs, finishing the firststepof label2. (1 item in the queue)console.log(1)runs, finishing the firststepof startLabel. (0 items in the queue)- Now the continue request is executed, and the second
stepof startLabel runs. (1 item in the queue) console.log(3)runs, finishing the secondstepof startLabel. (0 items in the queue)
You can use the narration.canContinue property to check if the next step can be executed.
narration.canContinue is false when:
- A
stepis running - The player must make a choice
- The player must enter a value
import { narration } from "@drincs/pixi-vn";
function NextButton() {
return (
<button
disabled={!narration.canContinue}
onClick={() => {
narration.continue({});
}}
>
Next
</button>
);
}Go back
UI screen
You can find the example of the go back button in the interface examples section.
At every step, the system saves the current state of the game. To go back to the previous step, use the stepHistory.back function.
You can use the stepHistory.canGoBack property to check if going back is possible.
stepHistory.canGoBack is false when there are no steps in the history to restore.
import { stepHistory } from "@drincs/pixi-vn";
if (stepHistory.canGoBack) {
stepHistory.back();
}You can block the "go back" by calling stepHistory.blockGoBack.
import { stepHistory } from "@drincs/pixi-vn";
stepHistory.blockGoBack();Close labels
To close the current label, use the narration.labels.closeCurrent function.
import { narration } from "@drincs/pixi-vn";
narration.labels.closeCurrent();To close all labels, use the narration.labels.closeAll function. If you call this function and do not call any label afterwards, the game will end
import { narration } from "@drincs/pixi-vn";
narration.labels.closeAll();Other features
Templates
In all templates, this implementation is already included.
If you execute multiple stepHistory.back and narration.continue requests synchronously, the system will internally queue them and calculate the delta to execute only the necessary processes.
So you can implement rollback and rollforward features using the stepHistory.back and narration.continue functions.
import { HTML_CANVAS_LAYER_NAME, HTML_UI_LAYER_NAME } from "@/constans";
import { useGameProps } from "@/lib/hooks/props-hooks";
import { GameStatus } from "@/lib/stores/game-status-store";
import { hasScrollableParent } from "@/lib/utils/scroll-utils";
import { type StepLabelProps, stepHistory } from "@drincs/pixi-vn";
import { narration } from "@drincs/pixi-vn/narration";
import { useThrottler } from "@tanstack/react-pacer";
import { useEffect, useRef } from "react";
function isInsideRoot(target: EventTarget | null, selector: string): boolean {
if (!(target instanceof HTMLElement)) return false;
return target.closest(`#${selector}`) !== null;
}
export function useWheelActions({
throttleMs = 300,
minDelta = 20,
}: {
throttleMs?: number;
minDelta?: number;
} = {}) {
const pendingAsync = useRef(0);
const gameProps = useGameProps();
const runAsync = async (
fn: (props: StepLabelProps) => Promise<unknown>,
) => {
try {
pendingAsync.current += 1;
GameStatus.setLoading(pendingAsync.current > 0);
await fn(gameProps);
} finally {
pendingAsync.current -= 1;
GameStatus.setLoading(pendingAsync.current > 0);
if (pendingAsync.current === 0) {
gameProps.invalidateInterfaceData();
}
}
};
const handleWheel = useThrottler(
async (event: WheelEvent) => {
if (
!(
isInsideRoot(event.target, HTML_UI_LAYER_NAME) ||
isInsideRoot(event.target, HTML_CANVAS_LAYER_NAME)
)
)
return;
if (hasScrollableParent(event.target)) return;
// blocca lo scroll nativo
event.preventDefault();
const { deltaY } = event;
// ignora micro-movimenti del trackpad
if (Math.abs(deltaY) < minDelta) return;
if (deltaY < 0) {
// ⬆️ Scroll up
await runAsync(narration.continue.bind(narration));
}
if (deltaY > 0) {
// ⬇️ Scroll down
await runAsync(stepHistory.back.bind(stepHistory));
}
},
{ wait: throttleMs },
);
useEffect(() => {
window.addEventListener("wheel", handleWheel.maybeExecute, {
passive: false,
});
return () => {
window.removeEventListener("wheel", handleWheel.maybeExecute);
};
}, [handleWheel.maybeExecute]);
return null;
}