Get scene name or id

Hi everyone,

I am doing a local storage system for some variable values in my project, but i realised that i need to differenciate the values that i am saving from one scene to other, as the scripts i am using in both scenes are the same but i need to store different values depending on the scene they are on.

For doing that i am trying to find a way of identifying in which scene that value is being stored, so i can add it to the storage item name. Something like the following:

if (storageAvailable('localStorage')) {
            localStorage.setItem('var1' + this.currentScene.name , var1);  
    }

The problem is that I do not find any way of doing something like “this.currentScene.name” (what obviously does not works)

Thank you a lot!

Hi @practicas_imas,

As strange as it may sound, I think there isn’t any way to get that.

Unless you handle scene loading on your own. Similar post: How do I get the current scene name? - #8 by yaustar

1 Like

Not quite what you’re asking, but I do have a Scene Manager that keeps track of what scene you’re in by name:

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

Of course, this is assuming you’re using the manager to switch the scenes for you, but it works by keeping the manager in an initialization scene and then saving the name of the current scene and previous scene while additively working with any additional scenes.

Hope that’s helpful.

1 Like

That is a good way of doing it, but i am afraid it wont work for me as i need many scenes.
Thanks anyway!

Sad to hear that :frowning:
Hope it is implemented in the future

There is no concept of ‘scenes’ in the way you are thinking about it in PlayCanvas

Each ‘scene’ has a hierarchy and settings that are added/removed and applied. You don’t actually ‘switch’ scenes in PlayCanvas (read more here: Loading Scenes | Learn PlayCanvas)

What you can do is keep track of what scene hierarchy/settings you are loading and store it in a global variable.

eg https://playcanvas.com/project/973141/overview/keep-track-of-scene (search for currentSceneName in code)

1 Like

That way of doing it may be helpful. Using a global variable to store the scene change looks promising.
I will try now and see how it goes

Thanks a lot!

Tagging the Root node of each scene you load in might suffice for some of viewers here.

In my case I want the persistant UI to update based on whether the player has just loaded in an “internal building” or “external map” scene.

With this the UI will listen for a scene change, search the scene for the tag ‘scene_internal’ or ‘scene_map’ and update itself accordingly.