I’m using Javascript (actually Typescript) classes to create my objects, that can have several Babylon child objects.
To have my IDE (VSCode) not give errors/warnings all the time, when I create a class property that is a Babylon type, I need to already create it in the constructor, although I have a special init
method as well, that is called to create the actual object the right way.
For example (simplified), I have a class like:
export class Person extends BABYLON.Mesh {
public head: BABYLON.Mesh;
public body: BABYLON.Mesh;
public size: number = 10;
constructor(name: string, scene: BABYLON.Scene) {
super(name, scene);
this.head = new BABYLON.MeshBuilder.CreateSphere('', scene);
this.body = new BABYLON.MeshBuilder.CreateBox('', scene);
}
public init(size: number = 10) {
this.size = size;
this.head.dispose();
this.head = BABYLON.MeshBuilder.CreateSphere('head', { diameter: this.size / 2 }, this.scene);
this.addChild(this.head);
this.body.dispose();
this.body= BABYLON.MeshBuilder.CreateBox('body', { width: this.size, height: this.size, depth: this.size }, this.scene);
this.addChild(this.head);
}
}
I find it a bit strange to have to create all these ‘empty’ meshes in the constructor, while I override them again in the init
-method, just to keep my IDE happy. I also really have to make sure all these empty objects are disposed in the right way, because otherwise the inspector gets filled with ‘no name’-meshes in the root, after creating a lot of objects.
Am I doing something wrong? Is there a way to create an empty mesh, something like BABYLON.Mesh.Empty()
, (a bit like BABYLON.Vector3.Zero() ) that is enough to have no warnings in the IDE, and have no ‘no name’ meshes in the inspector?
I hope I explained it well, since english is not my first language.