Skip to content

Commit a52a5db

Browse files
authored
Fix crash on table creation when removing relation component (#297)
1 parent f972ed6 commit a52a5db

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## [[v0.5.1]](https://github.com/mlange-42/ark/compare/v0.5.0...v0.5.1)
4+
5+
### Bugfixes
6+
7+
- Fixes crash on table creation when removing a relation component (#297, fixes #295)
8+
39
## [[v0.5.0]](https://github.com/mlange-42/ark/compare/v0.4.8...v0.5.0)
410

511
### Breaking changes

ecs/storage.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,21 @@ func (s *storage) findOrCreateTable(oldTable *table, add []ID, remove []ID, rela
7373
}
7474

7575
var allRelations []RelationID
76-
if len(relations) > 0 {
77-
allRelations = appendNew(oldTable.relationIDs, relations...)
76+
if len(remove) > 0 {
77+
// filter out removed relations
78+
allRelations = make([]RelationID, 0, len(oldTable.relationIDs)+len(relations))
79+
for _, rel := range oldTable.relationIDs {
80+
if arch.mask.Get(rel.component) {
81+
allRelations = append(allRelations, rel)
82+
}
83+
}
84+
allRelations = append(allRelations, relations...)
7885
} else {
79-
allRelations = oldTable.relationIDs
86+
if len(relations) > 0 {
87+
allRelations = appendNew(oldTable.relationIDs, relations...)
88+
} else {
89+
allRelations = oldTable.relationIDs
90+
}
8091
}
8192
table, ok := arch.GetTable(s, allRelations)
8293
if !ok {

ecs/table_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func TestTableMatches(t *testing.T) {
3535
posID := ComponentID[Position](&w)
3636
velID := ComponentID[Velocity](&w)
3737
childID := ComponentID[ChildOf](&w)
38+
child2ID := ComponentID[ChildOf2](&w)
3839

3940
compMap := make([]int16, maskTotalBits)
4041
compMap[1] = 0
@@ -48,6 +49,7 @@ func TestTableMatches(t *testing.T) {
4849
)
4950

5051
expectTrue(t, table.MatchesExact([]RelationID{{component: childID, target: Entity{2, 0}}}))
52+
expectTrue(t, table.MatchesExact([]RelationID{{component: childID, target: Entity{2, 0}}, {component: child2ID, target: Entity{2, 0}}}))
5153
expectFalse(t, table.MatchesExact([]RelationID{{component: childID, target: Entity{3, 0}}}))
5254

5355
expectPanics(t, func() {

ecs/world_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,30 @@ func TestWorldRelations(t *testing.T) {
245245
})
246246
}
247247

248+
func TestWorldRelationsBug295(t *testing.T) {
249+
world := NewWorld()
250+
parentMap := NewMap1[Position](&world)
251+
childMap := NewMap3[Velocity, ChildOf, ChildOf2](&world)
252+
253+
parent1 := parentMap.NewEntity(&Position{})
254+
parent2 := parentMap.NewEntity(&Position{})
255+
child := childMap.NewEntity(
256+
&Velocity{},
257+
&ChildOf{},
258+
&ChildOf2{},
259+
Rel[ChildOf](parent1),
260+
Rel[ChildOf2](parent2),
261+
)
262+
263+
inFarmMap1 := NewMap[ChildOf](&world)
264+
inFarmMap2 := NewMap[ChildOf2](&world)
265+
266+
inFarmMap1.Remove(child)
267+
expectEqual(t, parent2, inFarmMap2.GetRelation(child))
268+
269+
inFarmMap2.Remove(child)
270+
}
271+
248272
func TestWorldSetRelations(t *testing.T) {
249273
w := NewWorld(16)
250274

0 commit comments

Comments
 (0)