Zoadian / nitro

An Entity Component System for the D Programming Language. http://www.boost.org/LICENSE_1_0.txt
Boost Software License 1.0
15 stars 0 forks source link

find a good solution to deleteComponent while iterating #5

Open Zoadian opened 10 years ago

Zoadian commented 10 years ago

if a component is deleted while iterating over that type it will fail. we have deletaLAter/deleteNow/flush in a branch, but maybe we can update the inputRange direktly and avoid those funktions entirely.

Ideas are very welcome.

Zoadian commented 10 years ago

addComponent while iterating is also broken!

rcorre commented 9 years ago

Just stumbled upon this framework and it looks interesting! I'll just throw an idea out there in case its helpful:

When I was working on my last game I needed a list structure that I could efficiently add and remove elements from without doing a linear search for each removal.

I ended up using a structure I called a DropList.

Basically, it is a linked list that uses a predicate to determine what elements should be removed. DropList!(Foo, x => !x.active) would be a list that lazily removes a Foo when its active flag is set to false.

When you deactivate a Foo, it remains in the list until the next time you iterate over it. When the iterator hits an element that was deactivated, it removes that node from the list and skips over it, repeating until it hits the next active element. In this way, you don't have to do much extra work for removals as they happen during iteration, yet your view of the list looks the same as it would have if elements were removed immediately.

I haven't used it in months now, but I believe it could safely remove elements during iteration, though it is likely not threadsafe. You can also add elements while iterating, but if I remember correctly new elements won't be visible until the next iteration (they are added to the front of the list).

You can see a usage example here (I called it RemovalList at the time, but basically the same thing).

I've only skimmed how nitro works, but I'd guess you could generate a bool[] for every component type to store the active state.

Just a thought, hope it helps!