[SOLVED] How to switch between scenes?

Hi, guys!
I’m making a game prototype, where i want to go to game scene on button click in menu, then in game scene when you die, game should change current scene back to menu. How can i do it from script (and not by using pc.Application.loadSceneHierarchy())? :smile:

You can simply have two entities, one with level and another with menu.
And have another entity with script that will enable/disable menu or level entity.

i cannot do that, @max - i mean: because i have to make an ability to reset game scene to it’s default state, i have to do a template of given scene and ‘instantiate’ it on scene, so every script entity attrbutes bindings will be pointing to template objects (UIDs case), and because of that i will have to change all these bindings at runtime on initialize(). it can be done, but it’s a waste of time.
Ok, let’s say that i will use pc.Application.loadSceneHierarchy() - how can i get UID (the number) of given scene in editor (or maybe even at runtime, by it’s name)?

http://developer.playcanvas.com/en/engine/api/stable/symbols/pc.Application.html#loadSceneHierarchy say:

url - The URL of the scene file. Usually this will be “scene_id.json”

loadSceneHierarchy() takes the relative URL of the scene file as the first argument. When built using the Editor, the scene url will be "<id>.json". Where <id> is the scene id. You can get the id of the scene from the Editor URL e.g. https://playcanvas.com/editor/scene/<id>

1 Like

@dave, you’re right, thanks! i should have been looked at browser URL! ^^
and by the way, another question: when i load scene hierarchy by pc.Application.loadSceneHierarchy(), preloader will show automatically by itself, or should i run it by itself? and if yes, how can i do it quickly? :slight_smile:

The preloader (loading screen) is handled by the loading script (__loading__.js in a published app) and is only shown during the initial load.

hey can you tell me the code to enable and disable entities? need help.

Please use developer reference and user manual as well as tutorials to learn about basics of PlayCanvas: http://developer.playcanvas.com/

Enable/Disable Entity: http://developer.playcanvas.com/en/api/pc.Entity.html#enabled

Is there any plan in the works to be able to select scenes by name? (we may implement our own, just wondering)

Thanks!

Scene names are not unique, so we can’t use those for selection.

I understand they’re not unique, but the current situation is quite challenging.

Particularly, if you fork, then all your scene IDs will now be different.

I ran into this issue specifically when I looked at the demo project for changing scenes:

https://developer.playcanvas.com/en/tutorials/changing-scenes/

  • It worked, then I forked it to edit it, and it immediately didn’t work because the scene IDs were now invalid.

Would it be possible to take a similar direction to Unity, where I believe you get the first scene matching the name given that’s in the list of scenes in your build, or you can request the scene by index into that list?

An index would at least make for portability in terms of forking and be a little less obscure than the current numbers.

Scene names are not unique

A good solution might be to allow scene name selection, with the notes indicating that if there are multiple scenes with the same name, then only the first one found is used.

It’s then up to the developer to make sure the scene names are unique.

The existing functionality to select a scene based on the scene id should of course be kept, but as scotty5e mentioned, there are issues with it when forking. With both options, the developer can choose what method has the least issues for them.

I’m assuming here that the scene names would be filtered based on the current project ( so that you could have a scene called “Main Menu” in many different projects, but each project would load its own custom one )

2 Likes

The interface could warn when creating a non-unique scene name (or indeed enforce uniqueness, but I prefer a warn and “eh, it’s your own problem” approach)