Skip to content

Commit ddab44e

Browse files
TryGetRef fix
1 parent 82a75ab commit ddab44e

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/Arch.Benchmarks/TryGetBenchmark.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,8 @@ public void TryGetGenericRefFail()
4747
for (var index = 0; index < _entities.Count; index++)
4848
{
4949
var entity = _entities[index];
50-
var pos = _world.TryGetRef<Position2D>(entity, out var exists);
51-
52-
if (!exists)
53-
{
54-
_consumer.Consume(pos);
55-
}
50+
_world.TryGetRef<Position2D>(entity, out var exists);
51+
_consumer.Consume(exists);
5652
}
5753
}
5854

src/Arch/Core/World.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,12 +1149,17 @@ public ref T TryGetRef<T>(Entity entity, out bool exists)
11491149
{
11501150
ref var slot = ref EntityInfo.EntityData[entity.Id];
11511151

1152-
if (!(exists = slot.Archetype.Has<T>()))
1152+
if (!slot.Archetype.TryIndex<T>(out int compIndex))
11531153
{
1154+
exists = false;
11541155
return ref Unsafe.NullRef<T>();
11551156
}
11561157

1157-
return ref slot.Archetype.Get<T>(ref slot.Slot);
1158+
exists = true;
1159+
ref var chunk = ref slot.Archetype.GetChunk(slot.Slot.ChunkIndex);
1160+
Debug.Assert(compIndex != -1 && compIndex < chunk.Components.Length, $"Index is out of bounds, component {typeof(T)} with id {compIndex} does not exist in this archetype.");
1161+
var array = Unsafe.As<T[]>(chunk.Components.DangerousGetReferenceAt(compIndex));
1162+
return ref array[slot.Slot.Index];
11581163
}
11591164

11601165
/// <summary>

0 commit comments

Comments
 (0)