[ecs] Delay calculation of last index

Removing a hierarchy from an entity can result in a large number of
component removes in the same pool, thus changing index of the lest
element of the pool. This *seems* to fix the memory corruption I've been
experiencing with the debug UI.
This commit is contained in:
Bill Currie 2023-07-04 16:37:09 +09:00
parent 71bc708030
commit 7e338b7e29

View file

@ -116,7 +116,6 @@ Ent_RemoveComponent (uint32_t ent, uint32_t comp, ecs_registry_t *registry)
uint32_t ind = pool->sparse[id];
component_t *c = &registry->components.a[comp];
if (ind < pool->count && pool->dense[ind] == ent) {
uint32_t last = pool->count - 1;
// invalidate the entity for this component to prevent the component
// being double-removed due to deletion of the component resulting
// in the entity being deleted (happens with hierarchies)
@ -137,6 +136,7 @@ Ent_RemoveComponent (uint32_t ent, uint32_t comp, ecs_registry_t *registry)
}
}
}
uint32_t last = pool->count - 1;
if (last > ind) {
pool->sparse[Ent_Index (pool->dense[last])] = ind;
pool->dense[ind] = pool->dense[last];