Skip to content

Commit 2cc26ec

Browse files
committed
#124 add relationship component query examples
1 parent c050110 commit 2cc26ec

File tree

2 files changed

+35
-6
lines changed
  • examples
    • c/relations/relation_component/src
    • cpp/relations/relation_component/src

2 files changed

+35
-6
lines changed

examples/c/relations/relation_component/src/main.c

+23-6
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ int main(int argc, char *argv[]) {
3434
ecs_entity_t e1 = ecs_new_id(ecs);
3535
ecs_set_pair(ecs, e1, Requires, Gigawatts, {1.21});
3636
const Requires *r = ecs_get_pair(ecs, e1, Requires, Gigawatts);
37-
printf("requires: %f\n", r->amount);
37+
printf("requires: %.2f\n", r->amount);
3838

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

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

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

87+
// When querying for a relationship, provide both parts of the pair:
88+
ecs_query_t *q = ecs_query_init(ecs, &(ecs_query_desc_t) {
89+
.filter.terms = {
90+
{ .id = ecs_pair(ecs_id(Requires), Gigawatts) }
91+
}
92+
});
93+
94+
// When iterating, always use the pair type:
95+
ecs_iter_t it = ecs_query_iter(ecs, q);
96+
while (ecs_query_next(&it)) {
97+
r = ecs_term(&it, Requires, 1);
98+
for (int i = 0; i < it.count; i ++) {
99+
printf("requires %.2f gigawatts\n", r[i].amount);
100+
}
101+
}
102+
87103
// Output:
88-
// requires: 1.210000
89-
// requires: 1.210000
90-
// expires: 0.500000
104+
// requires: 1.21
105+
// requires: 1.21
106+
// expires: 0.5
91107
// Requires
92108
// Requires
93109
// Expires
94110
// 0
111+
// requires 1.21 gigawatts
95112
}

examples/cpp/relations/relation_component/src/main.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ int main(int, char*[]) {
6060
std::cout << ecs.pair<Expires, Position>().type_id().path() << "\n";
6161
std::cout << ecs.pair<MustHave, Position>().type_id().path() << "\n";
6262

63+
// When querying for a relationship component, add the pair type as template
64+
// argument to the builder:
65+
auto q = ecs.query_builder<Requires>()
66+
.arg(1).obj<Gigawatts>() // set second part of pair for first term
67+
.build();
68+
69+
// When iterating, always use the pair type:
70+
q.each([](Requires& rq) {
71+
std::cout << "requires " << rq.amount << " gigawatts\n";
72+
});
73+
6374
// Output:
6475
// requires: 1.21
6576
// requires: 1.21
@@ -68,4 +79,5 @@ int main(int, char*[]) {
6879
// ::Requires
6980
// ::Expires
7081
// 0
82+
// requires 1.21 gigawatts
7183
}

0 commit comments

Comments
 (0)