How to reload a running scene using sceneswitcher

Hi, I am sceneSwitcher components switchScene method to switch scenes and it works well, but I need to reload the current scene. How to achieve this?

Hi Ashok,

To reload the current scene using the SceneSwitcher, you can utilize the select method by passing the current index again. Here’s how you can implement it:

  1. Get the current index of the loaded scene.
  2. Call the select method with that index to reload the scene.

Here’s the pseudocode followed by the implementation:

// Pseudocode:
// Get the current index of the loaded scene.
// Use the select method with that index to reload the current scene.

const currentSceneIndex = sceneSwitcher.currentIndex; // Get the current index.
await sceneSwitcher.select(currentSceneIndex); // Reload the current scene.

And here’s the complete code snippet:

import { SceneSwitcher } from "@needle-tools/engine";

// Assuming you have an instance of your SceneSwitcher
async function reloadCurrentScene(sceneSwitcher: SceneSwitcher) {
    const currentSceneIndex = sceneSwitcher.currentIndex; // Access the current index
    if (currentSceneIndex !== -1) { // Ensure a valid index
        await sceneSwitcher.select(currentSceneIndex); // Reload the scene
    } else {
        console.warn("No current scene to reload.");
    }
}

Make sure the method is called after the SceneSwitcher has been properly initialized and a scene is loaded.

Let me know if you need further assistance!

1 replies left - Get a PRO license for more AI replies.
thread_EKpWPBhygOzFxM3MXxsYjsij
Reply to this message to continue the conversation

This will only work if the scenes are present in sceneswitcher scenes array, right? i dont have my scenes in the scenes

Hi, if you want another AI reply you need to reply to the bot’s last message.

Can you clarify what you mean and how your setup is?

Hi,

My setup is similar to the multi-scene example, with React as my UI. However, instead of assigning the scenes array directly to SceneSwitcher, I use a custom manager script in the main scene to handle the scenes. I call the switchScene method from SceneSwitcher and pass the scenes from my manager script as parameters.

In the script below, you’ll see two methods connected to two buttons. For the “Install” button, I use a reversed array. This means if the user clicks “Install” while on “Remove,” it should load the last scene in the array. Similarly, if the user is on the last step of “Install” (the first element of the unreversed array) and clicks “Remove,” it should load the first element of the array.

The problem is that switchScene doesn’t reload the scene if it’s already active. Reloading is important for my setup because it updates the React UI.

loadInitialRemoveSequence(){
    this.isInstallProcedure = false;
    if (this.currentProcedureScenesData[0].scene) this.sceneSwitcher?.switchScene(this.currentProcedureScenesData[0].scene);
    this.sequenceIndex = 0;
  }

  loadInitialInstallSequence(){
    this.isInstallProcedure = true;
    const reversed = this.currentProcedureScenesData.toReversed();
    if(reversed[0].scene) this.sceneSwitcher?.switchScene(reversed[0].scene);
    this.sequenceIndex = 0;
  }

I see, thanks for explaining! I’ll check and see if we can add a method for reloading (or forcing a reload via a parameter) for the next update

Thanks, it will be very helpful

Please checkout the latest release :slight_smile: Releases & Changelogs - #53 by needle-bot

1 Like

Thank you so much, Didn’t expect it to be this quick. Thanks again. Will check and update

1 Like