Construct 3 icon

Construct 3

Documentation

AssetManager script interface

Ashley's avatar
Medal
Construct Team Founder
Published 3 Jun, 2019
568 words
~2-4 mins

The IAssetManager interface provides access to the project's assets, such as audio files and other project files added to your project.

In general project files simply cannot be fetched by their URL, because in preview mode they are only stored locally, and some platforms like Cordova have limitations on being able to fetch URLs so need to use different approaches to read files. The Asset Manager handles all of these details for you, so you should always fetch resources using this class instead of other methods like fetch() or XMLHttpRequest.

AssetManager APIs

async fetchText(url)

async fetchJson(url)

async fetchBlob(url)

async fetchArrayBuffer(url)

Retrieve the contents of a given URL as a string, JSON object, Blob or ArrayBuffer. These methods work cross-platform, including in preview mode, where methods like fetch and XMLHttpRequest will fail, so these methods should always be preferred for fetching resources. Returns a promise that resolves when the resource has been loaded.

async getProjectFileUrl(url)

Retrieve a URL that can be fetched directly for a given resource. Returns a promise that resolves to a string with a URL that may be the same as the original URL, or a different URL (e.g. blob: URL) if direct fetching is not supported. This is intended for using with local files where the other fetch methods are not appropriate, such as assigning the src attribute of a video.

async getMediaFileUrl(url)

As with getProjectFileUrl but for sound and music files, which are exported to a media subfolder.

mediaFolder

A string of the subfolder media files are in, including sound and music files. In preview this is an empty string, and after export it is the media subfolder followed by a forward slash, e.g. "media/".

isWebMOpusSupported

A boolean indicating if the current browser/platform has built-in support for playing WebM Opus files (the default format encoded by Construct). If true then the <audio> tag and decodeAudioData can be assumed to support WebM Opus files. If false you can switch to using decodeWebMOpus() to use Construct's WebM Opus decoder instead. See the Audio scripting example for a demonstration.

async decodeWebMOpus(audioContext, arrayBuffer)

This is designed as a drop-in replacement for Web Audio's decodeAudioData for platforms that do not have built-in support for WebM Opus. In this case Construct provides its own WebM Opus decoder as a fallback. It can only be used when isWebMOpusSupported is false; when it is true this method throws an exception since you should use the built-in methods instead. Pass an AudioContext and ArrayBuffer of the WebM Opus data to decode. This returns a promise that resolves to an AudioBuffer of the decoded audio that can be directly played. See the Audio scripting example for a demonstration.

async loadScripts(...urls)

Fetch and run the JavaScript files at the given URLs. This can load scripts in the Files folder of the Project Bar, which unlike scripts in the Scripts folder are not automatically loaded by Construct.

async compileWebAssembly(url)

Fetch and compile a WebAssembly.Module from the given URL, which is typically a .wasm file. This uses streaming compilation where supported. Note this does not instantiate the module, which needs to be done before any calls can be made. Pass the module resulting from this call to WebAssembly.instantiate() to get a WebAssembly.Instance from the module.