Jump to content

best practice for sharing instances


tomer
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...