Variable holding geometry showing undefined

I was loading some pcd files in a scene for some side project, and I had to use the geometry after loading it in the scene for raycasting and rotation. But I was getting variable undefined error. Here is my sample code:

var points_geometry = undefined
var load_pcd =  function(){
    loader.load('./arma_Red.pcd',function(points){
        var buffer_geo = new THREE.BufferGeometry()
        buffer_geo.setAttribute('position',new THREE.Float32BufferAttribute(points.geometry.attributes.position.array,3))
        var pts_mat = new THREE.PointsMaterial({color: 0xff00ff})
        points_geometry = new THREE.Points(buffer_geo,pts_mat)
        scene.add(points_geometry)
    })
    
}

var animate = function(){
    renderer.render(scene,camera)
    controls.update()
    requestAnimationFrame(animate)
}
load_pcd()
console.log(points_geometry)
animate()

I am getting points_geometry as undefined after calling load_pcd function. Please help me out with this

Have you checked the scope?

JavaScript Scope

Function Scope
Global Scope

1 Like

It’s undefined because the data is not loaded yet, when you call console.log(points_geometry).

1 Like

But I called the load_pcd function just before I called console.log(points_geometry). Should’nt it load the geometry to my scene right away?

No. Loading is asynchronous.

1 Like

Thanks for the reply again @prisoner849