[SOLVED] Create an entity programmatically

Which bit doesn’t seem to work? Do you get any errors in console?

No no errors there but when the code search this.ship=app.root.findByName(‘Ship’); don’t find it

Does the ship appear in the game?

Are you sure that the callback of loading the JSON is called (ie have you set a breakpoint on entity.name="Ship"?)

No @yaustar , the callback is not called the breakpoint has not effects

Hmm… Is the loadFromUrl actually called? Can you put a breakpoint on

app.assets.loadFromUrl("https://raw.githubusercontent.com/ayring/kallen/master/shipfinal.json", "model", function (err, asset) {

to make sure it is called?

uhm seems not, maybe it shouldn’t be inside the initialize function?

Initialise function should be okay. Is anything else in the initialise function being called?

Re-checked the loadFromUrl is called as well as the other things in initialize function just the callback isn’t called it jump down from the loadFromUrl

Link to project and which file is this in?

Here it is Project https://playcanvas.com/editor/scene/396696
file https://playcanvas.com/editor/code/352037/terrain.js line 117 and follows

The problem is that you are trying to find the ship entity (line 586) BEFORE the ship and finished loading and added to the scene.

I get the null reference error before my breakpoint was hit in the callback.


Uh? I see, since it’s a large model it need some time to get loaded, but how do i avoid this?

Ideally you would a preloader to load in all the assets before you entered the scene that you need the assets for.

There’s no ‘easy’ way to get round this beyond managing the loading of external assets.

Even if was a small model, there is no guarantee on how fast it loads as it’s dependent on factors beyond your control.

Using ‘load’ the model was loaded, (you can see the code on the following lines that are disabled) but it load the model with no textures and i tried the code i have in player.js to change the texture and don’t seems to work … see line 153

No it wasn’t. It was still loading but in that case, you didn’t wait for the load callback to create the entity. You create it immediately and when the model was ready, it just popped into existence.

If loadFromUrl also returned the asset, that method would work here as well.

I’ve now created a patch for the loadFromUrl to return the asset so you can use it when creating an entity immediately.

Here’s my scene with the patch and example: https://playcanvas.com/editor/scene/547080

I have changed the code, get no errors now but i don’t see the ship :smiley: i don’t know if it’s better or worst lol EDIT: in console i see the loaded message so it should load the model properly EDIT2: tryed a couple of things…nothing to do seems the model is not loaded properly

i noticed this loadFromUrl Use this to load and create an asset if you don’t have assets created. doesn’t that mean since i have assets created in the project that is not proper to use loadfromurl? and instead use load >> load Load the asset’s file from a remote source. EDIT: that should be why in your project with no assets loaded it work and in mine doesn’t

Reverted back to load and added this line but doesn’t work Ship.model.model.meshInstances[0].material = app.assets.find(‘Wood2’,‘material’).resources[0];

Was thinking since it create the entity before the asset is loaded i have to assign the material to asset after it’s loaded so should be something like this?

asset.on("load", function (err, model) {
            Ship.model.model.meshInstances[0].material = app.assets.find('Wood2','material').resources[0];

or since we are working on the asset the line inside the callback should be something like asset.material.diffuseMap = app.assets.find(‘Wood2’,‘material’).resources[0];