Skip to content

Commit

Permalink
replace function ComponentType by more comprehensive ComponentInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 committed Jan 11, 2024
1 parent fdf6139 commit f7a1840
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ This change was necessary to get the same performance as before, despite the mor

### Features

* Adds functions `ComponentType(*World, ID) reflect.Type` and `ResourceType(*World, ID) reflect.Type` (#315)
* Adds methods `World.Ids(Entity) []ID` and `Query.Ids() []ID` (#315)
* Adds functions `ComponentInfo(*World, ID)` and `ResourceType(*World, ResID)` (#315, #318)
* Adds methods `World.Ids(Entity)` and `Query.Ids()` (#315)

### Other

Expand Down
8 changes: 8 additions & 0 deletions ecs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,11 @@ type componentType struct {
ID
Type reflect.Type
}

// CompInfo provides information about a registered component.
// Returned by [ComponentInfo].
type CompInfo struct {
ID ID
Type reflect.Type
IsRelation bool
}
15 changes: 12 additions & 3 deletions ecs/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ func TypeID(w *World, tp reflect.Type) ID {
return w.componentID(tp)
}

// ComponentType returns the reflect.Type for a component [ID], and whether the ID is assigned.
func ComponentType(w *World, id ID) (reflect.Type, bool) {
return w.registry.ComponentType(id)
// ComponentInfo returns the [CompInfo] for a component [ID], and whether the ID is assigned.
func ComponentInfo(w *World, id ID) (CompInfo, bool) {
tp, ok := w.registry.ComponentType(id)
if !ok {
return CompInfo{}, false
}

return CompInfo{
ID: id,
Type: tp,
IsRelation: w.registry.IsRelation.Get(id),
}, true
}

// ResourceID returns the [ResID] for a resource type via generics.
Expand Down

0 comments on commit f7a1840

Please sign in to comment.