|
| 1 | +#include <instancing.h> |
| 2 | +#include <iostream> |
| 3 | + |
| 4 | +/* Instancing is the ability of queries to iterate results with fields that have |
| 5 | + * different numbers of elements. The term "instancing" is borrowed from |
| 6 | + * graphics APIs, where it means reusing the same data for multiple "instances". |
| 7 | + * |
| 8 | + * Query instancing works in much the same way. By default queries match all |
| 9 | + * components on the same entity. It is however possible to request data from |
| 10 | + * other entities, like getting the Position from the entity's parent. |
| 11 | + * |
| 12 | + * Instancing refers to the ability of queries to iterate components for |
| 13 | + * multiple entities while at the same time providing "instanced" components, |
| 14 | + * which are always provided one element at a time. |
| 15 | + * |
| 16 | + * Instancing is often used in combination with parent-child relationships and |
| 17 | + * prefabs, but is applicable to any kind of query where some of the terms are |
| 18 | + * matched on N entities, and some on a single entity. |
| 19 | + * |
| 20 | + * By default queries are not instanced, which means that if a result contains |
| 21 | + * mixed fields, entities will be iterated one by one instead of in batches. |
| 22 | + * This is safer, as code doesn't have to do anything different for owned and |
| 23 | + * shared fields, but does come at a performance penalty. |
| 24 | + * |
| 25 | + * The each() iterator function always uses an instanced iterator under the |
| 26 | + * hood. This is transparent to the application, but improves performance. For |
| 27 | + * this reason using each() can be faster than using uninstanced iter(). |
| 28 | + */ |
| 29 | + |
| 30 | +struct Position { |
| 31 | + double x, y; |
| 32 | +}; |
| 33 | + |
| 34 | +struct Velocity { |
| 35 | + double x, y; |
| 36 | +}; |
| 37 | + |
| 38 | +int main(int, char *[]) { |
| 39 | + flecs::world ecs; |
| 40 | + |
| 41 | + // Create a query for Position, Velocity. We'll create a few entities that |
| 42 | + // have Velocity as owned and shared component. |
| 43 | + auto q = ecs.query_builder<Position, const Velocity>() |
| 44 | + .arg(1).set(flecs::Self) // Ensure Position is never shared |
| 45 | + .instanced() // create instanced query |
| 46 | + .build(); |
| 47 | + |
| 48 | + // Create a prefab with Velocity. Prefabs are not matched with queries. |
| 49 | + auto prefab = ecs.prefab("p") |
| 50 | + .set<Velocity>({1, 2}); |
| 51 | + |
| 52 | + // Create a few entities that own Position & share Velocity from the prefab. |
| 53 | + ecs.entity("e1").is_a(prefab) |
| 54 | + .set<Position>({10, 20}); |
| 55 | + |
| 56 | + ecs.entity("e2").is_a(prefab) |
| 57 | + .set<Position>({10, 20}); |
| 58 | + |
| 59 | + // Create a few entities that own all components |
| 60 | + ecs.entity("e3") |
| 61 | + .set<Position>({10, 20}) |
| 62 | + .set<Velocity>({3, 4}); |
| 63 | + |
| 64 | + ecs.entity("e4") |
| 65 | + .set<Position>({10, 20}) |
| 66 | + .set<Velocity>({3, 4}); |
| 67 | + |
| 68 | + |
| 69 | + // Iterate the instanced query. Note how when a query is instanced, it needs |
| 70 | + // to check whether a field is owned or not in order to know how to access |
| 71 | + // it. In the case of an owned field it is iterated as an array, whereas |
| 72 | + // in the case of a shared field, it is accessed as a pointer. |
| 73 | + q.iter([](flecs::iter& it, Position *p, const Velocity *v) { |
| 74 | + |
| 75 | + // Check if Velocity is owned, in which case it's accessed as array. |
| 76 | + // Position will always be owned, since we set the term to Self. |
| 77 | + if (it.is_owned(2)) { // Velocity is term 2 |
| 78 | + std::cout << "Velocity is owned" << std::endl; |
| 79 | + for (auto i : it) { |
| 80 | + p[i].x += v[i].x; |
| 81 | + p[i].y += v[i].y; |
| 82 | + std::cout << it.entity(i).name() << |
| 83 | + ": {" << p[i].x << ", " << p[i].y << "}\n"; |
| 84 | + } |
| 85 | + |
| 86 | + // If Velocity is shared, access the field as a pointer. |
| 87 | + } else { |
| 88 | + std::cout << "Velocity is shared" << std::endl; |
| 89 | + for (auto i : it) { |
| 90 | + p[i].x += v->x; |
| 91 | + p[i].y += v->y; |
| 92 | + std::cout << it.entity(i).name() << |
| 93 | + ": {" << p[i].x << ", " << p[i].y << "}\n"; |
| 94 | + } |
| 95 | + } |
| 96 | + }); |
| 97 | +} |
0 commit comments