Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes a bug when >1 entities are added to and removed from 1 family in the same frame #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/engine/entity/include/halley/entity/family.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ namespace Halley {
// Move all entities to be removed to the back of the vector
{
int n = int(entities.size());
std::vector<std::pair<int, int>> moves;
moves.reserve( removeCount );
// Note: it's important to scan it forward. Scanning backwards would improve performance for short-lived entities,
// but it causes an issue where an entity is removed and added to the same family in one frame.
for (int i = 0; i < n; i++) {
Expand All @@ -177,7 +179,7 @@ namespace Halley {
if (iter != toRemove.end() && id == *iter) {
toRemove.erase(iter);
if (i != n - 1) {
std::swap(entities[i], entities[n - 1]);
moves.emplace_back( std::make_pair( i, n - 1 ) );
i--;
}
n--;
Expand All @@ -186,6 +188,9 @@ namespace Halley {
}
}
}
for (const std::pair<int, int>& move : moves) {
std::swap(entities[move.first], entities[move.second]);
}
Ensures(size_t(n) + removeCount == entities.size());
}

Expand Down