From ca37a648ce65ce96d2785f986075fb7fd56e08c4 Mon Sep 17 00:00:00 2001 From: armada Date: Mon, 26 Oct 2026 01:08:48 +0300 Subject: [PATCH] Fix World.TryGet(Entity, ComponentType) returning corrupted data for struct components --- src/Arch.Tests/WorldTest.cs | 13 +++++++++++++ src/Arch/Core/World.cs | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/Arch.Tests/WorldTest.cs b/src/Arch.Tests/WorldTest.cs index f14244a5..78f8bc8d 100644 --- a/src/Arch.Tests/WorldTest.cs +++ b/src/Arch.Tests/WorldTest.cs @@ -733,6 +733,19 @@ public void TryGet() That(_world.TryGet(entity, out Rotation _), Is.EqualTo(false)); } + /// + /// Checks if the non-generic TryGet returns the stored component value. + /// + [Test] + public void TryGetByComponentType() + { + var entity = _world.Create(new Transform { X = 1, Y = 2 }); + + That(_world.TryGet(entity, (ComponentType)typeof(Transform), out var component), Is.EqualTo(true)); + That(component, Is.EqualTo(new Transform { X = 1, Y = 2 })); + That(_world.TryGet(entity, (ComponentType)typeof(Rotation), out _), Is.EqualTo(false)); + } + [Test] public void TryGetRefSuccess() { diff --git a/src/Arch/Core/World.cs b/src/Arch/Core/World.cs index 1e38cdb2..fd91a83d 100644 --- a/src/Arch/Core/World.cs +++ b/src/Arch/Core/World.cs @@ -1547,8 +1547,8 @@ public bool TryGet(Entity entity, ComponentType type, out object? component) ref var chunk = ref slot.Archetype.GetChunk(slot.Slot.ChunkIndex); Debug.Assert(compIndex != -1 && compIndex < chunk.Components.Length, $"Index is out of bounds, component {type} with id {compIndex} does not exist in this archetype."); - var array = Unsafe.As(chunk.Components.DangerousGetReferenceAt(compIndex)); - component = array[slot.Slot.Index]; + var array = chunk.Components.DangerousGetReferenceAt(compIndex); + component = array.GetValue(slot.Slot.Index); return true; }