diff --git a/src/Arch/Core/CommandBuffer/SparseSet.cs b/src/Arch/Core/CommandBuffer/SparseSet.cs index e176b1ef..18436704 100644 --- a/src/Arch/Core/CommandBuffer/SparseSet.cs +++ b/src/Arch/Core/CommandBuffer/SparseSet.cs @@ -169,7 +169,7 @@ public ref T Get(int index) /// public void Clear() { - for (var index = 0; index < Size; index++) + for (var index = 0; index < Entities.Length; index++) { Entities[index] = -1; } diff --git a/src/Arch/Core/CommandBuffer/StructuralSparseSet.cs b/src/Arch/Core/CommandBuffer/StructuralSparseSet.cs index 8db25aec..7f872de7 100644 --- a/src/Arch/Core/CommandBuffer/StructuralSparseSet.cs +++ b/src/Arch/Core/CommandBuffer/StructuralSparseSet.cs @@ -102,7 +102,7 @@ public bool Contains(int index) /// public void Clear() { - for (var index = 0; index < Size; index++) + for (var index = 0; index < Entities.Length; index++) { Entities[index] = -1; } diff --git a/src/Arch/Core/Extensions/EntityExtensions.cs b/src/Arch/Core/Extensions/EntityExtensions.cs index 47c9ad3e..e6e3dad7 100644 --- a/src/Arch/Core/Extensions/EntityExtensions.cs +++ b/src/Arch/Core/Extensions/EntityExtensions.cs @@ -167,6 +167,20 @@ public static ref T TryGetRef(this in Entity entity, out bool exists) return ref world.TryGetRef(entity, out exists); } + /// + /// Ensures the existance of an component on an . + /// + /// The component type. + /// The . + /// The component value used if its being added. + /// A reference to the component. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ref T AddOrGet(this in Entity entity, T cmp = default) + { + var world = World.Worlds[entity.WorldId]; + return ref world.AddOrGet(entity, cmp); + } + /// /// Adds an new component to the and moves it to the new . /// diff --git a/src/Arch/Core/World.cs b/src/Arch/Core/World.cs index 78572210..d006fcf0 100644 --- a/src/Arch/Core/World.cs +++ b/src/Arch/Core/World.cs @@ -929,13 +929,15 @@ public ref T Get(Entity entity) public bool TryGet(Entity entity, out T component) { component = default; - if (!Has(entity)) + + var slot = EntityInfo.GetSlot(entity.Id); + var archetype = EntityInfo.GetArchetype(entity.Id); + + if (!archetype.Has()) { return false; } - var slot = EntityInfo.GetSlot(entity.Id); - var archetype = EntityInfo.GetArchetype(entity.Id); component = archetype.Get(ref slot); return true; } @@ -950,16 +952,37 @@ public bool TryGet(Entity entity, out T component) [MethodImpl(MethodImplOptions.AggressiveInlining)] public ref T TryGetRef(Entity entity, out bool exists) { - if (!(exists = Has(entity))) + var slot = EntityInfo.GetSlot(entity.Id); + var archetype = EntityInfo.GetArchetype(entity.Id); + + if (!(exists = archetype.Has())) { return ref Unsafe.NullRef(); } - var slot = EntityInfo.GetSlot(entity.Id); - var archetype = EntityInfo.GetArchetype(entity.Id); return ref archetype.Get(ref slot); } + /// + /// Ensures the existence of an component on an . + /// + /// The component type. + /// The . + /// The component value used if its being added. + /// A reference to the component. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public ref T AddOrGet(Entity entity, T cmp = default) + { + ref var component = ref TryGetRef(entity, out var exists); + if (exists) + { + return ref component; + } + + Add(entity, cmp); + return ref Get(entity); + } + /// /// Adds an new component to the and moves it to the new . ///