From 06dbf6c9874ff39492a2875822ee4ae813d1217f Mon Sep 17 00:00:00 2001 From: Ullrich Praetz Date: Fri, 19 Jul 2024 12:47:36 +0200 Subject: [PATCH] Engine - ECS: make TreeNode blittable --- Engine/src/ECS/Components/TreeNode.cs | 1 - Engine/src/ECS/Entity/Store/Entities.cs | 10 ++++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Engine/src/ECS/Components/TreeNode.cs b/Engine/src/ECS/Components/TreeNode.cs index c55dd8129..c1bda3ffd 100644 --- a/Engine/src/ECS/Components/TreeNode.cs +++ b/Engine/src/ECS/Components/TreeNode.cs @@ -16,7 +16,6 @@ public struct TreeNode : IComponent // todo should be internal // [Browse(Never)] internal int parentId; // 4 0 if entity has no parent internal IdArray childIds; // 8 - internal int[] dummy; // 8 todo remove /// same as public ReadOnlySpan GetChildIds(EntityStore store) diff --git a/Engine/src/ECS/Entity/Store/Entities.cs b/Engine/src/ECS/Entity/Store/Entities.cs index f2ae13ebb..a35768ae3 100644 --- a/Engine/src/ECS/Entity/Store/Entities.cs +++ b/Engine/src/ECS/Entity/Store/Entities.cs @@ -81,7 +81,10 @@ internal int CreateEntityInternal(Archetype archetype, int id) /// /// Create and return a clone of the passed in the store. /// - /// + /// + /// Child entities of the passed are not copied to the cloned entity.
+ /// If doing this these child entities would be children of the passed entity and the clone. + ///
public Entity CloneEntity(Entity entity) { var archetype = entity.archetype ?? throw EntityNullException(entity); @@ -96,6 +99,9 @@ public Entity CloneEntity(Entity entity) var scriptTypeByType = Static.EntitySchema.ScriptTypeByType; // CopyComponents() must be used only in case all component types are blittable Archetype.CopyComponents(archetype, entity.compIndex, clone.compIndex); + if (clone.HasComponent()) { + clone.GetComponent() = default; // clear child ids. See child entities note in remarks. + } // --- clone scripts foreach (var script in entity.Scripts) { var scriptType = scriptTypeByType[script.GetType()]; @@ -110,7 +116,7 @@ public Entity CloneEntity(Entity entity) // --- deserialize DataEntity dataBuffer.pid = IdToPid(clone.Id); - dataBuffer.children = null; // child ids are not copied. If doing this these children would have two parents. + dataBuffer.children = null; // clear children. See child entities note in remarks. // convert will use entity created above converter.DataEntityToEntity(dataBuffer, this, out string error); // error == null. No possibility for mapping errors AssertNoError(error);