[SOLVED] Create an entity programmatically

Hello, i have this code but i have an error, what i did wrong?

var asset = new pc.Asset("Ship", "model", {
                url: "http://www.lostinn.com/shipfinal.json"
            });
            app.assets.add(asset);
            asset.on("load", function (err, model) {
            //loaded
            });
            app.assets.load(asset);
            var Ship = new pc.Entity(); // Create an Entity
            Ship.addComponent("model", {
                type: 'asset',
                asset: asset
            });
            Ship.addComponent('rigidbody', {
                type: 'static'
            });
            Ship.addComponent("collision",{
                type:"box",
                halfExtents: (0.4,2,10)
            });
            Ship.name="Ship";
            Ship.tags.add("main","sea");
            Ship.setLocalScale(35,35,35);
            // Add it to the Entity hierarchy
            app.root.addChild(Ship);

When you have an error you should read what it says. If you don’t get what it means post it here.

But the first thing that I see is that you use app instead of this.app. But I don’t know if you defined it earlier.

@mcmorry i use the old scripts that’s why i use app instead of this.app and the error is just [playcanvas-stable.dbg.js:0]: Script error. so i thought that someone more expert could see an error without having to go through a step by step debugging since i have a very slow connections where i am right now

Is that the full callstack? At a guess, it might be because you are trying to use an asset from http where the app is hosted on https.

oh right i remember i read something about that thanks @yaustar will give it a try with https.
Edit: no that was not the problem so usedd chrome and got this
WebGL: INVALID_ENUM: bindTexture: invalid target
playcanvas-stable.dbg.js:5476 WebGL: INVALID_ENUM: bindTexture: invalid target
setTexture @ playcanvas-stable.dbg.js:5476
playcanvas-stable.dbg.js:5476 WebGL: INVALID_ENUM: bindTexture: invalid target
setTexture @ playcanvas-stable.dbg.js:5476
playcanvas-stable.dbg.js:5476 WebGL: INVALID_ENUM: bindTexture: invalid target
setTexture @ playcanvas-stable.dbg.js:5476
playcanvas-stable.dbg.js:5476 WebGL: INVALID_ENUM: bindTexture: invalid target
setTexture @ playcanvas-stable.dbg.js:5476
playcanvas-stable.dbg.js:5476 WebGL: INVALID_ENUM: bindTexture: invalid target
setTexture @ playcanvas-stable.dbg.js:5476
playcanvas-stable.dbg.js:5476 WebGL: INVALID_ENUM: bindTexture: invalid target
setTexture @ playcanvas-stable.dbg.js:5476
www.lostinn.com/shipfinal.json Failed to load resource: net::ERR_CERT_COMMON_NAME_INVALID
6[.Offscreen-For-WebGL-00000288751339A0]RENDER WARNING: there is no texture bound to the unit 0
396696:1 [.Offscreen-For-WebGL-00000288751339A0]RENDER WARNING: there is no texture bound to the unit 0
396696:1 [.Offscreen-For-WebGL-00000288751339A0]RENDER WARNING: there is no texture bound to the unit 0

When I tried this in my sandbox app (http) I get the following error:

On https:

Are you hosting the app on your own server?

I’ve also noticed that there is no http://www.lostinn.com/shipfinal.mapping.json file (see here for example https://github.com/yaustar/3dmodel-Playcanvas). Was that on purpose?

no @yaustar it must be not copied when i saved the files on website :stuck_out_tongue:

And no, i’m using the editor and just wanted to recover the ship model from my website, i added the missing file but i still have the error
[396696?debug=true:1]: Uncaught SyntaxError: Unexpected end of JSON input

SyntaxError: Unexpected end of JSON input
at JSON.parse ()

I would go with either the export is missing files/the files are missing on the server, there is a security/CORS issue or the data is corrupted.

Can you share the project with the ship model?

https://playcanvas.com/editor/scene/396696 and here is the file that has the code for the remote model https://playcanvas.com/editor/code/352037/terrain.js (into initialize function) i have downloaded the ship model for site from the project itself

I’ve uploaded the files to GitHub and they seem to work fine for me: https://playcanvas.com/editor/scene/547080

Have loaded the file on github and still don’t work, the problem isn’t on code?

‘file’ or ‘files’?

This is what it looks like on github in terms of files that I’ve uploaded and structure https://github.com/yaustar/3dmodel-Playcanvas/tree/master/shipfinal

Also, be sure to look at how I’ve loaded it in the script.

put all files in github, copied your script and tried to set a break point to check without success, tomorrow will try again, too late now

You need to link to the raw content, not the preview that github gives.

Which gives you this link: https://raw.githubusercontent.com/ayring/kallen/master/shipfinal.json

Github sure isn’t user friendly lol anyway made the change but stil missing something … will check slowly step by step sigh

Ok now i have the entity (yesssssss) but the model is without texture, how i add it? this is the code (PS. i use old scripts so i had to use this code @yaustar, your for some reason didn’t work)

var asset = new pc.Asset("Ship", "model", {
                url: "https://raw.githubusercontent.com/ayring/kallen/master/shipfinal.json"
            });
            app.assets.add(asset);
            asset.on("load", function (err, model) {
            //loaded
            });
            app.assets.load(asset);
            var Ship = new pc.Entity(); // Create an Entity
            Ship.addComponent("model", {
                type: 'asset',
                asset: asset
            });
            Ship.addComponent('rigidbody', {
                type: 'static'
            });
            Ship.addComponent("collision",{
                type:"box",
                halfExtents: (0.4,2,10)
            });
            Ship.name="Ship";
            Ship.tags.add("main","sea");
            Ship.setEulerAngles(0,90,0);
            Ship.setLocalScale(35,35,35);
            Ship.enabled=false;
            // Add it to the Entity hierarchy
            app.root.addChild(Ship);

Sorry, forgot to reply to this.

I use loadFromUrl rather than load which also loads the mapping.json file as well and there the materials and textures along with it.

1 Like

Sorry to ask this i added to your code entity.name=‘Ship’; coz i need the loaded entity with that name, but don’t seems to work any clue?

app.assets.loadFromUrl("https://raw.githubusercontent.com/ayring/kallen/master/shipfinal.json", "model", function (err, asset) {
            var entity = new pc.Entity();
            entity.addComponent("model");
            entity.model.model = asset.resource;
            //entity.addComponent('rigidbody', {
            //    type: 'static'
            //});
            //entity.addComponent("collision",{
            //    type:"box",
            //    halfExtents: (0.4,2,10)
            //});
            entity.name="Ship";
            //entity.tags.add("main","sea");
            //entity.setLocalScale(35,35,35);
            app.root.addChild(entity);
            });

and are the other instructions that i putted as comment right or not?