Skip to content

Commit

Permalink
fix IsRelation check to allow for non-struct components
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 committed Jan 30, 2024
1 parent d761903 commit f092209
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [[v0.10.1]](https://github.com/mlange-42/arche/compare/v0.10.0...v0.10.1)

### Bugfixes

* Fix IsRelation check to allow for non-struct components, like type aliases (#354)

## [[v0.10.0]](https://github.com/mlange-42/arche/compare/v0.9.0...v0.10.0)

### Highlights
Expand Down
2 changes: 1 addition & 1 deletion ecs/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (r *componentRegistry) unregisterLastComponent() {
}

func (r *componentRegistry) isRelation(tp reflect.Type) bool {
if tp.NumField() == 0 {
if tp.Kind() != reflect.Struct || tp.NumField() == 0 {
return false
}
field := tp.Field(0)
Expand Down
40 changes: 40 additions & 0 deletions ecs/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ type genericComp[T any] struct {
Value T
}

type callbackComp1 struct {
Callback func(a, b float64) float64
}

type callbackComp2 = func(a, b float64) float64

func TestTypeSizes(t *testing.T) {
printTypeSize[Entity]()
printTypeSize[entityIndex]()
Expand Down Expand Up @@ -116,3 +122,37 @@ func TestGenericComponents(t *testing.T) {
assert.True(t, world.Has(e3, id1))
assert.True(t, world.Has(e3, id2))
}

func TestCallbackComponents(t *testing.T) {
world := NewWorld()

id1 := ComponentID[callbackComp1](&world)
id2 := ComponentID[callbackComp2](&world)

e1 := world.NewEntityWith(
Component{
ID: id1,
Comp: &callbackComp1{Callback: func(a, b float64) float64 { return a + b }},
},
)

cb2 := callbackComp2(func(a, b float64) float64 { return a * b })
e2 := world.NewEntityWith(
Component{
ID: id1,
Comp: &callbackComp1{Callback: func(a, b float64) float64 { return a - b }},
},
Component{
ID: id2,
Comp: &cb2,
},
)

c1 := (*callbackComp1)(world.Get(e1, id1))
c2 := (*callbackComp1)(world.Get(e2, id1))
c3 := (*callbackComp2)(world.Get(e2, id2))

assert.Equal(t, 3.0, c1.Callback(2, 1))
assert.Equal(t, 1.0, c2.Callback(2, 1))
assert.Equal(t, 6.0, (*c3)(2, 3))
}
2 changes: 1 addition & 1 deletion generic/compiled.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (q *compiledQuery) Compile(w *ecs.World, include, optional, exclude []Comp,
panic(fmt.Sprintf("relation component %v not in filter", targetType))
}
isRelation := false
if targetType.NumField() > 0 {
if targetType.Kind() == reflect.Struct && targetType.NumField() > 0 {
field := targetType.Field(0)
isRelation = field.Type == relationType && field.Name == relationType.Name()
}
Expand Down

0 comments on commit f092209

Please sign in to comment.