Skip to main content

View Class Template

A view class to iterate over entities having specific components. More...

Declaration

template <typename... Components> class helios::engine::ecs::View<Components> { ... }

Public Constructors Index

template <typename... Components>
View (EntityManager *em)

Constructs the view and retrieves the necessary component sets. More...

Public Member Functions Index

template <typename T>
View &exclude ()

Excludes entities that have a specific component. More...

template <typename... Components>
View &whereEnabled ()

Filters to only include entities with enabled components. More...

template <typename... Components>
Iteratorbegin ()

Returns an iterator to the first valid entity. More...

template <typename... Components>
Iteratorend ()

Returns an iterator to the end (past the last entity). More...

Private Member Attributes Index

template <typename... Components>
EntityManager *em_
template <typename... Components>
std::tuple< SparseSet< Components > *... >includeSets_

Pointers to the SparseSets of the included components. More...

template <typename... Components>
std::vector< std::function< bool(EntityId)> >excludeChecks_

List of exclusion predicates. * Stores functions that return true if an entity should be EXCLUDED. Operates on EntityId (index) because the SparseSet uses it internally. More...

template <typename... Components>
boolfilterEnabledOnly_ = false

Flag to filter only enabled components. More...

template <typename... Components>
boolfilterActiveOnly_ = false

Description

A view class to iterate over entities having specific components.

The View acts as a lightweight iterator over the SparseSets of the requested components. It uses the first component type (Lead) as the primary iterator and cross-references existence in other sets.

## Usage

```cpp for (auto [entity, transform, velocity, active] : gameWorld->view< TransformComponent, VelocityComponent, Active >().whereEnabled()) { // Process entity } ```

Template Parameters
Components

The component types to query for.

See Also

EntityManager

See Also

SparseSet

Definition at line 48 of file View.ixx.

Public Constructors

View()

template <typename... Components>
helios::engine::ecs::View< Components >::View (EntityManager * em)
inline explicit

Constructs the view and retrieves the necessary component sets.

Parameters
em

Pointer to the EntityManager to retrieve sets and construct GameObjects.

Definition at line 78 of file View.ixx.

78 explicit View(EntityManager* em) : em_(em) {
79 // Retrieve pointers to the specific component sets immediately.
80 includeSets_ = std::make_tuple(em_->getSparseSet<Components>()...);
81 };

Referenced by helios::engine::ecs::View< Components >::exclude, helios::engine::ecs::View< Components >::Iterator::Iterator and helios::engine::ecs::View< Components >::whereEnabled.

Public Member Functions

begin()

template <typename... Components>
Iterator helios::engine::ecs::View< Components >::begin ()
inline nodiscard

Returns an iterator to the first valid entity.

Uses the first component type's SparseSet as the lead. If the first entity is invalid, advances to the next valid one.

Returns

Iterator to the first valid entity, or end() if none found.

Definition at line 298 of file View.ixx.

298 [[nodiscard]] Iterator begin() {
299 auto* leadSet = std::get<0>(includeSets_);
300
301 if (!leadSet) {
302 return Iterator{};
303 }
304
305 Iterator it{leadSet->begin(), leadSet->end(), this};
306
307 if (!it.isValid()) {
308 it.advance();
309 }
310
311 return it;
312 }

References helios::engine::ecs::View< Components >::Iterator::advance and helios::engine::ecs::View< Components >::Iterator::isValid.

end()

template <typename... Components>
Iterator helios::engine::ecs::View< Components >::end ()
inline nodiscard

Returns an iterator to the end (past the last entity).

Returns

End iterator for comparison.

Definition at line 319 of file View.ixx.

319 [[nodiscard]] Iterator end() {
320 auto* leadSet = std::get<0>(includeSets_);
321
322 if (!leadSet) {
323 return Iterator{};
324 }
325
326 return Iterator{leadSet->end(), leadSet->end(), this};
327 }

Referenced by helios::engine::ecs::View< Components >::Iterator::Iterator.

exclude()

template <typename T>
View & helios::engine::ecs::View< Components >::exclude ()
inline

Excludes entities that have a specific component.

Entities possessing the specified component type will be skipped during iteration. Multiple exclusions can be chained.

```cpp // Skip entities with Shield or Invincible for (auto [e, health] : world->view<HealthComponent>() .exclude<ShieldComponent>() .exclude<InvincibleComponent>()) { // Only vulnerable entities } ```

Template Parameters
T

The component type to exclude.

Returns

Reference to this View for method chaining.

Definition at line 103 of file View.ixx.

104 auto* set = em_->getSparseSet<T>();
105
106 if (set) {
107 excludeChecks_.emplace_back([set](EntityId entityId) {
108 return set->contains(entityId);
109 });
110 }
111 return *this;
112 }

Reference helios::engine::ecs::View< Components >::View.

whereEnabled()

template <typename... Components>
View & helios::engine::ecs::View< Components >::whereEnabled ()
inline

Filters to only include entities with enabled components.

Components must implement `isEnabled()` returning bool. Components without this method are assumed to be enabled.

Returns

Reference to this View for method chaining.

Definition at line 122 of file View.ixx.

123 filterEnabledOnly_ = true;
124 return *this;
125 }

Reference helios::engine::ecs::View< Components >::View.

Private Member Attributes

em_

template <typename... Components>
EntityManager* helios::engine::ecs::View< Components >::em_

Definition at line 51 of file View.ixx.

51 EntityManager* em_;

excludeChecks_

template <typename... Components>
std::vector<std::function<bool(EntityId)> > helios::engine::ecs::View< Components >::excludeChecks_

List of exclusion predicates. * Stores functions that return true if an entity should be EXCLUDED. Operates on EntityId (index) because the SparseSet uses it internally.

Definition at line 63 of file View.ixx.

63 std::vector<std::function<bool(EntityId)>> excludeChecks_;

filterActiveOnly_

template <typename... Components>
bool helios::engine::ecs::View< Components >::filterActiveOnly_ = false

Definition at line 70 of file View.ixx.

70 bool filterActiveOnly_ = false;

filterEnabledOnly_

template <typename... Components>
bool helios::engine::ecs::View< Components >::filterEnabledOnly_ = false

Flag to filter only enabled components.

Definition at line 68 of file View.ixx.

68 bool filterEnabledOnly_ = false;

includeSets_

template <typename... Components>
std::tuple<SparseSet<Components>*... > helios::engine::ecs::View< Components >::includeSets_

Pointers to the SparseSets of the included components.

Definition at line 56 of file View.ixx.

56 std::tuple<SparseSet<Components>*... > includeSets_;

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.