Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IsRelation check to allow for non-struct components #354

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# 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)

### Other

* Repository [arche-demo](https://github.com/mlange-42/arche-demo) provides a [live demo](https://mlange-42.github.io/arche-demo/)
of several models built with Arche.

## [[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
Loading