# Calling variables from AssetManager

**URL:** <https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048>\
**Category:** Questions\
**Created:** [March 8, 2020, 1:54pm UTC](https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048 "2020-03-08T13:54:35Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Dan](https://forum.babylonjs.com/u/Dan)\
**Post date:** [March 8, 2020, 1:54pm UTC](https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048/1 "2020-03-08T13:54:35Z")

</div>

Hi,  
This has been killing me for days… I wanted to call a certain variable from AssetManager but keep getting -\> “undefined” response.

//global variable  
Var length;

```
var assetsManager = new BABYLON.AssetsManager(scene);
var meshTask = assetsManager.addMeshTask("object", "", "./objectDirectory/", "object.gltf");

```

meshTask.onSuccess = function (task) {

length = this.loadedMeshes.length;

console.log(length); // this works -\> response 23

}

// calling outside the function  
console.log(length); // this doesn’t work. -\> response “undefined”

Am I missing something ?

Thanks.

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [March 8, 2020, 2:00pm UTC](https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048/2 "2020-03-08T14:00:57Z")

</div>

Without a playground it is difficult to be definitive but would suggest that you are getting undefined outside the function because this line is being called before meshTask has successfully finished and so, as it says, length is not yet defined.

Everything you want to do to loaded meshes must be done inside the onSuccess function.

---

<div class="post-metadata">

**Author:** ![Dan](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Dan](https://forum.babylonjs.com/u/Dan)\
**Post date:** [March 8, 2020, 2:14pm UTC](https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048/3 "2020-03-08T14:14:54Z")

</div>

Apparently if I add a timeout it works.

```
     	setTimeout(function () {
	    console.log(length);			
		    }, 5000);

```

I suppose I was calling the function before the task has finished … But isn’t this the job of -\< meshTask.onSuccess ??

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [March 8, 2020, 2:27pm UTC](https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048/4 "2020-03-08T14:27:28Z")

</div>

No

the interpreter farms out the load task and goes straight to the next block of code, it does not hang around waiting for the task to finish before executing the next block of code. Once the load task has been completed the interpreter is notified and then the onSuccess code executed.

This way, for example, while the models are being loaded the interpreter can get on with building a menu system.

---

<div class="post-metadata">

**Author:** ![Dan](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Dan](https://forum.babylonjs.com/u/Dan)\
**Post date:** [March 8, 2020, 2:40pm UTC](https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048/5 "2020-03-08T14:40:43Z")

</div>

Ok I understand.  
So is there a way to notify an external calling function that all the assets are now loaded and are ready to be called ? what approach would you advice ? Thanks.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [March 8, 2020, 2:53pm UTC](https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048/6 "2020-03-08T14:53:36Z")

</div>

You could either pass a callback in or use the scene on Ready observable to not be forced to pass it all the way down.

---

<div class="post-metadata">

**Author:** ![Dan](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Dan](https://forum.babylonjs.com/u/Dan)\
**Post date:** [March 8, 2020, 3:21pm UTC](https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048/7 "2020-03-08T15:21:14Z")

</div>

Thanks !  
I will check the scene on Ready observable approach.

---

<div class="post-metadata">

**Author:** ![holger](https://avatars.discourse-cdn.com/v4/letter/h/ed8c4c/32.png) [@holger](https://forum.babylonjs.com/u/holger)\
**Post date:** [March 8, 2020, 5:36pm UTC](https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048/8 "2020-03-08T17:36:32Z")

</div>

> [@Dan](#):
>
> So is there a way to notify an external calling function that all the assets are now loaded and are ready to be called

What about using `AssetsManager.loadAsync()` [https://doc.babylonjs.com/api/classes/babylon.assetsmanager#loadasync](https://doc.babylonjs.com/api/classes/babylon.assetsmanager#loadasync)  
AFAIU It returns a Promise which gets resolved if all Assetes are loaded.

---

<div class="post-metadata">

**Author:** ![Dan](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Dan](https://forum.babylonjs.com/u/Dan)\
**Post date:** [March 8, 2020, 6:08pm UTC](https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048/9 "2020-03-08T18:08:39Z")

</div>

I couldn’t find any examples on how to use

> [@holger](#):
>
> AssetsManager.loadAsync()

Do you have a simple example ?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [March 8, 2020, 6:50pm UTC](https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048/10 "2020-03-08T18:50:47Z")

</div>

> [@Dan](#):
>
> AssetsManager.loadAsync

`loadAsync` is only available on the sceneLoader if you prefere to call into it manually: [https://www.babylonjs-playground.com/#WGZLGJ#1033](https://www.babylonjs-playground.com/#WGZLGJ#1033)

---

<div class="post-metadata">

**Author:** ![Dan](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Dan](https://forum.babylonjs.com/u/Dan)\
**Post date:** [March 8, 2020, 7:04pm UTC](https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048/11 "2020-03-08T19:04:16Z")

</div>

I have already seen that but couldn’t find any examples being used with AssetManager.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [March 8, 2020, 7:05pm UTC](https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048/12 "2020-03-08T19:05:53Z")

</div>

ok 🙂 there are just no async capabilities with asset manager and the callback pattern will be your best friend there.

---

<div class="post-metadata">

**Author:** ![Pryme8](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pryme8/32/7212_2.png) [@Pryme8](https://forum.babylonjs.com/u/Pryme8)\
**Post date:** [March 8, 2020, 7:06pm UTC](https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048/13 "2020-03-08T19:06:35Z")

</div>

That’s because the “On Success” does not fire till its done so the length never gets bound and gets tossed into the stack basically. While your thread continues on to process the console log that was defined on the line below.

If you have an expected number of assets you are loading you could have a variable that increments on each OnSuccess and then fire a function to see if the incremented number matches what you are expecting if so then do some sort of response.

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [March 8, 2020, 7:46pm UTC](https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048/14 "2020-03-08T19:46:02Z")

</div>

> [@Dan](#):
>
> So is there a way to notify an external calling function that all the assets are now loaded and are ready to be called ?

What is the problem with firing up the external function from within onSuccess? Is it a scoping issue?

---

<div class="post-metadata">

**Author:** ![Dan](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Dan](https://forum.babylonjs.com/u/Dan)\
**Post date:** [March 8, 2020, 8:41pm UTC](https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048/15 "2020-03-08T20:41:37Z")

</div>

I am trying to understand the sync capabilities of asset manager with external calling functions.

Currently I have solved it by putting the external calling function after assetsManager.load();

Seems to work … 🙂 But I don’t know if it’s a best way to ensure that external calling functions don’t return -\> ‘undefined’ values.

example: asset\_length = this.loadedMeshes.length; (from asset manager)

function external () {  
console.log(asset\_length); // this doesn’t work. -\> response “undefined”  
}

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [March 9, 2020, 11:40am UTC](https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048/17 "2020-03-09T11:40:07Z")

</div>

The best way is to wait for onSuccessCallback before doing anything else.

---

<div class="post-metadata">

**Author:** ![Dan](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@Dan](https://forum.babylonjs.com/u/Dan)\
**Post date:** [March 9, 2020, 2:01pm UTC](https://forum.babylonjs.com/t/calling-variables-from-assetmanager/9048/18 "2020-03-09T14:01:16Z")

</div>

Indeed @sebavan. I am also taking this route. Thanks !
