Simple way to switch to new scenes, using scene name

I had thought that switching between different scenes in PlayCanvas would have been as easy as something like ( in Unity ) Application.LoadLevel(“nameOfScene”);

After some time searching through the forums, one solution was this…

  • Open the scene in PlayCanvas, and make a note of the number at the end of the URL ( eg the 404548 of https://playcanvas.com/editor/scene/404548 )
  • Add this code…
    app.root.destroy();
    app.loadSceneHierarchy(numberOfLevelToLoadFromAbove, function (err, parent) {
    // handle error here, if any
    });

If there was just an app.LoadLevel(actualNameOfLevel) that replaced the current running scene with that new scene, that would make things a lot more straightforward, esp for Jam type projects.

2 Likes

I’m not completely familiar with how scene loading in Unity works, but I believe they have a similar model, you either destroy and load a scene or load a scene and add it to the existing content.

In PlayCanvas:

app.loadSceneHierarchy("1000.json", function (err, entity) {
  // loaded...
});

Loads the Entities, but does not affect existing Entities.

If you want to remove the existing Entities you call entity.destroy() on the parent Entity you wish to remove. For example, app.root.destroy() will remove all Entities.

Hi Dave,

It looks like we’re doing the same thing regarding loading a new level ( 2nd paragragh of my first post mentions the same loadSceneHierarchy and app.root.destroy as you do ).

As a user mainly using the editor to create scenes, I would prefer to use the name I give to the scene ( eg “MainMenu” or “GameOverScene” ).

For the GameJam, it actually took me a while to figure out what the number was that precedes .json and where to get it ( I wasn’t paying attention to whatever was in the browser url field, I was looking through all of the scene properties within the editor to try to find that number ).

I suppose this suggestion is more for people who want to quickly jump between scenes based on the name they gave to the scene.

Eg app.loadSceneByName ( SceneName, optionToDestroyExistingScene = default to true, optionalFunctionToHandleError ).

This would effectively use SceneName to extract the number.json value and call loadSceneHierarchy in the background, and then use optionToDestroyExistingScene to call app.root.destroy on success ( a sort of handy built in wrapper ).

For the GameJam, I figured it would take about 5 minutes to code the switching between scenes logic - it ended up taking a lot longer to get it working ( searching on forums, googling, trying out loadSceneHierarchy and not being aware of how to get the number part ) so I’m hoping to make it easier for the next person.

1 Like

Hey, switching from Unity to test this online IDE, can you make a dummy example with this working, using the current API that is offered with Playcavas.com

Bumping this request/suggestion.

Scene-based gamedev in PlayCanvas has been working well for us so far, but the numbers-system is a bit clunky. Getting access to a dictionary somehow (pc.SceneList or something?) would be a huge help.

1 Like