Skip to content

Commit

Permalink
Add function ResourceTypeID to get resource ID from reflect.Type` (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 committed May 16, 2024
1 parent 14ec612 commit 6799138
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [[unpublished]](https://github.com/mlange-42/arche/compare/v0.12.0...main)

### Features

* Adds function `ResourceTypeID` to register/get a resource ID from a `reflect.Type` (#420)

### Other

* Fix component type in examples/base (#419)
Expand Down
10 changes: 10 additions & 0 deletions ecs/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ func ResourceID[T any](w *World) ResID {
return w.resourceID(tp)
}

// ResourceTypeID returns the [ResID] for a resource type.
// Registers the type if it is not already registered.
//
// See [ResourceID] for a more commonly used generic variant.
//
// The number of resources per [World] is limited to [MaskTotalBits].
func ResourceTypeID(w *World, tp reflect.Type) ResID {
return w.resourceID(tp)
}

// ResourceIDs returns a list of all registered resource IDs.
func ResourceIDs(w *World) []ResID {
intIds := w.resources.registry.IDs
Expand Down
9 changes: 7 additions & 2 deletions ecs/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ func TestCompResIDs(t *testing.T) {
posID := ComponentID[Position](&w)
rotID := ComponentID[rotation](&w)

tPosID := TypeID(&w, reflect.TypeOf(Position{}))
tRotID := TypeID(&w, reflect.TypeOf(rotation{}))

res1ID := ResourceID[Position](&w)
res2ID := ResourceID[Velocity](&w)

tPosID := TypeID(&w, reflect.TypeOf(Position{}))
tRotID := TypeID(&w, reflect.TypeOf(rotation{}))
tRes1ID := ResourceTypeID(&w, reflect.TypeOf(Position{}))
tRes2ID := ResourceTypeID(&w, reflect.TypeOf(Velocity{}))

assert.Equal(t, posID, tPosID)
assert.Equal(t, rotID, tRotID)
assert.Equal(t, res1ID, tRes1ID)
assert.Equal(t, res2ID, tRes2ID)

assert.Equal(t, uint8(0), posID.id)
assert.Equal(t, uint8(1), rotID.id)
Expand Down

0 comments on commit 6799138

Please sign in to comment.