From b4823e77f45ca5afa2ec3bfa58e4c2ccb83262c0 Mon Sep 17 00:00:00 2001 From: Lars Date: Thu, 15 Feb 2024 15:58:34 +0100 Subject: [PATCH] Solves #181 --- src/Arch.Tests/WorldTest.cs | 4 ++++ src/Arch/Core/World.cs | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/Arch.Tests/WorldTest.cs b/src/Arch.Tests/WorldTest.cs index ee2b933a..d0626369 100644 --- a/src/Arch.Tests/WorldTest.cs +++ b/src/Arch.Tests/WorldTest.cs @@ -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)); } /// diff --git a/src/Arch/Core/World.cs b/src/Arch/Core/World.cs index 6b3bbe4c..b55ab099 100644 --- a/src/Arch/Core/World.cs +++ b/src/Arch/Core/World.cs @@ -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); } ///