After Publishing Won't Open Next Scene

I just finished making a working game. From the editor the scenes will change just fine. After I published the game the scenes no longer change and I get the image below

The code for my scene change is below:

var StartGame = pc.createScript('startGame');

// initialize code called once per entity
StartGame.prototype.initialize = function() {
 
};

// update code called every frame
StartGame.prototype.update = function(dt) {
if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {

window.open("https://launch.playcanvas.com/1613875","_self");

}
};

I used the ID number to switch each scene if that helps, I checked the ID numbers and they match up.

The link to the project after publish

The link to project main menu scene in editor
https://playcanvas.com/editor/scene/1613559

@Fabian_S I am not very familiar with the method that you are using for switching scenes. My assumption is that it may require some type of secure sign on and that is why it is throwing this error requiring this. Probably works in your local play in the editor because of this. When you actually publish to playcanvas your build is hosted on a server and no longer has a link to your identity. I would suggest using this method of switching scenes by name.

https://developer.playcanvas.com/en/user-manual/packs/loading-scenes/

I am sure a developer can offer more information. Here is a script I use often to switch screens.

class ChangeSceneOnClick extends pc.ScriptType {
    // initialize code called once per entity
    initialize() {
        if (this.preloadSceneData) {
            this.app.scenes.loadSceneData(this.app.scenes.find(this.sceneName), function (err) {
                if (err) {
                    console.warn(err);
                }
            });
        }

        this.entity.button.once('click', function () {
            loadScene(this.sceneName, { hierarchy: true, settings: true });
        }, this);
    }
}

pc.registerScript(ChangeSceneOnClick, 'changeSceneOnClick');
ChangeSceneOnClick.attributes.add('sceneName', {type: 'string'});
ChangeSceneOnClick.attributes.add('preloadSceneData', {type: 'boolean'});

@Tirk182 Unfortunately this is how my game design course has told me to input this scene change and I can’t have it change with a click, I have to have it changed by pressing a key. I appreciate the input however I am unable to use this. If I was to use this code I wouldn’t be sure how to input the next scene and the credit scene as well due to my very limited knowledge in Javascript and needing the ability to explain my scripts in my course.

@Fabian_S I understand. This is just a method of using an on screen button. You can use the the same method you are using to control your space ship. Map the same methods to your update function and monitor for the Space key to hit.

if (this.app.keyboard.wasPressed(pc.KEY_SPACE)) {

   One Way>>>>>>>>>> loadScene('Level 1', { hierarchy: true, settings: true }); 
Second Way>>>>>>>>>>>this.app.scenes.changeScene('Level 1'); <<like example

}

You will need to populate the initialize to support preloading if required. More of this is explained in the documentation link above. You will be able to find the load_scene_helper.js in the documents. Here is the source.

/**
     * @name loadScene
     * @function
     * @description Loads a scene hierarchy and settings depending on the options.
     * @param {string} sceneName - Name of the scene to load.
     * @param {Object} [options] - Optional. Extra options to do extra processing on the GLB.
     * @param {boolean} [options.hierarchy] - Optional. Set to true if you want to load the scene hierarchy.
     * @param {boolean} [options.settings] - Optional. Set to true if you want to load the scene settings.
     * @param {pc.callbacks.LoadHierarchy} [callback] - Optional. This is called if there is an error or if the scene is loaded successfully.
     * @param {Object} [scope] - The object scope to call the callback with.
     */

function loadScene(sceneName, options, callback, scope) {
    var app = pc.Application.getApplication();
    var scene = app.scenes.find(sceneName);

    if (scene) {
        // Check if the scene data is already loaded, if it is we should assume
        // that it stay cached after we loaded the scene and added the 
        // entities to the scene graph
        var wasSceneLoaded = scene.loaded;

        app.scenes.loadSceneData(scene, function(err, scene) {
            if (err) {
                if (callback) {
                    callback.call(scope, err);
                }
            } else {
                var sceneParent = null;

                // Destroy all the entities on the app.root to completely remove 
                // the existing scenes
                var rootChildren = app.root.children;
                while(rootChildren.length > 0) {
                    rootChildren[0].destroy();
                }

                // As we've already loaded the scene data, the callbacks for these
                // functions will be called immediately
                if (options.settings) {
                    app.scenes.loadSceneSettings(scene, function (err) {
                        if (err && callback) {
                            callback.call(scope, err);
                        }
                    });
                }

                if (options.hierarchy) {
                    app.scenes.loadSceneHierarchy(scene, function (err, parent) {
                        if (err) {
                            if (callback) {
                                callback(err);
                            }
                        } else {
                            sceneParent = parent;
                        }
                    });
                }

                if (!wasSceneLoaded) {
                    app.scenes.unloadSceneData(scene);
                }

                if (callback) {
                    callback.call(scope, null, sceneParent);
                }
            }
        });
    } else {
        if (callback) {
            callback.call(scope, "Scene not found: " + sceneName);
        }
    }
}

Although this did work when I used your 2nd example of javascript code, when my code to restart the scene restarted my level 1 scene, the scoring code I have, messed up. How would I fix this in order for the code to work efficiently with the others and with least amount of code?

@Fabian_S You can have a look to the follow link to a game that handles a game restart.

https://playcanvas.com/project/1002406/overview/halloween-jaunt--remix2

Here is the method I used. Again this functionality can easily be connected to a keyboard key rather than a button.

// Try again button
ButtonControl.prototype.tryAgain = function() {
    window.location.reload();  
};

This button control has not helped. now it doesn’t recognize the function and whenever I go to the main menu scene and play again the score messes up again.

@Fabian_S Sorry that there is a lot of confusion between buttons and Keyboard monitoring. Bottom line is that when you publish your game the reference to it on the web is no longer the same. So using

window.open("https://launch.playcanvas.com/1613875","_self");

will not be a valid link to what you expect. Here is some context on the method you are trying to use:

if (this.app.keyboard.wasPressed(pc.KEY_ANYKEY)) {
   window.location.reload(); 
}

I have looked at your code and have seen only one usage of a suggested solution provided. Also, links to documentation and tutorials have been provided to help improve understanding on how to handle scene switches and loading.

In what way did your scoring code mess up?

The code here is saying to open a web page with this URL. However, this URL is for the Editor launch and shouldn’t be used for published games for a number of reasons. If the course is asking you to do this, they have given incorrect information. (If the course leads would like to to talk to us, please ask them to email us at support@playcanvas.com)

As Tirk182 mentioned, to change screens, please use the methods shown in the developer documentation: https://developer.playcanvas.com/en/user-manual/packs/loading-scenes/

There are a number of examples there to help.

1 Like