Loading assets
Learn how to load and manage assets in Pixi’VN using aliases, bundles, and different loading strategies to keep your game smooth.
Especially for games with online assets, following good loading practices is essential to avoid long wait times and keep the experience smooth for the player.
To load and manipulate assets (images, gifs, videos, etc.) you will need to use Assets. Assets is a class with many features and comes from the PixiJS library. For more information, read here.
Use aliases to load assets
To load an asset, always use its alias — the unique identifier defined in the manifest or when registering the asset. Using the src path directly is strongly discouraged: it couples your code to the file location, making it fragile when paths or hosting change.
import { Assets } from "@drincs/pixi-vn";
const texture = await Assets.load("eggHead");{
"bundles": [
{
"name": "default",
"assets": [
{
"alias": "eggHead",
"src": "./assets/eggHead.png"
}
]
}
]
}Organize assets into bundles
A best practice is to group assets into bundles rather than registering them individually. Bundles allow you to load a whole set of assets with a single call, and to defer loading until the assets are actually needed.
For local assets, naming bundles based on where they are used is less critical — since local files load instantly, there is no real benefit in splitting them by screen or label. This convention is most valuable for online assets, where targeted loading directly reduces wait times.
It is recommended to name each bundle based on where it will be used. For example:
- use the label's id (e.g.
startLabel.id) as the bundle name for assets used in that specific label - use the route path (e.g.
"/") as the bundle name for assets used in the corresponding screen, such as the main menu
This way, you can load exactly the right assets at the right moment, without loading more than necessary.
import { startLabel } from "@/content/labels/start.label";
import type { FileRouteTypes } from "@/routeTree.gen";
import type { AssetsManifest } from "@drincs/pixi-vn";
export const manifest: AssetsManifest = {
bundles: [
// screens
{
// main menu
name: "/" as FileRouteTypes["fullPaths"],
assets: [
{
alias: "background_main_menu",
src: "https://raw.githubusercontent.com/user/project/refs/heads/main/main-menu.png",
},
],
},
// labels
{
name: startLabel.id,
assets: [
{
alias: "bg01-hallway",
src: "https://raw.githubusercontent.com/user/project/refs/heads/main/breakdown/bg01-hallway.webp",
},
],
},
],
};When to load assets
By default, assets are loaded on demand — only when they are actually needed. However, loading at the last moment can cause noticeable pauses during gameplay.
Here are the most common and recommended moments to load assets in advance:
It is possible to load assets at project startup before the player can interact with the project, for example during the startup loading screen. It is suggested to use this procedure only for assets used in the main page or for assets used frequently, and not to exceed 100MB.
To do this, you must use the Assets.load function and wait for it to finish (with await) when the project starts.
import { manifest } from "@/assets";
import { Assets, sound } from "@drincs/pixi-vn";
let assetsInitialized = false;
/**
* Define all the assets that will be used in the game.
* This function will be called before the game starts.
*/
export async function defineAssets() {
if (!assetsInitialized) {
const origin = `${location.protocol}//${location.host}/`;
Assets.resolver.rootPath = origin;
await Assets.init({ manifest, basePath: `${origin}assets/` });
assetsInitialized = true;
}
// The game will not start until these asserts are loaded.
await Assets.loadBundle("base");
}It is possible to load assets in the background when the project starts, reducing loading times when your project starts. It is recommended not to exceed 2GB.
To do this, you can use the Assets.backgroundLoad function when the project starts. You can learn more about the Assets.backgroundLoad function here.
import { manifest } from "@/assets";
import { Assets, sound } from "@drincs/pixi-vn";
let assetsInitialized = false;
/**
* Define all the assets that will be used in the game.
* This function will be called before the game starts.
*/
export async function defineAssets() {
if (!assetsInitialized) {
const origin = `${location.protocol}//${location.host}/`;
Assets.resolver.rootPath = origin;
await Assets.init({ manifest, basePath: `${origin}assets/` });
assetsInitialized = true;
}
// The game will start immediately, but these assets will be loaded in the background.
Assets.backgroundLoadBundle(["audio"]);
}ink
You can use this method with the ink syntax. See more here.
To group the loadings from one step to another of a label into a single loading, it is possible to load all or part of the assets used by the label before it starts. Since this does not reduce the waiting times for the player, but adds them to a single loading, it is recommended to load them in the background.
To do this, you must use the Assets.load function and wait for it to finish (with await) when the label starts. You will use the onLoadingLabel function.
For cleaner code, it is recommended to define a bundle in your manifest with the id corresponding to the label id and define within it the assets that will be used in that label.
import { newLabel, showImage, Assets } from "@drincs/pixi-vn";
export const startLabel = newLabel(
"start",
[
() => {
await showImage("eggHead");
},
() => {
await showImage("flowerTop");
},
],
{
onLoadingLabel: async (_stepId, label) => {
// The label will not start until these assets are loaded.
await Assets.loadBundle(label.id);
},
);Templates
In all templates, when a label is started, the background loading of the bundle (if it exists) with the id corresponding to the label will be started. So, it is enough to create a manifest with a bundle for each label.
To make the game smoother by trying to remove asset loading times from one step to another, it is possible to start a "loading group" at the beginning of a label. This means that you may potentially not feel any loading, especially in the later steps of the label.
To do this, you can use the Assets.backgroundLoad function when the label starts. You can learn more about the Assets.backgroundLoad function here. And, you will use the onLoadingLabel function.
For cleaner code, it is recommended to define a bundle in your manifest with the id corresponding to the label id and define within it the assets that will be used in that label.
import { newLabel, showImage, Assets } from "@drincs/pixi-vn";
export const startLabel = newLabel(
"start",
[
() => {
await showImage("eggHead");
},
() => {
await showImage("flowerTop");
},
],
{
onLoadingLabel: async (_stepId, label) => {
// The label will start immediately, but these assets will be loaded in the background.
Assets.backgroundLoadBundle(label.id);
},
},
);It is possible to load assets before a screen is shown. This is useful when a screen uses images or other resources that must be ready before the component renders.
In this example, TanStack Router is used, which is already included in all templates. You can find more information about it here.
The loader function runs before the route component mounts. You can call Assets.loadBundle inside loader to preload the bundle associated with that route.
Additionally, you can use pendingComponent to display a loading indicator while the assets are being fetched, so the user sees a meaningful transition instead of a blank screen.
import { NarrationPendingComponent } from "@/components/loading";
import { NarrationScreen } from "@/components/screens/narration";
import { Assets } from "@drincs/pixi-vn";
import { createFileRoute } from "@tanstack/react-router";
export const Route = createFileRoute("/game/narration")({
component: NarrationElement,
pendingComponent: NarrationPendingComponent,
loader: async () => {
await Assets.loadBundle("/game/narration");
},
});
function NarrationElement() {
return <NarrationScreen />;
}