AssetManager script interface

The IAssetManager interface provides access to the project's assets, such as audio files and other project files added to your project. It is typically accessed via runtime.assets.

On most modern platforms, assets can be directly retrieved as if over the network with standard APIs like fetch() or XMLHttpRequest, even in non-web export platforms like mobile or desktop apps. However in some circumstances, generally with unusual export options like Playable Ads or with legacy projects, it may be necessary to use Construct's IAssetManager interface to fetch resources, as it implements workarounds that prevent standard fetches working in those environments.

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. 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, none of which are automatically loaded by Construct. When loading multiple scripts, they will run in the order they are provided, e.g. loadScripts("script1.js", "script2.js") will always run script1.js first and script2.js second. For best efficiency, try to load all the scripts you need in a single call, rather than repeated calls.
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.
async loadStyleSheet(url)
Fetch a stylesheet at the given URL and attach it to the current document, applying its styles. Returns a Promise that resolves when the stylesheet has been applied to the document.
Construct Animate Manual 2024-02-15

On this page