[SOLVED] Switching scenes via button

So I’m making a game for the Mini Jam(Game Jam) on itch.io, and I have a start menu, but I can’t find out how to switch scenes with a button, project link here:
PlayCanvas | HTML5 Game Engine

1 Like

Hi @Nicholas_Taylor,

Here is an example that does that:

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

1 Like

Alright, I’ll Try


what do I do here?

oh wait I think I found the problem

I did

how do I make it disapear when loaded into a scene?

Hi @Nicholas_Taylor,

My Scene Manager project might be able to help you out:

https://playcanvas.com/project/756176/overview/scene-manager

It destroys the scene you are looking at when a button is clicked and loads the new one, based on the event fired by the button.

I hope this is helpful.

2 Likes

ok, i will try, thanks!

Hi @Nicholas_Taylor ,

I’ve recently had a similar problem myself. Here’s what I have mostly done:

First, you can preload the scene (which was efficient for my project at least). You need an attribute with the exact name of the scene you want to load. And in the initialize function, you preload the scene.

NextScene.attributes.add("sceneName", {
    type: "string",
    default: ""
});

// initialize code called once per entity
NextScene.prototype.initialize = function() {
    this.scene = this.app.scenes.find(this.sceneName);
    this.app.scenes.loadSceneData(this.scene, function(err, sceneItem) {
        if (err) {
            console.error(err);
        }
    });
};

After the input of the player (which I haven’t programmed; it’s on you), you destroy the current scene and replace it with the next one like:

 this.app.root.findByName("Root").destroy(); // I don't know if you can leave out the findByName part but let's keep it here for safety reasons

 this.app.scenes.loadSceneHierarchy(this.scene.url, function (err, scene) {
        if (err){
            console.log(err);
        }
        });

        this.app.scenes.loadSceneSettings(this.scene.url, function (err, scene) {
            if (err){
                console.log(err);
            }
        });

And this is pretty much it. I hope this was helpful for you

1 Like

so for whatever reason, this.app.root.findByName it says that root, isn’t defined

Oh ok,

then you should create an entity attribute and then access the root

Ok, ill try

so would it be:
NextScene.attributes.add("Root", { type: "Entity", default: "" });?



Screenshot 2022-03-02 1.07.37 PM

what do I do?

Hi @Nicholas_Taylor! Your code is outside a function and will not work at that place of the script. As @SayHiToMePls mentioned, that part of the code need to be executed after the user input (button click). I suggest to look at the project of @eproasim, because that is a working example.

1 Like

alright, ill check it out

oh, ok so, I tried using that but, it makes my pages not run and I cant do anything, my game is unplayable, it won’t load

i tried it yesterday, its working now because i got rid of it