Skip to content

Commit

Permalink
Version now starts at 1 for entities. Therefore a default `EntityRe…
Browse files Browse the repository at this point in the history
…ference.IsAlive()` returns false.
  • Loading branch information
genaray committed Aug 23, 2023
1 parent 0c309f0 commit 1c947d8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Arch.SourceGen/Fundamentals/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static StringBuilder AppendCreate(this StringBuilder sb, int amount)

// Recycle id or increase
var recycle = RecycledIds.TryDequeue(out var recycledId);
var recycled = recycle ? recycledId : new RecycledEntity(Size,0);
var recycled = recycle ? recycledId : new RecycledEntity(Size, 1);

// Create new entity and put it to the back of the array
var entity = new Entity(recycled.Id, Id);
Expand Down
7 changes: 6 additions & 1 deletion src/Arch.Tests/WorldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public partial class WorldTest
public void Setup()
{
_world = World.Create();
/*
Entity entity = default;
EntityReference entityReference = default;
_world.Create();*/

for (var index = 0; index < 10000; index++)
{
_world.Create(_entityGroup);
Expand Down Expand Up @@ -119,7 +124,7 @@ public void Recycle()
var newEntity = localWorld.Create(_entityGroup);

That(recycledEntity.Id, Is.EqualTo(entity.Id)); // Id was recycled
That(localWorld.Version(recycledEntity), Is.EqualTo(1)); // Version was increased
That(localWorld.Version(recycledEntity), Is.EqualTo(2)); // Version was increased
That(newEntity.Id, Is.Not.EqualTo(recycledEntity.Id));
}

Expand Down
14 changes: 7 additions & 7 deletions src/Arch/Core/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ internal World(int id)
/// Should not be modified by the user.
/// </summary>
public static World[] Worlds { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; private set; } = new World[4];

/// <summary>
/// Stores recycled <see cref="World"/> ids.
/// </summary>
internal static PooledQueue<int> RecycledWorldIds { [MethodImpl(MethodImplOptions.AggressiveInlining)] get; set; } = new(8);

/// <summary>
/// Tracks how many <see cref="Worlds"/> exists.
/// Tracks how many <see cref="Worlds"/> exists.
/// </summary>
internal static int WorldSize = 0;

Expand Down Expand Up @@ -153,9 +153,9 @@ public static World Create()
#else
var recycle = RecycledWorldIds.TryDequeue(out var id);
var recycledId = recycle ? id : WorldSize;

var world = new World(recycledId);

// If you need to ensure a higher capacity, you can manually check and increase it
if (recycledId >= Worlds.Length)
{
Expand All @@ -177,11 +177,11 @@ public static World Create()
/// <param name="world">The <see cref="World"/>.</param>
public static void Destroy(World world)
{

#if !PURE_ECS
Worlds[world.Id] = null;
RecycledWorldIds.Enqueue(world.Id);
WorldSize--;
WorldSize--;
#endif

world.Capacity = 0;
Expand Down Expand Up @@ -241,7 +241,7 @@ public Entity Create(Span<ComponentType> types)
{
// Recycle id or increase
var recycle = RecycledIds.TryDequeue(out var recycledId);
var recycled = recycle ? recycledId : new RecycledEntity(Size, 0);
var recycled = recycle ? recycledId : new RecycledEntity(Size, 1);

// Create new entity and put it to the back of the array
var entity = new Entity(recycled.Id, Id);
Expand Down

0 comments on commit 1c947d8

Please sign in to comment.