I have been experimenting with babylonjs and so far everything is great. I am able to load meshes and animate them just fine when everything is in the ImportMesh callback function within the same file. However; when I try to split things up and do something simple like below, I seem to get these race conditions. Take this example:
var models = ["brick_wall", "tree1", "person1"]
for (var i =0;i<models.length;++i) {
BABYLON.SceneLoader.ImportMesh(models[i], "models/","tree.babylon", scene, (newMeshes) => {
this.meshMap[models[i]] = newMeshes[0]
console.log("mesh set:" + models[i])
});
}
The console log gets printed out as "mesh set: undefined". I'm assuming because import mesh executes the callback function in its own thread before "i" has even had a chance to be set? However I'm having trouble getting my head around as to how "i" and/or models list would still not be set when simply doing a console print within the callback. When I do a console print of:
models[1]
within the same callback, I am able to see the value correctly? Makes no sense :(
Does anyone know whats going on here with import mesh in regards to threading? Is there a best practices for babylonjs with waiting for threads? I tried using the scene.executewhenready but I still cannot seem to store and retrieve anything in a variable outside the callback.