Changing scenes on published builds has error from 1.40.0 onwards

Change same scene is not working on exported build…
Using above method…

My few games started facing issue because of this now… while exporting…

The same method works fine while online debugging…
How i can solve this issue?
Any one can help?

Edit:

After analysing old exported builds…
I came to know that Above feature was working fine on or before this file playcanvas-stable.min.js (From exported version)

/**
 * @license
 * PlayCanvas Engine v1.39.4 revision 5653326
 * Copyright 2011-2021 PlayCanvas Ltd. All rights reserved.
 */

@yaustar
I am not able to find any solution right now and update release is stuck due to this…

Unfortunately i have to use playcanvas-stable.min.js file from v1.39.4 to make my Exported game work…

That’s a tough one, we would need a reproducible project to work with to see what the issue is.

It looks like it could be related to this change I made to add Scene data caching API: https://github.com/playcanvas/engine/pull/2623 but I’m not aware of it changing existing behaviour

But I would need a reproducible project to be sure.

@yaustar

Here is the Fork project of that game, I removed everything related to game…
(Click on red button to change same scene…)

https://playcanvas.com/project/803195/overview/bug-change-scene-in-export

The above build works fine online in debug mode…
But it does not work when you export it and click on red button…

1 Like

Thanks, I will take a look at that later today

1 Like

I’m still having trouble trying to narrow down the exact issue but looking at the game’s loading code, there’s some parts that make me think that it is used to work/working with launcher by luck.

In changeSceneScript.js

ChangeSceneScript.prototype.changeScene = function() {
    
    try
    {
        var oldHierarchy =  this.app.root.findByName("Root");// this.app.root.findByName('Root');
        
        setTimeout(function(){
            oldHierarchy.destroy();
            this.app.root.destroy();
        }.bind(this));
    }
    catch(e)
    {
        console.log(e);
    }
};

Don’t destroy the app.root, that should never be destroyed as that’s the engine’s graph root node

So change it to the following:

ChangeSceneScript.prototype.changeScene = function() { 
    try
    {
        var oldHierarchy =  this.app.root.findByName("Root");// this.app.root.findByName('Root');
        
        setTimeout(function(){
            oldHierarchy.destroy();
        }.bind(this));
    }
    catch(e)
    {
        console.log(e);
    }
};

On the initialise function

ChangeSceneScript.prototype.initialize = function() {
    
    // this.sceneManagerEntity = this.app.root.findByName('sceneManager');
    
    setTimeout(function(){

        this.changeSceneBtn.element.on('click', function (evt) {
            setTimeout(function(){
                this.changeScene();
            }.bind(this),10);
        }, this);
        
        console.log("sceneManagerEntity = ", this.sceneManagerEntity);


        this.on('destroy', function() {

            this.loadScene (this.sceneManagerEntity.script.sceneManagerScript.sceneID[0], function () {
                // this.app.addTweenManager();  // make sure it will not going to affect the animation speed
            }.bind(this));

        }.bind(this));
    }.bind(this),500);
    
};

It is loading the new scene from the destroy callback and it’s dependent on a reference and data outside the script instance. However, that data and entity may already be destroyed before this callback is triggered.

It’s better to cache the data needed and load the scene after the old hierarchy is destroyed.

Here’s a fixed fork: https://playcanvas.com/editor/scene/1174122

1 Like

@yaustar Thank you very much for the quick solution! :smiley: :love_you_gesture:t2: :star:

Actually, old script was working till the point… So never thought of changing this way…

This new build works perfect!
Also it will help others as well…