Skip to content

Commit

Permalink
#124 add relationship component query examples
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Apr 19, 2022
1 parent c050110 commit 2cc26ec
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
29 changes: 23 additions & 6 deletions examples/c/relations/relation_component/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ int main(int argc, char *argv[]) {
ecs_entity_t e1 = ecs_new_id(ecs);
ecs_set_pair(ecs, e1, Requires, Gigawatts, {1.21});
const Requires *r = ecs_get_pair(ecs, e1, Requires, Gigawatts);
printf("requires: %f\n", r->amount);
printf("requires: %.2f\n", r->amount);

// The component can be either the first or second part of a pair:
ecs_entity_t e2 = ecs_new_id(ecs);
ecs_set_pair_second(ecs, e2, Gigawatts, Requires, {1.21});
r = ecs_get_pair_second(ecs, e2, Gigawatts, Requires);
printf("requires: %f\n", r->amount);
printf("requires: %.2f\n", r->amount);

// Note that <Requires, Gigawatts> and <Gigawatts, Requires> are two
// different pairs, and can be added to an entity at the same time.
Expand All @@ -50,7 +50,7 @@ int main(int argc, char *argv[]) {
ecs_entity_t e3 = ecs_new_id(ecs);
ecs_set_pair(ecs, e3, Expires, ecs_id(Position), {0.5});
const Expires *e = ecs_get_pair(ecs, e3, Expires, ecs_id(Position));
printf("expires: %f\n", e->timeout);
printf("expires: %.1f\n", e->timeout);

// You can prevent a pair from assuming the type of a component by adding
// the Tag property to a relationship:
Expand Down Expand Up @@ -84,12 +84,29 @@ int main(int argc, char *argv[]) {
printf("%s\n", type_str);
ecs_os_free(type_str);

// When querying for a relationship, provide both parts of the pair:
ecs_query_t *q = ecs_query_init(ecs, &(ecs_query_desc_t) {
.filter.terms = {
{ .id = ecs_pair(ecs_id(Requires), Gigawatts) }
}
});

// When iterating, always use the pair type:
ecs_iter_t it = ecs_query_iter(ecs, q);
while (ecs_query_next(&it)) {
r = ecs_term(&it, Requires, 1);
for (int i = 0; i < it.count; i ++) {
printf("requires %.2f gigawatts\n", r[i].amount);
}
}

// Output:
// requires: 1.210000
// requires: 1.210000
// expires: 0.500000
// requires: 1.21
// requires: 1.21
// expires: 0.5
// Requires
// Requires
// Expires
// 0
// requires 1.21 gigawatts
}
12 changes: 12 additions & 0 deletions examples/cpp/relations/relation_component/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ int main(int, char*[]) {
std::cout << ecs.pair<Expires, Position>().type_id().path() << "\n";
std::cout << ecs.pair<MustHave, Position>().type_id().path() << "\n";

// When querying for a relationship component, add the pair type as template
// argument to the builder:
auto q = ecs.query_builder<Requires>()
.arg(1).obj<Gigawatts>() // set second part of pair for first term
.build();

// When iterating, always use the pair type:
q.each([](Requires& rq) {
std::cout << "requires " << rq.amount << " gigawatts\n";
});

// Output:
// requires: 1.21
// requires: 1.21
Expand All @@ -68,4 +79,5 @@ int main(int, char*[]) {
// ::Requires
// ::Expires
// 0
// requires 1.21 gigawatts
}

0 comments on commit 2cc26ec

Please sign in to comment.