NateTheGreatt / bitECS

Flexible, minimal, data-oriented ECS library for Typescript
Mozilla Public License 2.0
942 stars 84 forks source link

How to handle destroyed entities cleanup in central location #99

Closed zewa666 closed 2 days ago

zewa666 commented 2 years ago

Hey there,

I'd like to know what approach to take to perform some cleanup after an entity has been removed. So e.g I have a map of <eid, sprites> where once the entity is removed, the according entry of the map should be deleted.

I thought about perhaps about instead of calling removeEntity directly instead do addComponent(world, AboutToBeDead, eid) and in the enterQuery of listening for AboutToBeDead, to perform the cleanup and call removeEntity afterwards. It feels like a workaround though.

zewa666 commented 2 years ago

I see that on current master there is a function called getRemovedEntities so that might serve as another option to check in a system and perform cleanups. Does this library follow any specific npm release cycles since it seems the last release is quite behind the current master state

NateTheGreatt commented 1 year ago

i think exit queries can help you here @zewa666

const Sprite = defineComponent()
const spriteQuery = defineQuery([Sprite])
const exitedSpriteQuery = exitQuery(spriteQuery)

exitedSpriteQuery.forEach(eid => {
  // cleanup sprites here
})

not sure if that solves your issue or not.

i've heard some users of bitECS implement their own Remove tag & query so they can have more control over removals/recycling of IDs.

another strategy is to create your own customRemoveEntity function which calls removes all components from an entity and then adds the entity to a queue. the queue can be drained at the end of a frame, calling bitECS's removeEntity on each ID in the queue to safely hand back the EID to bitECS for recycling.

NateTheGreatt commented 1 year ago

i do create releases when i deem master branch to be stable. it's nearing stability, should have a release out in the next couple weeks.

zewa666 commented 1 year ago

thanks for the response, I went with the wrapped removeEntity approach + a postcleanup on end game to clear all potential leftovers

NateTheGreatt commented 2 days ago

observers and manual entity recycling can be used to address this now:

https://github.com/NateTheGreatt/bitECS/tree/rc-0-4-0