tomer Posted November 16, 2017 Share Posted November 16, 2017 Hello guys, I'm new to Babylon.js and I'm wondering what will be the best practice for sharing instances between different scopes? 1. by name (let's say that I'm sharing the scene instance somehow): const mesh = scene.getMeshByName('someMesh'); 2. by instance: class MainCamera { private static camera: BABYLON.ArcRotateCamera; static init(scene: BABYLON.Scene) { if (typeof MainCamera.camera === 'undefined') { MainCamera.camera = new BABYLON.ArcRotateCamera(null, 0, 0, 10, BABYLON.Vector3.Zero(), scene); // ... } } static get = (): BABYLON.Camera => MainCamera.camera; } in a different scope: import { MainCamera } from '../scene/mainCamera'; const camera = MainCamera.get(); The second approach seems much more OO to me, what do you guys think? Thanks, Tomer. Quote Link to comment Share on other sites More sharing options...
RaananW Posted November 16, 2017 Share Posted November 16, 2017 Hi Tomer, I am not sure a class with only static members make sense. The singleton pattern makes sense when you really need a single instance, but you need an instance per scene. Not only there, when you init the camera again, the old instance is simply gone... Use babylon's native functions (like you suggested), set IDs to each object (or use .uniqueId), and I think it'll be better. But this is really up to you, and the way you see your application. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.