Game restart: Recreate scene without reloading assets?

Hi folks!

To allow users to start a game, exit (to a main menu), and restart the game countless times without reloading the page, is there a recommended, robust way of cleaning up and handling Babylon.js’ lifecycle?

For robustness, I was thinking of disposing and recreating Scene every game restart so that I don’t have to manage disposing all the existing Babylon resources for players, weapons, environments, etc… and start from a clean slate. But I’d also like to reuse the assets already loaded instead of reloading them.

I load most of my assets into Babylon’s AssetContainers, then create instances or clones of the meshes inside as players, weapons, etc… are added.

Google’s AI overview says that using AssetContainer allows you to move everything within it from one scene to another scene.

However, Claude says that won’t move everything in the container to the new scene:

Every Babylon item (Mesh , Material , etc.) has _scene baked in at constructor time and Babylon never reassigns it. Mesh.clone() / createInstance() bind the new clone to source.getScene() — i.e. the source’s stale _scene . So clones made from a cached template always bind to the original scene, no matter which scene you’re trying to put them in. scene.addMesh() only pushes to the array; it doesn’t update _scene . Result: cached templates produce clones tied to the disposed scene.

Just curious to hear your thoughts on this :smiley: Thank you all!

  • A hard reload mitigates potential memory leaks :grin:

  • Also depends on the ratio load/lazy load. Keep in mind, even if you do not lazy load (at all / much) right now, you might come to realise that lazy loading can shorten loading times considerably. So you might invest a lot of time in building an asset-reusing-reloading system and then end up lazy loading 90% of your assets anyway :confused:

  • Keep in mind, too, should you ever add any feature that alters meshes, you cannot readily re-use them; e.g. if you dynamically merge meshes at runtime for performance reasons.

Thank you so much, Joe!

I’ve been doing this for development :smile: but now realize if I want to publish on a web game portal, it’ll need to allow players to restart a game without refreshing the page.

I currently load all assets up front (haven’t looked into lazy loading yet but should). Great point that building a system that reuses assets on scene recreation might not be a good use of time.

Perhaps, I can hope that the browser’s cache will prevent refetching of assets on repeated calls to BABYLON.Import___Async()/BABYLON.Load___Async() for the same file.

Great point too! I do this once at the start for pathfinding and will be careful in handling this.