How to wait asset loading then render the scene?

Hi! I have a problem about asset loading, I would like to load one or more assets, and then create some instances of the asset; I am trying to use AssetManager.
My code look like this:


manager=AssetManager
console.log(loading...)
for each asset:
   task=manager.addMeshTask(asset)
   task.onSuccess(){
       save asset in a data structure
       console.log(loaded asset)
   }

manager.load()
console.log(loading done)

generateMesh(...)

Now the problem is that I get an error (mesh is undefined) when I call generateMesh because the mesh is not loaded; in fact the log in the console look like this:

loading…
loading done
error!
loaded asset

How is possible to first load all the meshes and then start the scene?

I also tried to put that part of code into the init function like this:

initFunction().then() => {
  *all the loading*

  manager.onFinish= {
     engine.runRenderLoop(  scene.render()  )
  }

}

But I don’t know if this is a correct approach and the problem still persists.

I know it’s possible to use generateMesh inside task.OnSuccess() but I would like to have a different behaviour for each asset and in this way I should put an if for each asset type.

I think is useful to point out that everything works if the asset is generated after some time because the asset is loaded.

Any help is welcome :slight_smile:

A repro case in the playground would definitely help. Normally waiting on scene.whenReady should be favored.

At the end I managed to partially solve the problem, I don’t know if it is good practice but I did like this:

*createScene()

manager=AssetManager
console.log(loading...)
for each asset:
   task=manager.addMeshTask(asset)
   task.onSuccess(){
       save asset in a data structure
       console.log(loaded asset)
   }

manager.load()
  manager.onFinish= {
    **main()**
  }

where the main function contains essentially everything. In this way the project is started only when the manager finished loading.

Again, i don’t know if this is correct, maybe it’s horrible and in the future it will break everything but for now it works, and I hope it could help someone who is looking for something similar
:slight_smile:

sounds perfect to me :slight_smile: