ecs
ecs copied to clipboard
System entity lists
~~Right now systems constantly iterate all entities via world.view()
per frame. This is expensive and inhibited by the size of the worlds entity list.~~
Right now systems iterate entities of interest via world.view()
. The World stores entities in a decently efficient structure using a Map<Component, Set<Entity>>
.
I think it would be ideal for systems to state which components they are interested in and to store their own bitmask. Now the world can create smaller, more focused internal lists based on these bitmasks, and Systems could essentially work with an internal list: this.entities
, and avoid the constant querying via world.view()
.
As entity components are added and removed, they will be constantly compared to the system bitmasks to see if they qualify to be in that list. The world will need to maintain these lists.
Discussion
Q: Can a systems interest vary in terms of components?
A: I am thinking no, that would mean a new system since you are further qualifying interest in a particular group of entities. Basically when a system is created, it's bitmask is only set one time.
Q: Are there any issues with adding/removing entities in the same frame within a system iteration? For example, in a breakout game, if the ball collided with a brick within a collision system, the system might want to destroy the brick entity and add a new entity to the world with a particle emitter.
Also, I just destroyed an entity, all systems now have to be checked to pontentially remove that entity from their entity lists.
A: One concern I have is what is the behaviour when that brick is destroyed but is still present in the current list of entities this system is processing? I think we can handle modify an entity array live during iteration, but we really need to wrap this via the api so avoid tracking this manually.
Example of what I mean by modifying live:
const els = [1, 2, 3, 4, 5, 6];
for (let i = 0; i < els.length; ++i) {
if (els[i] % 2 === 0) {
els[i] = els[els.length - 1];
els.pop();
--i;
} else {
console.log(els[i]);
}
}
Q: What is the effect of modifying an entities component map during a systems iteration? Which in turn could mean the entity bitmask no longer matches the system, meaning the system is no longer interested in it.
A: ?