Modules

Assets

moduleextensionSource

Available on macOS, Linux, Windows, Wasm·since 0.31.0

#import "Extensions/Assets";

Load a file a program ships with (a texture, a sound, a level) by a relative path, from disk natively and over fetch() in the browser playground.

Example↗
#import "Extensions/Assets";

bytes, ok := load_asset("assets/texture.png");
defer array_free(bytes);

Natively the path is tried as given (from the working directory), then next to the executable. In the browser the page answers jai_asset_fetch by fetching the path relative to the asset base URL it was set up with; without that host function (a page with no asset base, a WASI command) the path is read from the program's file system instead. The bytes are allocated with context.allocator.

Procedures#

(path: string) -> bool

A relative path that stays inside the asset folder: no leading '/', no drive or URL scheme, no '..' component and no backslashes. load_asset refuses anything else on every platform, so a program behaves the same natively and in the browser.