OnDestroy/Cleanup like functionality for Entities.

Imagine you have got a city.
City is divided by “grid spatial partitioning”.
In every cell you have got some cars (Entities).

When the car (Entity) is destroyed by “EntityManager.DestroyEntity(entities);” I would like to remove this car from this grid structure so it does not hold dead Entities.

What would be the best approach, is ICleanupComponentData an option for such a case ?

Yes. This is the kind of use case it was designed for.

1 Like

Do you know any good example of usage of this component.
I’m a little baffled with unity example in documentation
https://docs.unity3d.com/Packages/com.unity.entities@1.0/manual/components-cleanup-introducing.html
, since they are calling EntityManager.DestroyEntity(…) two times.
If I call RemoveComponent(entity,ICleanupComponentData) on entity that was already after EntityManager.DestroyEntity(…). will it destroy this entity completely ?

I’m also not sure about one thing, when I destroy entity with ICleanupComponentData on it, it will remove all components but not cleanup components.
Will it not make structural change or is there some magic behind ?

The 1.0 example is out of date and possibly wrong. The 1.2 docs have a better example.
https://docs.unity3d.com/Packages/com.unity.entities@1.2/manual/components-cleanup-introducing.html
The normal order is:
-Destroy entity, now entity only has cleanup components
-Your system identifies such entities via a query, e.g. find entities with X cleanup component and without Y normal component usually found on active entities
-Handle any additional cleanup that you use the cleanup component for
-Use RemoveComponent to remove the cleanup component, either per-entity or for all entities matching a query
-Once systems have removed all cleanup components from the entity, it will no longer exist

Calling DestroyEntity for an entity with cleanup components will result in the entity getting an archetype change, and each cleanup component removal again causes an archetype change.

1 Like

That is just flat out wrong. As the 1.2 docs correct for, the second DestroyEntity() is supposed to be a call to RemoveComponent() where it gives a ComponentTypeSet.

If that is the only cleanup component left, then yes.

Correct. The entity’s archetype will be left with Entity, all cleanup components, and a newly added component called CleanupEntity. Once an entity is stripped down to just the archetype of Entity and CleanupEntity, it will be destroyed for real.

I believe removal of the last cleanup component and the final destruction of the entity get combined. But otherwise, cleanup components always require an additional structural change during setup.

Because of the extra structural change cost, I only use cleanup components for use cases where the performance benefits are large. And consequently, those systems that use them tend to get quite complicated. My best example of usage lives in a 2000+ line system, because it is tracking GPU resources of a deformable mesh, and also juggling that with skeleton entities to save a ton of lookups each frame. If you’d still like me to share it and step through it, I can. But hopefully the answers provided have been enough to get you going again.

3 Likes