Skip to content

EntityBag

Simon edited this page Jan 4, 2025 · 3 revisions

A bag is a common data structure in ECS world which is, in simple terms, a dynamic array. In Fleks an entity bag is an array that automatically resizes and has no gaps. If you remove an entity then the last entity of the bag gets moved to its location. Therefore, ordering is not granted unless you explicitly call sort.

Fleks provides an EntityBag interface and a MutableEntityBag implementation. A family stores its entities internally also in a MutableEntityBag which can be accessed via family.entities.

The EntityBag provides a lot of common Kotlin collection functionality like iteration, filtering, mapping, association, etc..

Since Fleks 2.11 there is also an EntityBagIterator class that provides more convenience for iterating (back and forth and also in a loop) over a bag. It can be created via bag.iterator(). Here is a list of the iterator's functions:

  • hasNext
  • next which has a loop parameter which is set to false per default. If set to true, then the iteration will start from the beginning again, if you are at the end.
  • hasPrevious
  • previous which also has a loop parameter similar to next
  • reset
  • goToFirst which will go to the first entity matching the given predicate
  • goToLast which is similar to goToFirst
Clone this wiki locally