Array of entities in editor

MyScript.attributes.add('textures', { type: 'asset', assetType: 'texture', array: true });

So, we have array of assets and array of strings. What a problem to make array of entities? :slight_smile:

2 Likes

Hey

So it’s not possible to make an array of entites in the editor, and I wouldn’t recommend it anyway. If you want to make an array of entities you should create one parent entity in the hierarchy, add that to the script’s attributes, and then reference that entity’s children[].

you could argue that you want the entities to be parented separately from each other as they could be unrelated objects like ui groups.

I still don’t get why they don’t have arrays of entities. So, I’m looking forward to an answer of that.
At the moment I’m doing it this way:

MyScript.attributes.add("Entity_1", {type: "entity", title: "My Awesome Entity 1"});
MyScript.attributes.add("Entity_2", {type: "entity", title: "My Awesome Entity 2"});

It’s ok, but not a very clean solution. Any further hints?

Unfortunately, it just hasn’t been implemented. The best alternative I can give is to tag the entities (you can multiple tags per entity) and search by tag to get an array of entities that have that tag.

1 Like

Thanks for the explanation and the hint.
I fear, I’m by far no deep enough in the code of PlayCanvas to code this feature on my own. But I would appreciate a solution for this. :wink:

Here is a simple example of what I mean what I disable all the boxes in the scene: https://playcanvas.com/project/604431/overview/entities-by-tag

Note that all the boxes have the tag, some-tag in the editor.

image

I’m updating this thread because it’s the first thing that pops up in the search results when you Google how to create an array of entities in the editor. It is actually possible to create an array of entities now! You just need to set “array” to true when you declare your script attribute, like so:

ExampleScript.attributes.add("entities", {type: "entity", array: true, default: [""], title: "Example Entity Array"});

Here’s more info from the manual: https://developer.playcanvas.com/en/user-manual/scripting/script-attributes/

8 Likes

Yay!

I don’t remember why I have need array of entities, but good to know this was implemented.