Question about pulling meshes into global variables

I’ve heard about using the

onsucess(meshes)=>{
Globalvariable = meshes;
}

format, but I have problems implementing it into real code where SceneLoader already has a function written by me inside (it also merges meshes). How do I exactly go about this?

Playground: Babylon.js Playground

Lines 15, 34.
This code is somewhat older than my current but the issue stays the same

1 Like

In this case it works well I think to create your materials in the createSceneFunction, outside of the SceneLoader.ImportMesh handlers, that way the materials are always available to both handlers, no matter which mesh finishes loading first. :slightly_smiling_face:
https://playground.babylonjs.com/#799FP0#1

Otherwise if you create the materials inside one of the handlers like in your PG, then they wont exist yet if the other mesh finishes loading first.

Thanks for that, but this doesn’t really do what I wanted (fixes other things)

I want to get the meshes themself to be usable outside the ImportMesh block and assigning them normally to global vars does not work. Someone suggested onsucess(meshes) but I don’t know how to implement that

1 Like

Creating the materials outside of the handlers was necessary to clear the errors out of the console and allow the materials to be accessed in both handlers.

You’re already creating globals enemy and plane, which are accessible outside of the load handlers as wanted. If you don’t want to use globals thou, you can declare enemy and plane variables in the createScene function, set to null initially, for example, like below. :slightly_smiling_face:
https://playground.babylonjs.com/#ELZ376

1 Like