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?