Run Button Code

Hello everyone,

I’m wondering what code would replicate what the Playground Run button does. :thinking:
For instance, clicking a button would restart the scene in exactly the same way.

1

I found various forum posts referencing createScene() and scene.dispose(), but my scene crashes when I call those functions.

Just curious if I should keep working with those options, or if there is a better way now to restart a scene.

Do you get any console errors?
If you’re using a createScene function setup like the playgound (creates and returns the scene object)
E. G.

Let myScene = createScene()
myScene.dispose()
myScene = createScene() 

Something like the above should work

And in the engine renderloop, check
myScene && myScene.isReady()
Before calling myScene.render()

Have you seen this example? Help Problems restarting a scene. How to restart a scene correctly?..onReadyObservable does not work

Hello @aWeirdo,

Here’s a PG that demonstrates what I’ve attempted so far:
https://www.babylonjs-playground.com/#83SAJ8#3

I’m new to programming and Babylon.js, so I don’t fully understand how to implement the code you provided. As you’ll see in the PG, calling scene.dispose() freezes the scene. I’m not sure if that’s a successful first step or not :thinking:

Also, hello @holger, and thanks for the link. I did read that post. The solution definitely seems to be there (just like with what @aWeirdo is providing), but I just don’t see it at my current level of understanding. The code makes sense, I guess I just don’t know where it all goes without a working example.

Perhaps with this discussion a functional PG can be made that demonstrates how this works for those with the same question in the future :smiley: :+1:

Hi @Anderson34
I only have a couple minutes atm, so i can’t explain in detail.
Check out the html template on this doc page, maybe that helps you get a better understanding :slight_smile:

First Steps - Babylon.js Documentation

Great, I’ll check that out. Thanks for the help!