Skip to content
Advertisement

How to access a variable outside a subfunction in javascript

I have the following function that uses a GLTF loader to load a model into the scene (imported from another class):

JavaScript

And I call that function from another class like this, wanting to push to the players array (meant to keep the players meshes) the gltf.scene mesh returned from the CreateMesh function.

JavaScript

My problem is that I cannot access that variable outside the gltfLoader.load() function as you see in the following example:

JavaScript

Advertisement

Answer

Assuming this.gltfLoader.load is async and doesn’t already have a promise-returning variant, handle this by “promisifying” that callback-style function.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement