Skip to content

Commit

Permalink
Fix: clear parent when deleting an entity used as a child.
Browse files Browse the repository at this point in the history
See: #29
  • Loading branch information
friflo committed Nov 11, 2024
1 parent f713eca commit 52fb537
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ECS/Entity/Store/NodeTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ internal void DeleteNode(Entity entity)
if (!HasParent(parentId)) {
return;
}
RemoveTreeParent(id);
int curIndex = RemoveChildNode(parentId, id);
OnChildNodeRemove(parentId, id, curIndex);
}
Expand Down
17 changes: 17 additions & 0 deletions src/Tests/ECS/Entity/Test_Entity_Tree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 52fb537

Please sign in to comment.