Assets
An asset is a reusable entry in the assets section of the configuration.
Each asset has an id, a name, a kind and data:
{ "id": "6b1e…", "name": "hero", "kind": "texture", "data": { "src": "images/hero.png" }}Kinds of assets
Section titled “Kinds of assets”The kind names the class that describes the asset. There are two families of kinds.
- Media kinds point to a file. The engine has three:
texture,audioandbitmapFont. - Data kinds hold only fields. Use them for things that are not actors, such as items, quests or lines of dialogue.
Loading
Section titled “Loading”Only media assets load. A data asset is ready when the game starts.
Put an asset id in an asset field and the engine
loads it for you. It loads the files before the scene starts. It unloads them when it destroys
the scene. The assets in the options of a world system stay loaded until the game ends.
A scene also loads the assets of the templates it reaches. These are the templates of its actors
and the templates in template fields, and then their templates in turn. So an actor that a
system spawns from such a template has its files ready. A template that code spawns only by an
id in the code is not loaded in advance.
Bitmap fonts stay loaded until the game ends.
An asset field is a field that the editor shows as a list of the project’s assets. The
built-in components already have them, such as the texture of a sprite. In your own component,
behavior or system, declare one with @DefineField:
@DefineField({ type: 'asset', kind: 'audio' }) hitSound!: string;See inspector fields for the other options.
If your code uses an id that no field holds, load the asset yourself. See Loading assets from code.
Reading an asset in code
Section titled “Reading an asset in code”Game code gets the project’s assets as assets. Ask it for an asset by its class and id:
import { Texture } from 'dacha';
const texture = this.assets.get(Texture, sprite.texture);texture?.resource; // the loaded texture, or undefinedA data asset has its fields right away. A media asset gives its file as resource. It is
undefined until the file loads.
Your own kinds
Section titled “Your own kinds”A data kind extends Asset. Describe its fields with @DefineField, as you do in a component:
import { Asset, DefineAsset, DefineField } from 'dacha';
@DefineAsset({ name: 'item' })export default class Item extends Asset { @DefineField() title!: string; @DefineField({ initialValue: 1 }) maxStack!: number; @DefineField({ type: 'asset', kind: 'texture' }) icon!: string;}A media kind extends MediaAsset. It fetches its file and returns the resource in
fetchResource. If the resource needs cleanup, free it in disposeResource:
import { MediaAsset, DefineAsset, DefineField } from 'dacha';
@DefineAsset({ name: 'levelMap' })export default class LevelMap extends MediaAsset<LevelData> { @DefineField({ type: 'file', extensions: ['json'] }) src!: string;
protected async fetchResource(): Promise<LevelData> { const response = await fetch(this.src); return response.json(); }}Pass your kinds to the engine in assets, next to the built-in ones. The editor finds them in
files that end with .asset.ts. See auto-registration.
Loading assets from code
Section titled “Loading assets from code”You need this section only when your code uses an asset id or a template id directly. If the id comes from a field, the engine loads it for you.
The engine does not know about such an id in advance. It loads the asset when an actor first needs it, and unloads it with the scene of that actor. Until the file arrives, the sprite draws nothing and the sound does not play.
To have the files ready in time, call acquire with an owner and the ids. acquire resolves
when the files are ready. Call release with the same owner when you no longer need them. The
engine unloads an asset when nothing holds it. This scene system holds the assets for its scene:
async onSceneLoad() { await this.assets.acquire(this, { assets: [cutsceneId], templates: [bossId] });}
onSceneDestroy() { this.assets.release(this);}The owner is an object, such as a system or a behavior, or a string.
A second acquire with the same owner replaces what the owner holds. To keep what the owner
holds and add to it, pass add: true:
await this.assets.acquire(this, { templates: [nextWaveId] }, { add: true });A world system lives across scenes. In a world system, use a separate owner key for each
scene, not this.
acquire also takes onProgress. It gets total, completed and failed for the files of
this call.
ensure starts the load and does not wait.
It does nothing when the scene already holds the asset, so you can call
it every frame:
const map = this.assets.get(LevelMap, minimap.map);if (map && !map.resource) { this.assets.ensure(this.scene, minimap.map);}Loading progress
Section titled “Loading progress”While a scene loads, the world dispatches SceneLoadProgress.
Each event has three numbers:
| Field | Meaning |
|---|---|
total |
How many steps the load has |
completed |
How many steps have finished |
failed |
How many files did not load |
The world dispatches this event immediately. So an interface gets it while the first scene loads:
import { SceneLoadProgress } from 'dacha/events';
context.on(SceneLoadProgress, ({ completed, total }) => { bar.style.width = `${total ? (completed / total) * 100 : 100}%`;});