Skip to content

Commit

Permalink
component ID getters in world and query copy ID slice
Browse files Browse the repository at this point in the history
  • Loading branch information
mlange-42 committed Jan 12, 2024
1 parent 20b35b2 commit c04cfb5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion ecs/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,12 @@ func (q *Query) Mask() Mask {
}

// Ids returns the component IDs for the archetype of the [Entity] at the iterator's current position.
//
// Returns a copy of the archetype's component IDs slice, for safety.
// This means that the result can be manipulated safely,
// but also that calling the method may incur some significant cost.
func (q *Query) Ids() []ID {
return q.archetype.node.Ids
return append([]ID{}, q.archetype.node.Ids...)
}

// Close closes the Query and unlocks the world.
Expand Down
6 changes: 5 additions & 1 deletion ecs/world.go
Original file line number Diff line number Diff line change
Expand Up @@ -1045,11 +1045,15 @@ func (w *World) Mask(entity Entity) Mask {
}

// Ids returns the component IDs for the archetype of the given [Entity].
//
// Returns a copy of the archetype's component IDs slice, for safety.
// This means that the result can be manipulated safely,
// but also that calling the method may incur some significant cost.
func (w *World) Ids(entity Entity) []ID {
if !w.entityPool.Alive(entity) {
panic("can't get mask for a dead entity")
}
return w.entities[entity.id].arch.node.Ids
return append([]ID{}, w.entities[entity.id].arch.node.Ids...)
}

// ComponentType returns the reflect.Type for a given component ID, as well as whether the ID is in use.
Expand Down

0 comments on commit c04cfb5

Please sign in to comment.