Description
What problem does this solve or what need does it fill?
Sometimes, to speed up a system, I cache data in something like a Local<HashMap<Entity, _>>
, and avoid heavier calculations if the cache hasn't changed. Much of this state wouldn't make sense as a separate component, and it feels cleaner to do it as a Local
. Unfortunately, there's no apparent way to detect if an entity despawns and clean up these local caches.
What solution would you like?
Maybe some sort of entity despawn event. Maybe, for symmetry, it could be like:
enum EntityLifecycle {
Spawned(Entity),
Despawned(Entity),
DespawnedRecursive(Entity), // Or not
}
What alternative(s) have you considered?
I could check whether components used to calculate the cache were removed, but then I'd need to either rerun the system in post-update, or only have a single copy there. That isn't always possible.
I could also externalize the cache, but that feels dirty as it pollutes the module namespace with a) state only relevant to one system and b) a secondary system to clean that state up.
Finally, I could create my own events, but this requires me to manually throw those events whenever I despawn. Given that a) this seems like a general problem and b) Bevy itself knows when it is despawning something, it probably makes more sense for the engine to notify that the entity was despawned.