Always provide the scene parameter to all babylon.js functions

Hey!

I just want to share a small tip that can save you a ton of time.

I never explicitly provided the optional scene parameter when creating objects on the scene—until now…

Since introducing a new scene (both scenes are initialized asynchronously) their contexts started to mix.

Now, I have to review every function call that accepts an optional scene parameter and explicitly pass the correct scene. :see_no_evil:

Explanation:
If you don’t provide the scene parameter to a function that supports it, Babylon.js retrieves it using the LastCreatedScene static getter from the EngineStore class. Since it is static—you get the point.

    public static get LastCreatedScene(): Nullable<Scene> {
        return this._LastCreatedScene;
    }

So, take your time and always specify the scene. You never know when it might save your bacon!

2 Likes

Yup.
Also, it’s somehow joining my last request about a warning :

1 Like

Just voted for it. :+1:

1 Like