[SOLVED] What is the best way to destroy an Entity from the game?

I’m trying to delete some cubes on a game after pressing a button. I have all the cubes in an array so I can pick the entity and use the destroy method, but it’s still visible in the game. Even when I pop the entity from the array and then I just destroy it. or when I delete de model before destroying it. It’s also a problem because the more the game progresses the more entities there are. and all the pages of playcanvas start crashing (editor, launch area, code editor). I have to finish the browser task to restart the browser and enter playcanvas again.

That is strange, entity.destroy() would definitely remove all entity meshes from the rendered layers.

Are you using batching? Normally the batched meshes would get updated when you destroy a batched entity, though can’t think any reason why this would be happening.

What is batching?

A way to improve performance, take a look at this:

https://developer.playcanvas.com/en/user-manual/optimization/batching/

1 Like

Ooooh, ok thanks. Now I understand batching. But I’m not using it.
The cubes are generated by cloning a cube I prepared in the scene. These clones are added as children of this.app.root (because it’s easier for me to find them and position them). And then when I want to remove them (I actually remove all the cloned cubes to make new ones) I just go through the children array and remove those which were cloned by using a tag.

Perhaps, you could give us a pseudo code how you create / destroy them? As @Leonidas mentioned, destroy() method should be effective in removing them. If they are still visible after the destroy() method, then you either never get to that method call, or you destroy something else.

2 Likes

When I clone a cube I do this:

var newGridCube = this.gridCube.clone();
newGridCube.setPosition(5, 0, -1); // this is just an example of a possible position
this.app.root.addChild(newGridCube); 

When I want to remove the cubes

for (var child of this.app.root.children) {
    if (child.tags !== undefined && child.tags.has("gridCube")) {
        this.app.root.removeChild(child);
        child.destroy();
    }
}

When I do a console log of each child I get the right entity. the position is even correct.

This should work, not sure why it fails, if you can replicate the conditions to a sample project of yours and share it we can take a look.

Some things to note:

  • If you are going to call destroy on an entity there is no need to call removeChild before. It is going to be called by the engine anyhow.
  • Your for loop is correct and it is working but you can do something simpler to get all of your gridCube tagged entities:
// this returns an array with the tagged entities
var cubes = this.app.root.findByTag('gridCube');
1 Like

Actually now that I think of it, that’s your issue, you are looping through the root hierarchy and destroying entities while in the loop.

That breaks the iteration/indexing and it will miss entities.

Try my suggestion of getting the cubes array and looping on that array to call destroy, instead of the app.root.children array.

1 Like

I’m gonna try that right away. I’ll let you know as soon as I test it. Thanks for the suggestion.

Ok, It’s happening the same. I can call the destroy method of the entities, and I think it’s doing so because on next calls of this.app.root.findByTag(“gridCube”); only retrieves new cubes, the old ones seem to be destroyed, but the mesh is still visible.

I’m doing this

var gridCubes = this.app.root.findByTag("gridCube");
    for (var i = 0; i < gridCubes.length; i++) {
        console.log(gridCubes[i]);
        if (i!==0) {
            gridCubes[i].destroy();
            
        }
    }

I don’t want to remove the element with index 0 becasue that’s the one I’m cloning. so I want ot keep it to make new cubes.

I could actually start the for loop in var i = 1, but I’m so used to starting it with 0 XD

This Visama looks more like an issue with the state of your scene and the order you do things.

Destroy definitely works and will remove the entity from the scene hierarchy.

Yeah, I think it’s destroying the entity. But I thought the mesh would be destroyed too and I won’t be rendered.

https://launch.playcanvas.com/851441?debug=true
If you want to try it yourself how it looks. The space key is used to clone cubes. The arrows to move, and the keys 1 and 2 to go up and down.

I noticed how the array of gridCubes changes, and therefore the destroy method is working, it’s destroying the entities from the hierarchy but I guess I’ll disable the element or the model first and then I’ll destroy it.

omg…

Now it’s working

Thank you @Leonidas , your solution of calling the cubes with the method FindByTag helped a lot.
Now it’s working fine, for both kind of cubes.

1 Like

Here is an option:
https://playcanvas.com/project/660558/overview/temp

1 Like