From 52fb5371406b50e60e46e96b8a72107c45e413dd Mon Sep 17 00:00:00 2001 From: Ullrich Praetz Date: Mon, 11 Nov 2024 08:38:15 +0100 Subject: [PATCH] Fix: clear parent when deleting an entity used as a child. See: https://github.com/friflo/Friflo.Engine.ECS/issues/29 --- src/ECS/Entity/Store/NodeTree.cs | 1 + src/Tests/ECS/Entity/Test_Entity_Tree.cs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/ECS/Entity/Store/NodeTree.cs b/src/ECS/Entity/Store/NodeTree.cs index b5f52897..8d28a0f1 100644 --- a/src/ECS/Entity/Store/NodeTree.cs +++ b/src/ECS/Entity/Store/NodeTree.cs @@ -539,6 +539,7 @@ internal void DeleteNode(Entity entity) if (!HasParent(parentId)) { return; } + RemoveTreeParent(id); int curIndex = RemoveChildNode(parentId, id); OnChildNodeRemove(parentId, id, curIndex); } diff --git a/src/Tests/ECS/Entity/Test_Entity_Tree.cs b/src/Tests/ECS/Entity/Test_Entity_Tree.cs index 359b2119..bde5c2a3 100644 --- a/src/Tests/ECS/Entity/Test_Entity_Tree.cs +++ b/src/Tests/ECS/Entity/Test_Entity_Tree.cs @@ -768,6 +768,23 @@ public static void Test_Entity_Tree_ChildEnumerator() } } + // https://github.com/friflo/Friflo.Engine.ECS/issues/29 + [Test] + public static void Test_Entity_Tree_CreateThenDestroyEntityWithParent() + { + var store = new EntityStore(); + var root = store.CreateEntity(); + var child1 = store.CreateEntity(); + root.AddChild(child1); + int id = child1.Id; + var parent = child1.Parent; + + child1.DeleteEntity(); + var child2 = store.CreateEntity(); + IsTrue(child2.Parent.IsNull); // before fix this assertion failed + AreEqual(id, child2.Id); // uses recycled id + } + [Test] public static void Test_Entity_Tree_id_argument_exceptions() {