static void DestroyBatch(Entity *entities, Chunk *chunk, int indexInChunk, int batchCount, EntityComponentStore *entityComponentStore, ManagedComponentStore managedComponentStore) { var archetype = chunk->Archetype; if (!archetype->SystemStateCleanupNeeded) { entityComponentStore->DeallocateDataEntitiesInChunk(entities, chunk, indexInChunk, batchCount); managedComponentStore.IncrementComponentOrderVersion(archetype, chunk->SharedComponentValues); entityComponentStore->IncrementComponentTypeOrderVersion(archetype); if (chunk->ManagedArrayIndex >= 0) { // We can just chop-off the end, no need to copy anything if (chunk->Count != indexInChunk + batchCount) { managedComponentStore.CopyManagedObjects(chunk, chunk->Count - batchCount, chunk, indexInChunk, batchCount); } managedComponentStore.ClearManagedObjects(chunk, chunk->Count - batchCount, batchCount); } chunk->Archetype->EntityCount -= batchCount; SetChunkCount(chunk, chunk->Count - batchCount, entityComponentStore, managedComponentStore); } else { var newType = archetype->SystemStateResidueArchetype; var sharedComponentValues = chunk->SharedComponentValues; if (RequiresBuildingResidueSharedComponentIndices(archetype, newType)) { var tempAlloc = stackalloc int[newType->NumSharedComponents]; BuildResidueSharedComponentIndices(archetype, newType, sharedComponentValues, tempAlloc); sharedComponentValues = tempAlloc; } // See: https://github.com/Unity-Technologies/dots/issues/1387 // For Locked Order Chunks specfically, need to make sure that structural changes are always done per-chunk. // If trying to muutate structure in a way that is not per chunk, will hit an exception in the else clause anyway. // This ultimately needs to be replaced by entity batch interface. if (batchCount == chunk->Count) { managedComponentStore.IncrementComponentOrderVersion(archetype, chunk->SharedComponentValues); entityComponentStore->IncrementComponentTypeOrderVersion(archetype); EntityManagerChangeArchetypeUtility.SetArchetype(chunk, newType, sharedComponentValues, entityComponentStore, managedComponentStore); } else { for (var i = 0; i < batchCount; i++) { var entity = entities[i]; managedComponentStore.IncrementComponentOrderVersion(archetype, entityComponentStore->GetChunk(entity)->SharedComponentValues); entityComponentStore->IncrementComponentTypeOrderVersion(archetype); EntityManagerChangeArchetypeUtility.SetArchetype(entity, newType, sharedComponentValues, entityComponentStore, managedComponentStore); } } } }