#include #include // Prefabs are entities that can be used as templates for other entities. They // are created with a builtin Prefab tag, which by default excludes them from // queries and systems. struct Attack { double value; }; struct Defense { double value; }; struct FreightCapacity { double value; }; struct ImpulseSpeed { double value; }; struct HasFTL { }; struct Position { double x = 0; double y = 0; }; int main(int, char *[]) { flecs::world ecs; // Create a prefab hierarchy. flecs::entity spaceship = ecs.prefab("Spaceship") // Add components to prefab entity as usual .set({50}) .set({50}) // By default components in an inheritance hierarchy are shared between // entities. The override function ensures that instances have a private // copy of the component. .override(); flecs::entity freighter = ecs.prefab("Freighter") // Short for .add(flecs::IsA, spaceship). This ensures the entity // inherits all components from spaceship. .is_a(spaceship) .add() .set({100}) .set({100}); flecs::entity mammoth_freighter = ecs.prefab("MammothFreighter") .is_a(freighter) .set({500}) .set({300}); ecs.prefab("Frigate") .is_a(spaceship) .add() .set({100}) .set({75}) .set({125}); // Create a regular entity from a prefab. // The instance will have a private copy of the Position component, because // of the override in the spaceship entity. All other components are shared. flecs::entity inst = ecs.entity("my_mammoth_freighter") .is_a(mammoth_freighter); // Inspect the type of the entity. This outputs: // Position,(Identifier,Name),(IsA,MammothFreighter) std::cout << "Instance type: [" << inst.type().str() << "]\n"; // Even though the instance doesn't have a private copy of ImpulseSpeed, we // can still get it using the regular API (outputs 50) const ImpulseSpeed *ptr = inst.get(); std::cout << "Impulse speed: " << ptr->value << "\n"; // Prefab components can be iterated just like regular components: ecs.each([](flecs::entity e, ImpulseSpeed& is, Position& p) { p.x += is.value; std::cout << e.name() << ": {" << p.x << ", " << p.y << "}\n"; }); }