To query all entities with a set of components assigned, use ``entityx::EntityManager::entities_with_components()``. This method will return only those entities that have *all* of the specified components associated with them, assigning each component pointer to the corresponding component instance:
```c++
-ComponentHandle<Position> position;
-ComponentHandle<Direction> direction;
+Position::Handle position;
+Direction::Handle direction;
for (Entity entity : entities.entities_with_components(position, direction)) {
// Do things with entity, position and direction.
}
To retrieve a component associated with an entity use ``entityx::Entity::component<C>()``:
```c++
-ComponentHandle<Position> position = entity.component<Position>();
+Position::Handle position = entity.component<Position>();
if (position) {
// Do stuff with position
}
```c++
struct MovementSystem : public System<MovementSystem> {
void update(entityx::EntityManager &es, entityx::EventManager &events, double dt) override {
- ComponentHandle<Position> position;
- ComponentHandle<Direction> direction;
+ Position::Handle position;
+ Direction::Handle direction;
for (Entity entity : es.entities_with_components(position, direction)) {
position->x += direction->x * dt;
position->y += direction->y * dt;
class CollisionSystem : public System<CollisionSystem> {
public:
void update(entityx::EntityManager &es, entityx::EventManager &events, double dt) override {
- ComponentHandle<Position> left_position, right_position;
+ Position::Handle left_position, right_position;
for (Entity left_entity : es.entities_with_components(left_position)) {
for (Entity right_entity : es.entities_with_components(right_position)) {
if (collide(left_position, right_position)) {
Entity e = em.create();
Entity f = em.create();
Entity g = em.create();
- std::vector<std::pair<ComponentHandle<Position>, ComponentHandle<Direction>>> position_directions;
+ std::vector<std::pair<Position::Handle, Direction::Handle>> position_directions;
position_directions.push_back(std::make_pair(
e.assign<Position>(1.0f, 2.0f), e.assign<Direction>(3.0f, 4.0f)));
position_directions.push_back(std::make_pair(
g.assign<Position>(5.0f, 6.0f);
int i = 0;
- ComponentHandle<Position> position;
+ Position::Handle position;
REQUIRE(3 == size(em.entities_with_components(position)));
- ComponentHandle<Direction> direction;
+ Direction::Handle direction;
for (auto unused_entity : em.entities_with_components(position, direction)) {
(void)unused_entity;
REQUIRE(position);
++i;
}
REQUIRE(2 == i);
- ComponentHandle<Tag> tag;
+ Tag::Handle tag;
i = 0;
for (auto unused_entity :
em.entities_with_components(position, direction, tag)) {
auto d = e.assign<Direction>(3.0, 4.0);
auto t = e.assign<Tag>("tag");
- ComponentHandle<Position> up;
- ComponentHandle<Direction> ud;
- ComponentHandle<Tag> ut;
+ Position::Handle up;
+ Direction::Handle ud;
+ Tag::Handle ut;
e.unpack(up);
REQUIRE(p == up);
e.unpack(up, ud);
removed = event.component;
}
- ComponentHandle<Direction> removed;
+ Direction::Handle removed;
};
ComponentRemovedReceiver receiver;
REQUIRE(!(receiver.removed));
Entity e = em.create();
- ComponentHandle<Direction> p = e.assign<Direction>(1.0, 2.0);
+ Direction::Handle p = e.assign<Direction>(1.0, 2.0);
e.remove<Direction>();
REQUIRE(receiver.removed == p);
REQUIRE(!(e.component<Direction>()));
TEST_CASE_METHOD(EntityManagerFixture, "TestComponentHandleInvalidatedWhenEntityDestroyed") {
Entity a = em.create();
- ComponentHandle<Position> position = a.assign<Position>(1, 2);
+ Position::Handle position = a.assign<Position>(1, 2);
REQUIRE(position);
REQUIRE(position->x == 1);
REQUIRE(position->y == 2);
TEST_CASE_METHOD(EntityManagerFixture, "TestComponentHandleInvalidatedWhenComponentDestroyed") {
Entity a = em.create();
- ComponentHandle<Position> position = a.assign<Position>(1, 2);
+ Position::Handle position = a.assign<Position>(1, 2);
REQUIRE(position);
REQUIRE(position->x == 1);
REQUIRE(position->y == 2);