Using loadSceneData sometimes starts random scene in editor

I’m trying to implement a custom loading solution for a multi scene application.

First thing I’m doing is caching all scene files with loadSceneData and parsing assets needed for the scene:

pc.app.scenes.list().forEach((sceneRegistryItem) => {
    pc.app.scenes.loadSceneData(sceneRegistryItem, (err, sceneRegistryItem) => {
        if (err) {
            console.error(err);
        }
        window.assets[sceneRegistryItem.name] = [];
        window._parseSceneAssets(sceneRegistryItem.name, sceneRegistryItem.data);
    });
});

Sometimes this has the effect of loading another scene than the scene I pressed play in the editor.
I’m guessing this is some kind of race condition, as I couldn’t find a way to consistently reproduce this.

Steps to reproduce:

  1. Goto Testproject
  2. Launch scene Blue
  3. Press F5 until different scene is loaded (Cubes are color coded with scene name)

For me it did occur more often, when I have dev tools opened.

I also have a different project, where the bug occurs more often. I think it has to do with more assets needing to load/custom load script. However it’s a private project. I can add someone tho to get access if needed.

EDIT: Does not seem to happen, when downloaded as zip and opened locally.

Hi @LucaHeft

This seems to help

pc.app.on('start', () => {

    pc.app.scenes.list().forEach((sceneRegistryItem) => {
        pc.app.scenes.loadSceneData(sceneRegistryItem, (err, sceneRegistryItem) => {
            if (err) {
                console.error(err);
            }
            window.assets[sceneRegistryItem.name] = [];
            window._parseSceneAssets(sceneRegistryItem.name, sceneRegistryItem.data);
        });
    });

}, this );
1 Like

Yep that seems to work. Altough this will delay execution to after the first scene is loaded. I’m fine with that for now.

I would like to actually run all this and wait for it to finish, before the first scene is loaded. However I think I would need to edit __start__.js script as this loads the first scene.
Which would work for downloaded builds, but not launch mode in the editor.

You can patch the engine to alter to startup to include your asset loader, but it is much easier to have a dummy startup scene where you can parse all the scenes and then load the next scenes assets and then automatically switch to the real first scene after you’ve loaded the assets,

Yes I guess this is the best solution. Thanks for the intel!

Here is an example.

1 Like