Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
0
Members

[Script] [SOLVED] How to know what Scene is running?

A topic by Lacroiix Lancaster created Mar 14, 2016 Views: 1,037 Replies: 7
Viewing posts 1 to 4
(1 edit)

So, I have one script for two Scenes (SceneA, SceneB). I want to do something like this:

if (Sup.getScene() === 'SceneA') 
    { //doSomething } 
else if (Sup.getScene() === 'SceneB') 
    { //doOtherStuff } 
else 
    { //doNothing }

The point is: I want to get the current scene running. Is it Possible?

@Edit

I tried this: Scene Detection by LukeLanFaust answer, but my Log was: >> activeScene content: undefined

Moderator(+2)

LukeLanFaust created a new function, loadScene, that must be called instead of Sup.loadScene to do the extra-work. This means you can't use the startup scene option from the settings anymore. You will have to call it yourself from code.

I was afraid you'd say that hahaha. Thanks by reply!

Hey Lacroiix, I noticed you wanted to be able to use the startup scene setting in the options menu but my script didn't support it. I went ahead and rewrote my script so that it automatically does the work for you :D

let activeScene: string;
let loadSceneLegacy = Sup.loadScene;

function loadScene(sceneAsset: Sup.Scene)
function loadScene(sceneAssetPath: string)
function loadScene(scene: any){
    loadSceneLegacy(scene);
    if(typeof scene == "string") {
        activeScene = scene;
        Sup.log(scene);
    }else{
        activeScene = scene.path;
        Sup.log(scene.path);
    }
}

Sup.loadScene = loadScene;

Just ignore the "loadScene" function since you don't have to use it anymore. Everything is now handled by the builtin function Sup.loadScene. I left the Sup.log calls in the function so that you'd be able to see it working in the console, so you can remove those lines after you've got it working. Once you start the game, it'll instantly set the activeScene to the scene loaded.

If you want to always use the scene's name, just replace "activeScene = scene.path" with "activeScene = scene.name", which looks like what you want in this case :)

Hope this helps!

Moderator(+2)

Hey! But this is cheating!

Just kidding :p Nice work. I would refrain from changing the API though because it could make difficult to help each other out.

An other idea would be to initialize activeScene variable to the value of the first scene instead of null. In this case, you won't have any problem with the first Sup.loadScene from the settings.

(+1)

But cheating is fun! :^)

Yeah, I agree about changing the API. It can make things messy if done too much, and causes confusion if you forget that a project handles it differently then other ones. I tried to keep it as close to the original functionality as possible without changing it's intended functionality to avoid most confusion, I just wanted to see if I could make it simple to use :)

(+1)

Runs like a charm. Thank you scriptBoy! ;) This saves lots of time (and files!) xD

No problem ;D