Below is a function I have created that utilizes a GLTF loader
to import a model into the scene from another class:
LoadModel(path){
this.gltfLoader.load(
path,
(gltf) =>
{
this.scene.add(gltf.scene)
}
)
}
I am trying to call this function from another class in order to add the returned gltf.scene mesh to the players array, which is intended to keep track of the players' meshes:
this.players.push(this.loader.LoadModel('./../static/player.glb'))
However, I am facing an issue where I cannot access the variable outside of the gltfLoader.load()
function. This is demonstrated in the code snippet below:
LoadModel(path){
let mesh = null
this.gltfLoader.load(
path,
(gltf) =>
{
this.scene.add(gltf.scene)
mesh=gltf.scene
console.log(mesh) // prints gltf.scene
}
)
console.log(mesh) //prints "null"
}