[SOLVED] Error that's not true

Btw, with this code i don’t get any error, but it doesn’t work properly

           var asset = new pc.Asset("Ship", "model", {
                url: "https://raw.githubusercontent.com/ayring/kallen/master/shipfinal.json"
            });
            
            
            var Ship = new pc.Entity(); // Create an Entity
            app.assets.add(asset);
            asset.ready(function(){
            app.assets.load(asset);
            
            Ship.addComponent('model');
            Ship.model.asset = asset;
            });
            
            asset.on("load", function (err, model) {
            //loaded
                Ship.model.model.meshInstances[0].material.diffuseMap=app.assets.find('wood1.jpeg','texture').resources[0];
                Ship.model.material.update();
            });

Hi @ayrin,

What is your output? What doesn’t work properly for you?

Hi @Leonidas well it simply don’t show the ship model, i guess it’s because the entity is created and duplicated before the model is fully loaded, i will attempt some more try tonight to see if i can overcome that. Any clue is welcome :slight_smile:

I think I can spot a mistake in your code, here is an attempt to fix it:

var asset = new pc.Asset("Ship", "model", {
    url: "https://raw.githubusercontent.com/ayring/kallen/master/shipfinal.json"
});


var Ship = new pc.Entity(); // Create an Entity
app.assets.add(asset);

asset.ready(function (err, model) {

    Ship.addComponent('model');
    Ship.model.asset = asset;

    Ship.model.model.meshInstances[0].material.diffuseMap=app.assets.find('wood1.jpeg','texture').resources[0];
    Ship.model.material.update();
});

app.assets.load(asset);

This way I have again the error loading model and the ship is shown in the middle of town instead on sea, but model and textures appear to be right
EDIT: seems the asset.ready fuction is fired before the model is loaded

@ayrin can you create a simple sample project doing only that? It will much easier to debug this.

Sorry i have no more free space that’s why i am trying to move some model externally :frowning:
EDIT: wondering if asset.on(‘load’ would do the trick instead asset.ready

Reading through the engine codebase it seems ready is always fired, but feel free to try the other way. That should work as well, I think the load event is fired prior to ready but as soon as the file has loaded.

Create a new PlayCanvas account for some more space :stuck_out_tongue:

1 Like

Hey @ayrin,

Created a simple sample project, using only the loadFromUrl method the model loads fine:

image

https://playcanvas.com/editor/scene/882308

I imagine the issue is somewhere else, and not directly related to this. Scan your code to see if any other method attempts to access the model before it’s loaded, and most likely that’s where you can the error.

the issue is here i guess, when i try to duplicate the ship entity and positioning all the 3 objects

if (this.build[i].Ship===true) {
                        var check=app.assets.find("Ship");
                        check.ready(function(check){
                        var nhouse=app.root.findByTag('ship');
                        for (var nh=0; nh<nhouse; nh++) {
                            if (nhouse[nh].enabled===false) {
                                nhouse[nh].enabled=true;
                                nhouse[nh].rotate(0,this.build[i].r,0);
                                nhouse[nh].rigidbody.teleport(this.build[i].x,this.build[i].y,this.build[i].z);
                                nh=100;
                            } 
                        }
                        if(nh!==100) {
                        this.ship=app.root.findByName('Ship');
                        var cship = this.ship.clone();
                        app.root.addChild(cship);
                        cship.enabled = true;
                        cship.rotate(0,this.build[i].r,0);
                        cship.rigidbody.teleport(this.build[i].x,this.build[i].y,this.build[i].z);
                        cship.tags.add('ship','main');
                        }
                        });
                    } else if (this.build[i].Ship===false) {
                        var ship=app.root.findByTag('ship');
                        for(var x=0;x<ship.length;x++) {
                            ship[x].enabled=false;
                        }
                    }

Yes, the issue in your original code is that you run the following method in your initialize method before the models have finished loading:

this.placeBuildings();

Make sure to run it after all models have loaded and it will work as expected.

i have tryed to:

asset.ready(function(err,model){
this.placebuildings();
}.bind(this));

and finally worked, thanks @Leonidas

1 Like