I need help for ImportMeshAsync

Hi,

i’m trying to create instances of an object.
I want to do this things in a class : if the object doesn’t exist then import him and create an instances, else (if the object doesn’t exist) create an instances.

I have the following code :

index

const scene = createScene();

let obj = new ObjetSupp("test", "./Objets de decoration/", "Tree.obj");
obj.Charge();
obj.Charge();

console.log(obj);

ObjetSupp

isCreated() {
    console.log(scene.meshes);

    if(scene.meshes.find(meshe => meshe.name == this.nom) != undefined){
        console.log(true);
        return true;
    }
    else{
        console.log(false);
        return false;
    }
}


async Charge() {
    if (!this.isCreated()) {

        console.log("not created");

        BABYLON.SceneLoader.ShowLoadingScreen = false;
        var newMeshes = await BABYLON.SceneLoader.ImportMeshAsync(null, this.path, this.file, scene);

        console.log("end await")

        for (var i = 0; i < newMeshes.meshes.length; i++) {
            if (newMeshes.meshes[i].material != null) {
                if (newMeshes.meshes[i].material.diffuseTexture != null) {
                    newMeshes.meshes[i].material.diffuseTexture.hasAlpha = true;
                    newMeshes.meshes[i].material.useAlphaFromDiffuseTexture = true;
                }
            }
        }

        console.log("start merge")

        this.mesh = BABYLON.Mesh.MergeMeshes(newMeshes.meshes, true, true, null, false, false);
        this.mesh.name = this.nom;
        this.mesh.normalizeToUnitCube();
        // this.mesh.isVisible = false;
    }

    console.log("create instance");
    this.mesh.createInstance(this.nom + "-" + this.mesh.instances.length+1);
}

But it doesn’t work beacause the 2nd “obj.Charge()” start before the end of the 1st one.
Here all my console log to illustre what i say :

Can i get some help ?
thanks by advance

You can wait until it’s done like below (and will need to make the function that below code is in async).

await obj.Charge();
await obj.Charge();
console.log(obj);
3 Likes

Hello @VerandaLouis just checking in, was your question answered?

Hello, sorry for late reply, so yeah in fact it works but an async function in a async function is not too much haha ?

And I put my question on hold for the moment because nothing is urgent.

1 Like

There aren’t any problems in having async functions nested inside others, as long as you use them properly :smiley:

2 Likes

alright, thank you :slight_smile: