Skip to content

Commit 531ca00

Browse files
committed
Fixed Relations<> indexer. See #70 (comment)
1 parent 23738d4 commit 531ca00

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

src/ECS/Base/Types/ComponentType.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ internal override void WriteRelations(ComponentWriter writer, Entity entity)
154154
} else {
155155
writer.writer.json.AppendChar(',');
156156
}
157-
var index = relations.start + n;
158-
var position = relations.GetPosition(index);
157+
var position = relations.GetPosition(n);
159158
var bytes = heap!.Write(writer.componentWriter, position);
160159
writer.writer.json.AppendBytes(bytes);
161160
}

src/ECS/Relations/Relations.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private static MethodInfo MakeGetRelationKey(out bool isEntity)
7878
}
7979

8080
internal int GetPosition(int index) {
81-
return positions != null ? positions[index] : position;
81+
return positions != null ? positions[start + index] : position;
8282
}
8383

8484
/// <summary>
@@ -111,7 +111,7 @@ public ref TRelation this[int index] {
111111
get {
112112
if (version != entityRelations.version) throw RelationsModifiedException();
113113
if (index >= 0 && index < length) {
114-
return ref components[positions != null ? positions[index] : position];
114+
return ref components[positions != null ? positions[start + index] : position];
115115
}
116116
throw IndexOutOfRangeException(index);
117117
}

src/Tests/ECS/Relations/Test_Relations.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,45 @@ public static void Test_Relations_outdated_DeleteEntity_source()
557557
});
558558
AreEqual("Relations<AttackRelation> outdated. Added / Removed relations after calling GetRelations<AttackRelation>().", e1!.Message);
559559
}
560+
561+
[Test]
562+
// Regression test for issue in Relations<> indexer (missed to use start)
563+
// Issue detected at: [Relations<TRelation> [index] operator sometimes returns the wrong TRelation]
564+
// https://github.com/friflo/Friflo.Engine.ECS/issues/70#issuecomment-2896790850
565+
public static void Test_Relations_indexer()
566+
{
567+
var store = new EntityStore();
568+
569+
var entity1 = store.CreateEntity();
570+
entity1.AddRelation(new IntRelation { value = 10 });
571+
entity1.AddRelation(new IntRelation { value = 11 });
572+
entity1.AddRelation(new IntRelation { value = 12 });
573+
entity1.AddRelation(new IntRelation { value = 13 });
574+
575+
var entity2 = store.CreateEntity();
576+
entity2.AddRelation(new IntRelation { value = 20 });
577+
entity2.AddRelation(new IntRelation { value = 21 });
578+
entity2.AddRelation(new IntRelation { value = 22 });
579+
entity2.AddRelation(new IntRelation { value = 23 });
580+
581+
var relations1 = entity1.GetRelations<IntRelation>();
582+
var relations2 = entity2.GetRelations<IntRelation>();
583+
584+
// Both relations must have same length for this test.
585+
// So they are using the same Relations<TRelation>.positions with different Relations<TRelation>.start values
586+
AreEqual(relations1.Length, relations2.Length);
587+
{
588+
int index = 0;
589+
foreach (var relation in relations1) {
590+
AreEqual(relations1[index++].value, relation.value);
591+
}
592+
} {
593+
int index = 0;
594+
foreach (var relation in relations2) {
595+
AreEqual(relations2[index++].value, relation.value);
596+
}
597+
}
598+
}
560599
}
561600

562601
}

0 commit comments

Comments
 (0)