query<C extends Component> method

  1. @override
Iterable<C> query<C extends Component>()
override

Queries the component set (typically Component.children) for components of type C.

Example:

final myComponents = world.children.query<MyCustomComponent>();

This is equivalent to world.children.whereType<MyCustomComponent>() except that query is O(1).

The function returns an Iterable. In past versions of Flame, it was a modifiable List but modifying this list would have been a bug.

When strictMode is true, you must call register for every type C you desire to use. Use something like:

world.children.register<MyCustomComponent>();

Implementation

@override
Iterable<C> query<C extends Component>() {
  // We are returning an iterable (view) here to avoid hard-to-detect
  // bugs where the user assumes the query is a unique result list
  // and they start doing things like `removeWhere()`.
  // This would remove components from the component set itself
  // (but not from the game)!
  return super.query();
}