How do I get a single array of entities with 'render' components OR 'model(legacy)' components?

How do I get a single array of entities with ‘render’ components OR ‘model(legacy)’ components.

ie this work for render components…

var renderComponents = this.app.root.findComponents("render");

…but followed by something like this doesn’t concatenate the model(legacy) components to the same array.

renderComponents +=this.app.root.findComponents("model");

How would I do this?
Cheers

Hi @Grimmy!

Maybe there is a better way, but I think you can at least combine two arrays to a new array using concat().

Okay thanks. I think I’m actually running into a slightly different problem.

Later in my code I am looping through the mesh instances of my entity with render components and then Im trying to do the same with the entities that have model components. I am getting an error.

Am I right in saying that model(legacy) components do not have mesh instances in the same way that render components do?

Im trying to do this but get a forEach error:

modelComponents = this.app.root.findComponents("model");//need to also get model(legacy as well as render)

    for (var i = 0; i < modelComponents.length; i++) 
    {
        modelComponents [i].meshInstances.forEach(function (meshInstance) 
        {
.....blahh

Alternatively, is there a way to quickly change all my model(legacy) components to render components?

As far I can see this should work the same.

You can use the search option in the editor.

Search by component type, select all entities of the search result, remove the model component and add a render component.

I get the error: Cannot read properties of null (reading ‘forEach’)

?

That could have another reason?

Odd because I do exactly the same with render components and there is no issue. For example…

This works:

var renderComponents = this.app.root.findComponents("render");//need to also get model(legacy as well as render)

    for (var i = 0; i < renderComponents.length; i++) 
    {
        renderComponents[i].meshInstances.forEach(function (meshInstance) 
        {...

…but this doesnt…

var modelComponents = this.app.root.findComponents("model");//need to also get model(legacy as well as render)
    
    alert("there are "+modelComponents.length+ " model components")//8

    for (var k = 0; k < modelComponents.length; k++) 
    {
        modelComponents[k].meshInstances.forEach(function (meshInstance) //error cannot read properties of null (resading 'foreACH'
        {....

Just to be sure, did you check for example the result of modelComponents.length?

console.log(modelComponents.length);

Ah, I already see you did.

Can you share the editor link of your project so I can take a look?

1 Like

Private message sent.

This code does something similar:

1 Like

I literally cut and paste that code and get an error: cant read properties of length…

 var models = this.app.root.findComponents("model");
    for (i = 0; i < models.length; i++) {
        var model = models[i];
        for (m = 0; m < model.meshInstances.length; m++) {// ERROR: Cannot read properties of null (reading 'length')
           //do nothing
        }
    }

The only difference is this.app.root

Do you get that error at the first model of the array? Could it be there is a model in your scene without a mesh instance?

It simply means your model.json file is not loaded at the time you are executing this code (mesh instances have not been generated yet). Set your model asset to preload, or execute the code after the asset is loaded.

Wierdly Im alreay doing that. However, all I did to fix this was put the following line in.

var models = this.app.root.findComponents("model");
    for (i = 0; i < models.length; i++) {
        var model = models[i];
        if(model.meshInstances)//PUT THIS IN AND IT ALL WORKS FINE!!
                {
                    for (m = 0; m < model.meshInstances.length; m++) {// ERROR: Cannot read properties of null (reading 'length')
                       //do nothing