#Despawn on drop?

5 messages · Page 1 of 1 (latest)

hallow dust
#

Say I have a collection of Entities. Assume that this is one of those collections that sometimes drops elements when full - an example would be a cache, stack, deque, FIFO, priority queue, etc. The use case is a highly-dynamic, procedurally generated world in which entities are being created and destroyed according to some algorithm. So for example, one possible scenario is one in which the "oldest" entities are dropped first.

What I want is for the collection to despawn the entity when it is dropped from the collection. I can't explicitly call despawn() on the items because I don't directly control when entities get dropped, the collection controls this.

Now, I could make the elements of the collection wrappers around Entity which implements Drop and calls commands.despawn - but in order to do that, I'd have to keep a reference to commands in the wrapper, which I'm pretty sure is not allowed.

Is there any solution for this?

scenic knoll
#

The actual despawning should probably happen in a system.
An easy, but maybe not performant choice would be to have the system check for a collection entry for every entity. If there is none, despawn it.
To not loop, you would need some extra collection that holds the entites to despawn and is accessible from the drop impl and the system.

woeful pine
#

Sounds like any solution that works around this data structure limitation is going to be suboptimal. Scanning the collection every frame or using some kind of despawn event on drop seem like the obvious options. A global variable would help reduce memory usage if you have tons of entities.

IMO the best option is to manually consume entities that get evicted from the collection, but you need an API that supports it.

hallow dust
#

The number of entities isn't that large, generally on the order of 100.

#

The specific collection I'm working with is lru, wondering whether I should just write my own lru implementation or fork the one I have.