[SOLVED] How to assign assets to script attributes?

I’m looking at the loading json tutorial.

I can’t work out how it knows to load a specific json file. If I have two in the project - say under scripts - I can’t work out how it will know which one to load. The definition in the code does not reference an asset, asset id, filename, or url. and its not already dragged into the scene.

I would have assumed something like:

Setup.attributes.add('characterData', { type: 'asset', assetType: 'json', filename:"names.json" });

There isn’t an example in

confused :frowning:

The script has an attribute defined:

// Create a script attribute to enable the drag and drop of a JSON asset containing character data
Game.attributes.add('characterData', {
    type: 'asset',
    assetType: 'json'
});

Then, to assign a specific JSON file to a specific entity with that script assigned, you simply drag and drop the JSON asset onto the attribute:

In the case of that tutorial project, the script is assign to a script component on the root entity in the Hierarchy panel (entitled ‘Root’).

It seems that your link to the API Reference Manual isn’t quite complete in terms of all the options for creating attributes. For example, it doesn’t list the assetType property. But this page should be complete.

brilliant thanks. Works when I do that.

Now I’m trying to find and clone my Asset to add many objects based on number and position pulled from the json.
I’m using the tutorial on “Programmatically Creating Entities” as a starter guide.
My asset is called Plinth.
I’ve added this to the spawnCube function.

Setup.prototype.spawnCube = function () {
    var entity = new pc.Entity();
    var modelPlinth = this.app.assets.find("Plinth", "model");

… and then create the new entity with

entity.addComponent("model", { type: "asset", asset: modelPlinth.clone() });

but this.app.assets.find(“Plinth”, “model”) returns null

Try


this.app.assets.find("Plinth.json", "model")

Fantastic - I’m starting to get the hang of it. Thanks very much guys.