Create a "master" game object to generate clones / instances from

Hi, I would like to generate a gameobject that can be cloned (as well as customs methods with it). My idea is to:
extend mesh
add custom methods to object
be able to clone / instantiate whole “gameobject”. By whole I mean mesh + methods in object

I have a non working example, as it seems to clone a transform (most likely my handicap as still trying to learn javascript). https://www.babylonjs-playground.com/#RQXDQV#2

Any help please?

Hello!! I’m not sure what would be the difference with mesh.clone or mesh.instantiate?

hi @Deltakosh as some objects will have different materials, need a clone instead of instance (or so I think). But so far can’t get it to work on neither case.

mesh.clone will clone the geometry and share the material of the mesh

Thanks, I misunderstood then.

On my original question, apart from wanting to be able to use different materials, I want to ‘copy’ (instantiate or clone) whole code and mesh as an object, so that it could be encapsulated as a whole. What is it that I’m missing?

Possibly one way would to to create a class that copies a mesh and defines the methods of the mesh and then create instances of the class.

HI @Johnk, seems like a good approach. My conceptual issue there, is:
a) load the mesh, then
b) how to inform the class that said mesh is ready to be instantiated.

SceneLoader has a variety of methods to load meshes all have a call back function to add the code to use after loading Load from any file type - glTF, OBJ, STL, etc. - Babylon.js Documentation

https://doc.babylonjs.com/api/classes/babylon.sceneloader

Have it “kind” of working. https://www.babylonjs-playground.com/#RQXDQV#3

But still don’t know how to reference mesh, as I can’t link callback function to class, so had to use a timer to wait (not ideal, as different people = differente loading speeds).

Tried to add the class definiton inside the callback, but of course didn’t worked.

I was thinking along these lines https://www.babylonjs-playground.com/#RQXDQV#4

Thanks, I was thinking the other way around.

What would happen if later on I need to instance more “helmets” ? Lets say they are bonus items for game, can’t be called from the start.

Once the game is underway the mesh should exist so something like

check using name that helmet is in scene.meshes
If it is create new instance

Ideally mesh would be ready beforehand, but if it isn’t? So far the examples I’ve seen use built in primitives which seem to be real quick, or they generate the clones in the callback. But for something a generated more dynamicly, can’t seem to find something that fits

There is mesh.onReady() https://doc.babylonjs.com/api/classes/babylon.mesh#onready so unless a loading error occurs the instance will be created once the mesh is loaded

scene.getMeshByName(‘helmet’).onReady(function() {do instance})