Clarification on Loading Scenes

I’m currently looking into scene loading again and after looking through the source code on github I now have a few questions:

First: The implementation of both pc.app.scenes.loadSceneHierarchy and pc.app.scenes.loadSceneSettings suggests that we need to pass a SceneRegistryItem in order to load the hierarchy or the settings respectively. But actually we need to pass the url of the SceneRegistryItem instead. Passing the object itself will result in an error and the scene cannot be loaded. Here I’m just confused as to why that is?

Second: I also stumbled upon pc.app.scenes.loadScene which I didn’t know about until now. When we currently change to a different scene, we always do something like this:

var oldHierarchy = pc.app.root.children[0];
var scene = pc.app.scenes.find(sceneName);
pc.app.scenes.loadSceneSettings(scene.url, (err) => {
  if (!err) {
    pc.app.scenes.loadSceneHierarchy(scene.url, (err, parent: pc.Entity) => {
      if (!err) {
        oldHierarchy.destroy();
      } else {
        console.error(err);
      }
    })
  }
});

Is this interchangeable, can I just change the code to something like the following to make sure that the new scene is fully loaded?

var oldHierarchy = pc.app.root.children[0];
var scene = pc.app.scenes.find(sceneName);
pc.app.scenes.loadScene(scene.url, (err) => {
  if (!err) {
    oldHierarchy.destroy();
  } else {
    console.error(err);
  }
});

I’m asking because the description of this function specifically says that this is an internal function, but doesn’t describe it as a private function.

I hope you can enlighten me on this :slight_smile:

Hi @AliMoe,

Actually that’s a good question. I’ve been using myself app.scenes.loadScene() for a while. The comment indeed points that this is an internal method, though I feel that’s an older comment?

I think there has been a motion to move most of the scene loading methods from pc.Application to the pc.SceneRegistry.

@yaustar may know more on this.

Master is showing 1.40.0-dev release where there’s a refactor on the API.

1.39.X release (which is what we are on now) is still expecting an URL to the scene JSON file.

Do not use loadScene if possible. It was made public for engine only users and requires private API usage to work correctly. The new API in 1.40.0 will allow you to preload and cache scene JSON data to avoid further HTTP requests on calls to loadSceneHiearchary and loadSceneSettings.

1 Like

Ok, so it’s best if we keep out current process of loading a new scene, right? Will we have to do the scene caching manually, or will it be handled automatically?

And as for the next release, we will have to change our parameters as soon as 1.40 is released? Will this release break our current system, or will it be backwards compatible for a little while?

Thanks for the quick answers

It will have to be manual as we didn’t want to change existing behaviour of the current API.

For scene loading, there are no breaking API changes for 1.40.0. loadSceneHiearchary will accept both a URL string or a SceneRegistryItem :slight_smile:

If you are comfortable using private API, you can use loadScene if you wish. We have an example here (that will need changing once 1.40.0 is released) that shows how to use it: https://developer.playcanvas.com/en/tutorials/switch-full-scene/

People use it for the reason you found where both settings/hierarchy make separate HTTP calls and is therefore ‘slow’.

1 Like

Thanks for the clarification, we will take a look at this again, once 1.40 hits us :slight_smile:

1 Like