How to restart a scene in babylon.js? "RESTART LEVEL"

How to restart a scene in babylon.js?
Hello everyone, I’m doing a practice with babylon.js and I can’t find a way to restart a scene.
That is, in godot Engine or unit it is quite simple since there are functions that allow you to restart a scene easily

In the case of Godot take the hierarchy and restart the nodes
Captura de pantalla_2020-06-05_10-58-06

In the case of unity it has a function called scene manager that allows you to “restart” or change the scenes using a scene as a reference.

Is there a simple way to restart or change scenes in babylon.js ?.

use a public variable called game over and try this but it doesn’t work

window.addEventListener("click", function () {
    //si es GameOver
    if(gameOver)
    {
        console.log("tendria que reiniciar la escena")
        gameOver = false;
        
    }
});


/////////////////// Init game ////////////////////////////
var scene = createScene(); //Call the createScene function

// Register a render loop to repeatedly render the scene
engine.runRenderLoop(function () {
        //
        if(gameOver)
        {
            //console.log("tendria que reiniciar la escena")
            scene.dispose()
        }
        else
        {
            scene.render();
        }
});

hello why not disposing and calling createScene again?

And how do I do that, do you have a simple example?
There is no information or examples of restarting scenes in the documentation.

I needed someone’s advice since I was trying to delete and remove parts of the scene using the assetsContainer, but it was very difficult for me, especially since sometimes I couldn’t save the objects in the scene, among other things.
At least in this way it is quite simple to create a restart of scenes, although sometimes the blue screen remains.
Thank you so much
I leave the example of the code.

/////////////////// Init game ////////////////////////////
var sceneCreate = createScene(); //Call the createScene function

window.addEventListener("click", function () { //si presiono click
    //si es GameOver
    if(gameOver)
    {
        gameOver = false//hago que game over sea falso
        sceneCreate = createScene();//IMPORTANT I LOAD THE SCENE IN THE VARIABLE AGAIN
    }
});

// Register a render loop to repeatedly render the scene
engine.runRenderLoop(function () {//loop de engine  
        sceneCreate.render();//renderizo escena
});

//////////////////////////////////////////////////////////////

Video showing how it works

This is great! And always better when you find it by yourself :slight_smile:
Thanks for sharing the code!

1 Like