Skip to content

Commit

Permalink
Test_StructuralChange_Docs - wip
Browse files Browse the repository at this point in the history
  • Loading branch information
friflo committed Feb 13, 2025
1 parent 53f17bb commit 0332cdd
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/Tests/ECS/Arch/Test_StructuralChange_Docs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Friflo.Engine.ECS;
using NUnit.Framework;


Expand All @@ -8,17 +9,40 @@
// 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]
public static void CollectionModifiedException()
{
var list = new List<int> { 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<Position>();
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();
}
}

}
Expand Down

0 comments on commit 0332cdd

Please sign in to comment.