Global variable "scene" persistent between playground runs, can be confusing!

This shouldn’t be a problem for any correct code, but can cause all kinds of weird behavior if people aren’t careful!

When re-running playground code, the global variable “scene” isn’t cleared (not sure about other globals) – it is still set to the scene that was created by the last execution of the playground. This playground demonstrates this happening, run it a few times and watch the counter increase:

https://playground.babylonjs.com/#KDB5GJ

Scene creation code shouldn’t refer to the global “scene” (it should either pass null as scene to creation functions, or explicitly pass the scene it’s creating) BUT it’s a very easy error to make to refer to “scene” somewhere without actually having it defined. This can create some very confusing results since there won’t be any errors, and code kind-of-sort-of works except that materials and textures and meshes are added to the OLD scene rather than the new one… and then they don’t show up in the inspector, and some poor newbie tears their hair out for a while.

Suggested: Clear all Babylon-related window globals in the context heading into a playground re-run. That way any dangling references to “scene” will fail immediately as the variable will be undefined.

1 Like

Hey that sounds good. Wanna do a PR on the PG code?

Delighted to. Since I’m not familiar with the code base, can you point me to the proximity of the code that likely needs changing?

sure thing, it is right there:
Babylon.js/rendererComponent.tsx at master · BabylonJS/Babylon.js (github.com)

thanks for the help :slight_smile:

(Clear engine, scene, and initFunction at the start of a playground run by egnor · Pull Request #10309 · BabylonJS/Babylon.js · GitHub was the PR, just for the record)

3 Likes