Skip to content

Commit

Permalink
Solves #181
Browse files Browse the repository at this point in the history
  • Loading branch information
genaray committed Feb 15, 2024
1 parent 92a6b86 commit b4823e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Arch.Tests/WorldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ public void TrimExcess()
That(world.Capacity, Is.EqualTo(archetype.EntitiesPerChunk));
That(archetype.ChunkCount, Is.EqualTo(1));
That(archetype.ChunkCapacity, Is.EqualTo(1));

// Recycled ids must be trimmed too so that the newest created entity is not out of bounds!
world.RecycledIds.TryPeek(out var entityId);
That(entityId.Id, Is.EqualTo(world.Capacity - 1));
}

/// <summary>
Expand Down
4 changes: 4 additions & 0 deletions src/Arch/Core/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ public void TrimExcess()
archetype.TrimExcess();
Capacity += archetype.ChunkCount * archetype.EntitiesPerChunk; // Since always one chunk always exists.
}

// Traverse recycled ids and remove all that are higher than the current capacity.
// If we do not do this, a new entity might get a id higher than the entityinfo array which causes it to go out of bounds.
RecycledIds.RemoveWhere(entity => entity.Id >= Capacity);
}

/// <summary>
Expand Down

0 comments on commit b4823e7

Please sign in to comment.