Skip to content

Make app.assets available immediately after AppBase construction#8952

Merged
mvaligursky merged 1 commit into
mainfrom
mv-app-assets-early
Jun 23, 2026
Merged

Make app.assets available immediately after AppBase construction#8952
mvaligursky merged 1 commit into
mainfrom
mv-app-assets-early

Conversation

@mvaligursky

Copy link
Copy Markdown
Contributor

Fixes a discrepancy where app.assets was undefined when accessed before AppBase#init() — e.g. from a loading screen script (pc.script.createLoadingScreen) under WebGPU but not WebGL. Reported in the follow-up comment on #8932.

The app registers itself globally (via setApplication) in its constructor, but app.assets was only created in init(). WebGL's graphics device creation resolves synchronously (microtask), so init() runs before the loading screen script executes; WebGPU's device creation is genuinely async, so the loading screen script runs first — in the window where app.assets did not yet exist.

Changes:

  • Create the AssetRegistry as an AppBase class field (assets = new AssetRegistry(this.loader)) instead of in init(), so app.assets is available right after construction on all backends. init() continues to apply the asset prefix and build the bundle registry on the same instance (no re-creation, so any early listeners are preserved).

Note: this only makes the registry available earlier; resource handlers are still registered in init(), so attempting to actually load a device-dependent asset (e.g. a texture) before init() returns a graceful "No resource handler" error rather than a TypeError. Adding assets to the registry for later loading works as expected.

app.assets was created in init(), but the app registers itself globally
(setApplication) in the constructor. Code that runs between construction
and init() - such as a loading screen script via pc.script.createLoadingScreen,
which calls callback(getApplication()) - therefore saw app.assets as undefined.

This surfaced as a WebGL/WebGPU discrepancy: WebGL device creation resolves
synchronously (microtask), so init() runs before the loading screen script,
while WebGPU device creation is async, so the loading screen script runs first,
in the window where app.assets did not yet exist.

Create the AssetRegistry as a class field so app.assets is available right
after construction on all backends. init() still applies the asset prefix and
builds the bundle registry on the same instance, so early listeners are kept.
@github-actions

Copy link
Copy Markdown

Build size report

This PR changes the size of the minified bundles.

Bundle Minified Gzip Brotli
playcanvas.min.js 2261.1 KB (−0.0 KB, −0.00%) 581.7 KB (−0.0 KB, −0.00%) 452.0 KB (−0.4 KB, −0.08%)
playcanvas.min.mjs 2258.5 KB (−0.0 KB, −0.00%) 580.8 KB (−0.0 KB, −0.00%) 451.7 KB (+0.0 KB, +0.01%)

@mvaligursky mvaligursky merged commit a629faa into main Jun 23, 2026
10 checks passed
@mvaligursky mvaligursky deleted the mv-app-assets-early branch June 23, 2026 10:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant