* **Wall Time** 30s * **tokens** Unclear, didn't output * **tool usages** 4 ``` explain how Scene.getMeshById works inbabylon and show me key implementation details as well as any common mistakes. ``` ⏺ How Scene.getMeshById Works in Babylon.js Implementation From scene.ts:3889, here's the actual implementation: public getMeshById(id: string): Nullable { for (let index = 0; index < this.meshes.length; index++) { if (this.meshes[index].id === id) { return this.meshes[index]; } } return null; } Key Details 1. Linear Search Algorithm - Iterates through the entire scene.meshes array sequentially - Uses simple string equality (===) to compare IDs - Returns the first match only - Time complexity: O(n) where n is the number of meshes 2. Return Type - Returns Nullable (can be null) - Always returns the first mesh found with matching ID - Returns null if no mesh with that ID exists 3. ID vs Name vs UniqueId Babylon.js has three different identifiers: - id: String identifier you can set manually (not guaranteed unique) - name: String identifier for human-readable references - uniqueId: Auto-generated numeric ID (guaranteed unique per scene) Common Mistakes 1. Assuming IDs are Unique // WRONG - Multiple meshes can share the same ID! const mesh1 = new Mesh("myMesh", scene); mesh1.id = "player"; const mesh2 = new Mesh("otherMesh", scene); mesh2.id = "player"; // ID collision! scene.getMeshById("player"); // Returns mesh1 (first one added) Fix: Use getMeshesById() (plural) if you expect multiple matches, or use uniqueId instead. 2. Confusing id with name const mesh = new Mesh("spaceship", scene); // This sets the NAME, not ID scene.getMeshById("spaceship"); // Returns null! ❌ // By default, id === name initially, but they're separate properties console.log(mesh.name); // "spaceship" console.log(mesh.id); // "spaceship" (copied from name at creation) Fix: Use getMeshByName() for name-based lookups, or explicitly set the id property. 3. Not Null-Checking // WRONG - Can cause runtime errors const mesh = scene.getMeshById("nonexistent"); mesh.position.x = 10; // TypeError: Cannot read property 'position' of null Fix: Always null-check: const mesh = scene.getMeshById("player"); if (mesh) { mesh.position.x = 10; } 4. Performance with Large Scenes // WRONG - Calling in render loop with many meshes scene.onBeforeRenderObservable.add(() => { const mesh = scene.getMeshById("player"); // O(n) lookup every frame! mesh?.position.x += 0.1; }); Fix: Cache the reference: const playerMesh = scene.getMeshById("player"); scene.onBeforeRenderObservable.add(() => { playerMesh?.position.x += 0.1; }); 5. Using Deprecated getMeshByID() (uppercase D) scene.getMeshByID("player"); // Deprecated! Works but discouraged Fix: Use getMeshById() (lowercase 'd') instead. Better Alternatives For Unique Lookups: // Store uniqueId at creation const mesh = new Mesh("player", scene); const meshId = mesh.uniqueId; // Later lookup (faster, guaranteed unique) const found = scene.getMeshByUniqueId(meshId); For Multiple Meshes with Same ID: // Get all meshes with this ID const allEnemies = scene.getMeshesById("enemy"); allEnemies.forEach(enemy => { enemy.dispose(); }); For Name-Based Lookup: const mesh = scene.getMeshByName("spaceship"); // Uses the 'name' property Related Methods - getMeshesById(id) - Returns array of all meshes with matching ID - getMeshByName(name) - Searches by name property instead - getMeshByUniqueId(uniqueId) - Uses auto-generated numeric ID (guaranteed unique)