[SOLVED] Templates sometimes failing to Load

I am instanciating templates at runtime and in general the following code works without issue and I get the message LOADED. However with some models I get the NOT LOADED error despite the template existing correctly in the templates folder(this.modelAssetTemplateName).

I cant see much difference between the models, they all have the same type of textures (jpgs), similar texture sizes and all load correctly when I simply drag the templates into the editor.

Is it just not finding the template quick enough? Do I somehow need to wait until the template is guaranteed to be found? How would I do that? Cheers

 var modelAsset = this.app.assets.find(this.modelAssetTemplateName);
    if( modelAsset && modelAsset.loaded)
    {
        var instance = modelAsset.resource.instantiate();
        this.model_position=this.app.root.findByName("Model Position");
        this.model_position.addChild(instance);
        console.log("LOADED:"+this.modelAssetTemplateName); 
    } 
    else
    {console.error("NOT LOADED:"+this.modelAssetTemplateName); }

The assets in general don’t have the behaviour of loading other assets that they are using.

In this case of templates, when loading a template asset, it doesn’t load the model/texture/sound assets that the template is using.

However, when assets are used in the scene and they aren’t loaded yet, the engine will kick off a load of the asset. In this case, creating an instance of the template and adding to scene will trigger a load of any assets that haven’t been loaded yet.

It’s a bit difficult to know what the issue is without a repro.

Things to check are:

  • Does the template asset exist and loaded is false?
  • Does it not find the template asset at all? Double check that the name is correct and in the correct case.
  • Is the template asset marked as preload?
  • Is the template asset loaded if not marked as preload

-All the templates do exist and the debug is printing the name its trying to load correctly.
-All templates are set to preload-checked
-If I set any template to Preload-UN Checked it doesnt load

My project is here: https://playcanvas.com/project/795361/overview/mattress-firm-v1
Video of model failing to load here: Dropbox - NOTLoad_Template.mp4 - Simplify your life

Below is an example of one template that does not get found/does not load/instantiate:
Error (This is the template it is trying to instantiate):
image
…but we can see it exists in the templates folder here…
image

Folders are assets as well so after adding a breakpoint on ‘NOT LOADED’ message and looking at the asset, we can see the at the asset found is a folder, not a template:

Change this line:

 var modelAsset = this.app.assets.find(this.modelAssetTemplateName);

To

 var modelAsset = this.app.assets.find(this.modelAssetTemplateName, 'template');

As an extra side note, change asset retries to 5 to make it more resilient to network errors when loaded:

1 Like

Fantastic! Thank you! I understand a bit more everyday :slight_smile: