vecs
Fast, flexible ecs in C++ with ergonomic API
Loading...
Searching...
No Matches
vecs::Query< Components > Class Template Reference

Represents a query for components within the ECS world. More...

#include <vecs.h>

Classes

class  Iterator
 Iterator for iterating over entities matching the query. More...
 
struct  Record
 A container for a single record and its components, as a result of a query. More...
 
struct  Single
 A container for a single entity and its components, as a result of a query. More...
 

Public Types

template<typename T >
using QueryTypeTraits = _traits::query::QueryTypeTraits<T>
 
template<typename T >
using ComponentStorageType = typename QueryTypeTraits<T>::StorageType
 
using QueryComponents
 

Public Member Functions

 Query (World *world)
 Constructs a Query to search for entities with specified components.
 
Iterator begin ()
 Returns an iterator to the beginning of the query results.
 
Iterator end ()
 Returns an iterator to the end of the query results.
 
Record get (Entity entity)
 Retrieves the components of an entity matching the query.
 
Record get_single ()
 Retrieves the components of a single entity matching the query.
 

Static Public Member Functions

static ComponentMask compute_mask ()
 

Detailed Description

template<typename... Components>
class vecs::Query< Components >

Represents a query for components within the ECS world.

This class provides an interface for querying entities that match a set of components. It allows iteration over matching entities and provides methods to retrieve individual results.

Template Parameters
ComponentsThe types of components to be queried.

Member Typedef Documentation

◆ QueryComponents

template<typename... Components>
using vecs::Query< Components >::QueryComponents
Initial value:
std::vector<std::tuple<ComponentStorageType<Components>...>>

Constructor & Destructor Documentation

◆ Query()

template<typename... Components>
vecs::Query< Components >::Query ( World * world)
inline

Constructs a Query to search for entities with specified components.

Parameters
worldPointer to the ECS world containing the entities and components.

Member Function Documentation

◆ begin()

template<typename... Components>
Iterator vecs::Query< Components >::begin ( )
inline

Returns an iterator to the beginning of the query results.

Returns
An iterator to the beginning of the entities that match the query.

◆ end()

template<typename... Components>
Iterator vecs::Query< Components >::end ( )
inline

Returns an iterator to the end of the query results.

Returns
An iterator to the end of the entities that match the query.

◆ get()

template<typename... Components>
Record vecs::Query< Components >::get ( Entity entity)
inline

Retrieves the components of an entity matching the query.

Parameters
entityThe entity to retrieve the components for.
Returns
A single Record containing the entity's components, or an invalid Record if the entity does not match the query.

Example usage:

auto e = world.spawn().insert(123, 321.f);
world.add_system([&e](Query<int, Optional<float>>& query) {
Record record = query.get(e);
if (record) {
auto [i, maybe_f] = *record;
printf("The record int component = %i\n", i);
if (maybe_f) {
printf("The record float component = %f\n", *maybe_f);
}
else {
printf("The record does not have a float component\n");
}
}
});
Provides an interface for interacting with optional components.
Definition vecs.h:1088
Represents a query for components within the ECS world.
Definition vecs.h:1270
A container for a single record and its components, as a result of a query.
Definition vecs.h:1455

◆ get_single()

template<typename... Components>
Record vecs::Query< Components >::get_single ( )
inline

Retrieves the components of a single entity matching the query.

This method is intended to be used with queries that are expected to match exactly one entity. If the query does not match exactly one entity, an invalid Record is returned.

Returns
A single Record containing the matched component, or an invalid Record if no components or multiple components match the query.

Example usage:

auto e = world.spawn().insert(123, 321.f);
world.add_system([](Query<Entity, int, float>& query) {
if (auto record = query.get_single()) {
auto [e, i, f] = *record;
printf("A single entity %zu contains an int and float component = %i, %f\n", e, i, f);
}
});
Record get_single()
Retrieves the components of a single entity matching the query.
Definition vecs.h:1596

The documentation for this class was generated from the following file: