you’re building a Babylon.js scene and using await scene.whenReadyAsync() and currentScene.dispose(). Let’s break down what those lines mean

Godson PrakasiaGodson Prakasia
1 min read

🔍 Code Breakdown:

const scene = new Scene(engine);

const cam = new FreeCamera("camera", new Vector3(0, 0, -5), scene);

const box = MeshBuilder.CreateBox("box", { size: 1.5 }, scene);

await scene.whenReadyAsync();
currentScene.dispose();

return scene;

✅ await scene.whenReadyAsync()

This line waits until the scene is fully initialized and ready to render.

Use case:

Some things in Babylon.js (like textures, materials, loading assets, shaders, etc.) may not be ready instantly. whenReadyAsync() lets you wait for everything to load before continuing.

Analogy:

“Wait until the stage is fully set up before showing the play.”


✅ currentScene.dispose()

This cleans up the previous scene (currentScene) from memory.

• Removes all meshes, materials, cameras, etc.

• Stops animations and events

• Frees up GPU memory

Why use it?

If you’re switching scenes (like loading a new level or restarting a game), you don’t want the old one taking up memory or interfering.

Analogy:

“Before starting a new scene, throw out the old set so it doesn’t clutter the stage.”


🧠 Summary:

CodeWhat it does
await scene.whenReadyAsync()Waits for scene setup (textures, shaders, etc.)
currentScene.dispose()Cleans up the old scene from memory
return sceneReturns the newly created, ready-to-go scene

0
Subscribe to my newsletter

Read articles from Godson Prakasia directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Godson Prakasia
Godson Prakasia