Skip to content
Flecs v4.1
queries/singleton

  • C

    View on GitHub

    1#include <singleton.h>
    2#include <stdio.h>
    3
    4// This example shows how to use singleton components in queries.
    5
    6// Singleton component
    7typedef struct {
    8 double value;
    9} Gravity;
    10
    11// Entity component
    12typedef struct {
    13 double x, y;
    14} Velocity;
    15
    16int main(int argc, char *argv[]) {
    17 ecs_world_t *ecs = ecs_init_w_args(argc, argv);
    18
    19 ECS_COMPONENT(ecs, Velocity);
    20 ECS_COMPONENT(ecs, Gravity);
    21
    22 // Mark Gravity as singleton
    23 ecs_add_id(ecs, ecs_id(Gravity), EcsSingleton);
    24
    25 // Set singleton
    26 ecs_singleton_set(ecs, Gravity, { 9.81 });
    27
    28 ecs_entity_t e1 = ecs_entity(ecs, { .name = "e1" });
    29 ecs_entity_t e2 = ecs_entity(ecs, { .name = "e2" });
    30 ecs_entity_t e3 = ecs_entity(ecs, { .name = "e3" });
    31
    32 // Set Velocity
    33 ecs_set(ecs, e1, Velocity, {0, 0});
    34 ecs_set(ecs, e2, Velocity, {0, 1});
    35 ecs_set(ecs, e3, Velocity, {0, 2});
    36
    37 // Create query that matches Gravity singleton
    38 ecs_query_t *q = ecs_query(ecs, {
    39 .terms = {
    40 { .id = ecs_id(Velocity) },
    41 { .id = ecs_id(Gravity) }
    42 }
    43 });
    44
    45 ecs_iter_t it = ecs_query_iter(ecs, q);
    46 while (ecs_query_next(&it)) {
    47 Velocity *v = ecs_field(&it, Velocity, 0);
    48 Gravity *g = ecs_field(&it, Gravity, 1);
    49
    50 for (int i = 0; i < it.count; i ++) {
    51 // Make sure to access g as a pointer, as we only have a single
    52 // instance of the Gravity component.
    53 v[i].y += g->value;
    54
    55 printf("%s velocity is {%f, %f}\n",
    56 ecs_get_name(ecs, it.entities[i]), v[i].x, v[i].y);
    57 }
    58 }
    59
    60 // Output
    61 // e1 velocity is {0.000000, 9.810000}
    62 // e2 velocity is {0.000000, 10.810000}
    63 // e3 velocity is {0.000000, 11.810000}
    64
    65 ecs_fini(ecs);
    66}
    void ecs_add_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
    Add a (component) ID to an entity.
    FLECS_API const ecs_entity_t EcsSingleton
    Mark component as singleton.
    FLECS_API const ecs_entity_t ecs_id(EcsDocDescription)
    Component ID for EcsDocDescription.
    ecs_id_t ecs_entity_t
    An entity identifier.
    Definition flecs.h:395
    struct ecs_world_t ecs_world_t
    A world is the container for all ECS data and supporting features.
    Definition flecs.h:439
    #define ecs_entity(world,...)
    Shorthand for creating an entity with ecs_entity_init().
    Definition flecs_c.h:188
    #define ECS_COMPONENT(world, id)
    Declare and define a component.
    Definition flecs_c.h:98
    #define ecs_query(world,...)
    Shorthand for creating a query with ecs_query_init().
    Definition flecs_c.h:215
    #define ecs_set(world, entity, component,...)
    Set a component value.
    Definition flecs_c.h:310
    #define ecs_field(it, T, index)
    Get field data for a component.
    Definition flecs_c.h:720
    #define ecs_singleton_set(world, comp,...)
    Set a singleton component value.
    Definition flecs_c.h:460
    const char * ecs_get_name(const ecs_world_t *world, ecs_entity_t entity)
    Get the name of an entity.
    bool ecs_query_next(ecs_iter_t *it)
    Progress a query iterator.
    ecs_iter_t ecs_query_iter(const ecs_world_t *world, const ecs_query_t *query)
    Create a query iterator.
    int ecs_fini(ecs_world_t *world)
    Delete a world.
    ecs_world_t * ecs_init_w_args(int argc, char *argv[])
    Create a new world with arguments.
    Iterator.
    Definition flecs.h:1183
    Queries are lists of constraints (terms) that match entities.
    Definition flecs.h:858

  • C++

    View on GitHub

    1#include <singleton.h>
    2#include <iostream>
    3
    4// This example shows how to use singleton components in queries.
    5
    6// Singleton component
    7struct Gravity {
    8 double value;
    9};
    10
    11// Entity component
    12struct Velocity {
    13 double x;
    14 double y;
    15};
    16
    17int main(int, char *[]) {
    18 flecs::world world;
    19
    20 // Mark Gravity as singleton
    21 world.component<Gravity>().add(flecs::Singleton);
    22
    23 // Set singleton
    24 world.set<Gravity>({ 9.81 });
    25
    26 // Set Velocity
    27 world.entity("e1").set<Velocity>({0, 0});
    28 world.entity("e2").set<Velocity>({0, 1});
    29 world.entity("e3").set<Velocity>({0, 2});
    30
    31 // Create query that matches Gravity singleton
    33 world.query<Velocity, const Gravity>();
    34
    35 q.each([](flecs::entity e, Velocity& v, const Gravity& g) {
    36 v.y += g.value;
    37 std::cout << e.path() << " velocity is {"
    38 << v.x << ", " << v.y << "}" << std::endl;
    39 });
    40
    41 // Output
    42 // ::e1 velocity is {0, 9.81}
    43 // ::e2 velocity is {0, 10.81}
    44 // ::e3 velocity is {0, 11.81}
    45}
    flecs::component< T > component(Args &&... args) const
    Find or register a component.
    Definition impl.hpp:11
    conditional_t< sizeof...(Args)==0 &&_::is_sparse_query< Comps... >::value, flecs::sparse_query< Comps... >, flecs::query< Comps... > > query(Args &&... args) const
    Create a query.
    Definition impl.hpp:326
    flecs::entity entity(Args &&... args) const
    Create an entity.
    Definition impl.hpp:195
    flecs::string path(const char *sep="::", const char *init_sep="::") const
    Return the entity path.
    Entity.
    Definition entity.hpp:30
    Typed query.
    Definition impl.hpp:271
    The world.
    Definition world.hpp:129