[SOLVED] Scene Loading with persistent entities

I’m trying to create scene loading (elements under ‘sceneContent’ while keep some persistent elements (elements under “static content”, a ui for for example). My current way is to load the new root element and then move the static content into this new structure, before destroying the old “Root”.
Is this a good solution or are there simpler / better ways of doing that?
Thanks for any heads up!

SceneManager.prototype.loadScene = function (sceneName) {
    if(sceneName != this.currentScene){
        this.currentScene = sceneName;
        
        // Get a reference to the scene's root object
        var oldHierarchy = this.app.root.findByName ('Root');
        var staticContent = oldHierarchy.findByName('staticContent');

        // Get the path to the scene
        var scene = this.app.scenes.find(sceneName);

        // Load the scenes entity hierarchy
        this.app.scenes.loadScene(scene.url, function (err, scene) {
            oldHierarchy.removeChild(staticContent);
            oldHierarchy.destroy();
            
            var newHierarchy = this.app.root.findByName ('Root');
            newHierarchy.addChild(staticContent);
            pc.ComponentSystem.initialize(this.app.root.findByName ('sceneContent'));
            pc.ComponentSystem.postInitialize(this.app.root.findByName ('sceneContent'));
            
        });
    }    
};

I would load additively rather than do a full hierarchy switch.

Eg https://developer.playcanvas.com/en/tutorials/additive-loading-scenes/

Alternatively, you can move the persistent entities to be on app.root rather than the root entity of the scene.

Either way, they both amount to the same layout more or less.

1 Like

Thanks for the info. Moving the static entitys up to app.root makes the most sense for my usecase. Good to know this workflow.

There’s a small gotcha in that that you should be aware of. Once those entities have moved to app.root, you cannot load that scene that had those entities originally again.

This is because they have a unique GUID in the scene data and loading two entities with the scene GUID will cause issues due to the way the data is structured and stored in the engine.

Thanks for the info. I think I encountered some of those issues earlier, when I wrapped my my head around how the pc scene loading actually works (I think actually it was your response in a different post which explained that it’s not a real scene change like in UE or else). For my current setup (having one separate “Loader Scene” and multiple “Content Scenes” it works, which is probably a good structure anyway when you want to have “persistent” elements.