From 0332cdde66d842e3c7e1fa968b0a23e54c15462b Mon Sep 17 00:00:00 2001 From: friflo Date: Thu, 13 Feb 2025 13:34:41 +0100 Subject: [PATCH] Test_StructuralChange_Docs - wip --- .../ECS/Arch/Test_StructuralChange_Docs.cs | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/Tests/ECS/Arch/Test_StructuralChange_Docs.cs b/src/Tests/ECS/Arch/Test_StructuralChange_Docs.cs index 27a9155a..abfe0d92 100644 --- a/src/Tests/ECS/Arch/Test_StructuralChange_Docs.cs +++ b/src/Tests/ECS/Arch/Test_StructuralChange_Docs.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using Friflo.Engine.ECS; using NUnit.Framework; @@ -8,6 +9,7 @@ // ReSharper disable InconsistentNaming namespace Tests.ECS.Arch { +[Ignore("source for docs: https://friflo.gitbook.io/friflo.engine.ecs/documentation/query#structuralchangeexception")] public static class Test_StructuralChange_Docs { [Test] @@ -15,10 +17,32 @@ public static void CollectionModifiedException() { var list = new List { 1, 2, 3 }; foreach (var item in list) { - list.Add(42); // throws InvalidOperationException : Collection was modified; enumeration operation may not execute. + // throws InvalidOperationException : Collection was modified; enumeration operation may not execute. + list.Add(42); } } + + +[Test] +public static void QueryException() +{ + var store = new EntityStore(); + store.CreateEntity(new Position()); + + var query = store.Query(); + query.ForEachEntity((ref Position position, Entity entity) => + { + // throws StructuralChangeException: within query loop. See: https://friflo.gitbook.io/friflo.engine.ecs/documentation/query#structuralchangeexception + entity.Add(new EntityName("test")); + }); + // Valid approach using a CommandBuffer + var buffer = store.GetCommandBuffer(); + query.ForEachEntity((ref Position position, Entity entity) => { + buffer.AddComponent(entity.Id, new EntityName("test")); + }); + buffer.Playback(); +} } }