+1 to this. You can also append something to the vector without any trouble. Actual workable C++:Zaflis wrote:Not if you iterate the vector from end to beginning. Some pseudo if you wanted to make particle system:pleegwat wrote:Doesn't work if you're actually destroying entities outside their own update call though, because you may end up swapping an entity which acted this cycle with one which has not.What you get is every single particle is dealt with exactly once on every loop, and deleting one means just one swap operation with the last. On the downside the list won't keep the original order. It is only a problem if you need sorting. But on a free list of entities that doesn't sound like being the case. In case of particles aswell the order doesn't matter.Code: Select all
for (p = PCount-1; p >= 0; p--) particle[p].DealWithIt() particle[p].life-- if particle[p].life <= 0 then PCount-- particle[p] = particle[PCount] particle.deleteLast() end end
Code: Select all
std::vector<Entity*> active_entities;
void step_all_active() {
for (size_t n = active_entities.size()-1; n >= 0; n--) {
Entity *&slot = active_entities[n];
slot->step();
if (!slot->is_active()) {
slot = active_entities.back();
active_entities.pop_back();
}
}
}
void SomeDerivedEntity::step() {
// ... do some updating...
if (do_we_need_to_make_another_entity_active) {
Entity &other_entity = the_other_entity;
if (!other_entity.is_active()) {
other_entity.set_active(true);
active_entities.push_back(&other_entity);
}
}
}