Query¶
fr::Query provides a fluent API for filtering and querying entities by their component composition. Queries support include/exclude filters, various terminal operations for collecting or processing matching entities, and can be scheduled for async execution.
Obtain a Query instance via Scene::CreateQuery():
Filter configuration¶
Excluding<Ts...>¶
Adds component types to the exclusion filter. Entities with any of the specified components will be excluded from query results.
Terminal operations¶
Count<Ts...>¶
Returns the total number of entities matching the query.
EntitiesWith<Ts...>¶
Returns all entity IDs that have all specified component types.
FindUnique<Ts...>¶
Asserts that exactly one entity matches, then returns it. Returns std::nullopt if zero or more than one entity matches. Useful for singleton entities (camera, player).
First<Ts...>¶
Returns the first entity matching the query, or std::nullopt if none found.
Iterate<Ts...>¶
Collects all matching entities and their components into a vector of tuples.
auto entities = query->Iterate<Position, Velocity>();
for (auto&& [entity, pos, vel] : entities) {
// ...
}
Transform<Ts...>¶
Maps each entity to a transformed value. Callback can accept either (Entity, Ts&...) or (Ts&...).
auto distances = query->Transform<Position>([origin](Entity, Position& pos) {
return std::sqrt(pos.x * pos.x + pos.y * pos.y);
});
Map<Ts...>¶
Applies a transform function and returns results as a vector, ordered by entity.
auto distances = query->Map<Position>([origin](Entity e, Position& pos) {
float dx = pos.x - origin.x;
float dy = pos.y - origin.y;
return std::sqrt(dx*dx + dy*dy);
});
Reduce<Ts...>¶
Accumulates values across all matching entities. Callback signature: (ResultType, Ts&...) -> ResultType.
auto totalHealth = query->Reduce<Health>([](float acc, Health& h) {
return acc + h.current;
}, 0.f);
Iteration¶
Each<Ts...>¶
Synchronous iteration over all matching entities. Callback receives (Entity, Ts&...).
query->Including<Position, Velocity>()
->WithLabel("UpdatePositions")
->Each<Position, Velocity>([](Entity e, Position& pos, Velocity& vel) {
pos.x += vel.dx * dt;
});
EachAsync<Ts...>¶
Dispatches chunk tasks to the thread pool for parallel execution. Call ExecuteTasks() on the scene to wait for completion.
query->EachAsync<Position, Velocity>([](Entity e, Position& pos, Velocity& vel) {
pos.x += vel.dx * dt;
});
scene->ExecuteTasks();
Utility¶
WithLabel¶
Assigns a human-readable label for profiling and debugging. When FREYR_PROFILING=ON, the label appears in Perfetto traces.
Notes¶
- Query instances should not be stored long-term as they hold references to
ComponentManager. - Use
Scene::CreateQuery()to obtain a fresh query instance when needed. - The QueryAggregator coordinates async query execution across worker threads.