Instantiate Templates

The examples I’ve seen on the learn playcanvas site, and from forum posts, show templates that exist in the hierarchy (disabled) being cloned. This seems like a strange approach to me - what’s the point in making templates if they have to exist in the hierarchy? And isn’t there any downside to having loads of entities, albeit disabled, in the hierarchy?

I’d rather instantiate templates without having them in the hierarchy at all - is this possible?

1 Like

Hey,

Those templates might be some sort of reference, and if not I also do not see any reason for that to occur. You want to instantiate the templates without the hierarchy, and I have done this already and I would suggest a couple ways.

  1. Create an attribute to store the specific template you wanna create
    In order to do this way, just make an attribute that has:
ScriptName.attributes.add('chhesTemplate', {
    type: 'asset',
    assetType: 'template'
});

After making that attribute (in a script) you can call the following code (either in the initialize function OR update, or wherever you need it):

var instance = this.chhesTemplate.resource.instantiate(); //this CREATES it and stores in a variable

You may use this variable as you please, because it stores the instance of the template

  1. You can also just find the asset’s ID and paste this into the .get()

Example:
Let’s say your asset’s ID (listed AT THE TOP of the template’s description, NOT the entity, the actual template asset) is 551247. This is just an example, but lets say it is this.

By using the following code, you can find the template asset and then create another instance of it

var templateAsset = this.app.assets.get(551247);
var instance = templateAsset.resource.instantiate();

I hope this helps!!

Chhes :cheese:

1 Like

That’s super, thanks so much for this!

Just tried it out, works a treat

1 Like

Those tutorials were written before the Template asset feature was done so they are using the ‘old’ way.

1 Like

Makes sense, and I know keeping documentation up to date is a nightmare given how fast things move. I wonder if there’s a way the community could suggest edits to the docs, to be reviewed/edited/accepted by the PlayCanvas team to keep them up to date…

1 Like

The developer.playcanvas.com website is a public repo, you are free and most welcome to submit PRs!

1 Like

Oh fantastic, I shall make a point of doing that, thanks!

1 Like

Hey again, some follow up questions on method 1 of your answer:

  • Is ‘chhesTemplate’ only the name of the property of the script - so this doesn’t relate to the name of the template?
  • If so, how have you identified which template you wish to use? If not, does that mean templates need to be uniquely named?

Hey again,

I picked ‘chhesTemplate’ because its something out of the box so I was hoping you would know it was something that you can name as anything. (It means you can name that ‘chhesTemplate’ to whatever you want)

The templates can be uniquely named and stuff, its all customizable. I just picked that name for fun and cause chhes. In order to set the template as something just drag a template onto the script which has been added to an entity, but you probably already know that.

If you need anything else be sure to reply back!

Chhes :cheese:

Ah ok, I’m new to the editor so didn’t realise the attributes would appear in editor to be set there! Thanks again for the help

YEP!!

wheeeeeeeeeeee

Chhes :cheese: