Flecs v3.1
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
Creation macro's

Convenience macro's for creating entities, components and observers. More...

Macros

#define ECS_DECLARE(id)    ecs_entity_t id, ecs_id(id)
 
#define ECS_ENTITY_DECLARE   ECS_DECLARE
 Forward declare an entity. More...
 
#define ECS_ENTITY_DEFINE(world, id_, ...)
 Define a forward declared entity. More...
 
#define ECS_ENTITY(world, id, ...)
 Declare & define an entity. More...
 
#define ECS_TAG_DECLARE   ECS_DECLARE
 Forward declare a tag. More...
 
#define ECS_TAG_DEFINE(world, id)   ECS_ENTITY_DEFINE(world, id, 0)
 Define a forward declared tag. More...
 
#define ECS_TAG(world, id)   ECS_ENTITY(world, id, 0)
 Declare & define a tag. More...
 
#define ECS_PREFAB_DECLARE   ECS_DECLARE
 Forward declare a prefab. More...
 
#define ECS_PREFAB_DEFINE(world, id, ...)   ECS_ENTITY_DEFINE(world, id, Prefab, __VA_ARGS__)
 Define a forward declared prefab. More...
 
#define ECS_PREFAB(world, id, ...)   ECS_ENTITY(world, id, Prefab, __VA_ARGS__)
 Declare & define a prefab. More...
 
#define ECS_COMPONENT_DECLARE(id)   ecs_entity_t ecs_id(id)
 Forward declare a component. More...
 
#define ECS_COMPONENT_DEFINE(world, id_)
 Define a forward declared component. More...
 
#define ECS_COMPONENT(world, id)
 Declare & define a component. More...
 
#define ECS_OBSERVER_DECLARE(id)   ecs_entity_t ecs_id(id)
 
#define ECS_OBSERVER_DEFINE(world, id_, kind, ...)
 Define a forward declared observer. More...
 
#define ECS_OBSERVER(world, id, kind, ...)
 Declare & define an observer. More...
 
#define ecs_entity(world, ...)    ecs_entity_init(world, &(ecs_entity_desc_t) __VA_ARGS__ )
 Shorthand for creating an entity with ecs_entity_init. More...
 
#define ecs_component(world, ...)    ecs_component_init(world, &(ecs_component_desc_t) __VA_ARGS__ )
 Shorthand for creating a component with ecs_component_init. More...
 
#define ecs_filter(world, ...)    ecs_filter_init(world, &(ecs_filter_desc_t) __VA_ARGS__ )
 Shorthand for creating a filter with ecs_filter_init. More...
 
#define ecs_query(world, ...)    ecs_query_init(world, &(ecs_query_desc_t) __VA_ARGS__ )
 Shorthand for creating a query with ecs_query_init. More...
 
#define ecs_observer(world, ...)    ecs_observer_init(world, &(ecs_observer_desc_t) __VA_ARGS__ )
 Shorthand for creating an observer with ecs_observer_init. More...
 

Detailed Description

Convenience macro's for creating entities, components and observers.

Macro Definition Documentation

◆ ECS_COMPONENT

#define ECS_COMPONENT (   world,
  id 
)
Value:
ecs_entity_t ecs_id(id) = 0;\
ECS_COMPONENT_DEFINE(world, id);\
(void)ecs_id(id)
ecs_id_t ecs_entity_t
An entity identifier.
Definition: flecs.h:220

Declare & define a component.

Example: ECS_COMPONENT(world, Position);

Definition at line 120 of file flecs_c.h.

◆ ecs_component

#define ecs_component (   world,
  ... 
)     ecs_component_init(world, &(ecs_component_desc_t) __VA_ARGS__ )

Shorthand for creating a component with ecs_component_init.

Example: ecs_component(world, { .type.size = 4, .type.alignment = 4 });

Definition at line 177 of file flecs_c.h.

◆ ECS_COMPONENT_DECLARE

#define ECS_COMPONENT_DECLARE (   id)    ecs_entity_t ecs_id(id)

Forward declare a component.

Definition at line 93 of file flecs_c.h.

◆ ECS_COMPONENT_DEFINE

#define ECS_COMPONENT_DEFINE (   world,
  id_ 
)
Value:
{\
ecs_component_desc_t desc = {0}; \
ecs_entity_desc_t edesc = {0}; \
edesc.id = ecs_id(id_); \
edesc.use_low_id = true; \
edesc.name = #id_; \
edesc.symbol = #id_; \
desc.entity = ecs_entity_init(world, &edesc); \
desc.type.size = ECS_SIZEOF(id_); \
desc.type.alignment = ECS_ALIGNOF(id_); \
ecs_id(id_) = ecs_component_init(world, &desc);\
ecs_assert(ecs_id(id_) != 0, ECS_INVALID_PARAMETER, NULL);\
}
ecs_entity_t ecs_component_init(ecs_world_t *world, const ecs_component_desc_t *desc)
Find or create a component.
ecs_entity_t ecs_entity_init(ecs_world_t *world, const ecs_entity_desc_t *desc)
Find or create an entity.
Used with ecs_component_init.
Definition: flecs.h:756
ecs_type_info_t type
Parameters for type (size, hooks, ...)
Definition: flecs.h:763
ecs_entity_t entity
Existing entity to associate with observer (optional)
Definition: flecs.h:760
Used with ecs_entity_init.
Definition: flecs.h:685
const char * name
Name of the entity.
Definition: flecs.h:690
bool use_low_id
When set to true, a low id (typically reserved for components) will be used to create the entity,...
Definition: flecs.h:711
const char * symbol
Optional entity symbol.
Definition: flecs.h:701
ecs_entity_t id
Set to modify existing entity (optional)
Definition: flecs.h:688
ecs_size_t alignment
Alignment of type.
Definition: flecs.h:670
ecs_size_t size
Size of type.
Definition: flecs.h:669

Define a forward declared component.

Example: ECS_COMPONENT_DEFINE(world, Position);

Definition at line 100 of file flecs_c.h.

◆ ECS_DECLARE

#define ECS_DECLARE (   id)     ecs_entity_t id, ecs_id(id)

Definition at line 24 of file flecs_c.h.

◆ ECS_ENTITY

#define ECS_ENTITY (   world,
  id,
  ... 
)
Value:
ecs_entity_t ecs_id(id); \
ecs_entity_t id = 0; \
ECS_ENTITY_DEFINE(world, id, __VA_ARGS__);

Declare & define an entity.

Example: ECS_ENTITY(world, MyEntity, Position, Velocity);

Definition at line 53 of file flecs_c.h.

◆ ecs_entity

#define ecs_entity (   world,
  ... 
)     ecs_entity_init(world, &(ecs_entity_desc_t) __VA_ARGS__ )

Shorthand for creating an entity with ecs_entity_init.

Example: ecs_entity(world, { .name = "MyEntity" });

Definition at line 166 of file flecs_c.h.

◆ ECS_ENTITY_DECLARE

#define ECS_ENTITY_DECLARE   ECS_DECLARE

Forward declare an entity.

Definition at line 28 of file flecs_c.h.

◆ ECS_ENTITY_DEFINE

#define ECS_ENTITY_DEFINE (   world,
  id_,
  ... 
)
Value:
{ \
ecs_entity_desc_t desc = {0}; \
desc.id = id_; \
desc.name = #id_; \
desc.add_expr = #__VA_ARGS__; \
id_ = ecs_entity_init(world, &desc); \
ecs_id(id_) = id_; \
ecs_assert(id_ != 0, ECS_INVALID_PARAMETER, NULL); \
} \
(void)id_; \
(void)ecs_id(id_);
const char * add_expr
String expression with components to add.
Definition: flecs.h:719

Define a forward declared entity.

Example: ECS_ENTITY_DEFINE(world, MyEntity, Position, Velocity);

Definition at line 35 of file flecs_c.h.

◆ ecs_filter

#define ecs_filter (   world,
  ... 
)     ecs_filter_init(world, &(ecs_filter_desc_t) __VA_ARGS__ )

Shorthand for creating a filter with ecs_filter_init.

Example: ecs_filter(world, { .terms = {{ ecs_id(Position) }} });

Definition at line 187 of file flecs_c.h.

◆ ECS_OBSERVER

#define ECS_OBSERVER (   world,
  id,
  kind,
  ... 
)
Value:
ecs_entity_t ecs_id(id) = 0; \
ECS_OBSERVER_DEFINE(world, id, kind, __VA_ARGS__);\
ecs_entity_t id = ecs_id(id);\
(void)ecs_id(id);\
(void)id;

Declare & define an observer.

Example: ECS_OBSERVER(world, AddPosition, EcsOnAdd, Position);

Definition at line 152 of file flecs_c.h.

◆ ecs_observer

#define ecs_observer (   world,
  ... 
)     ecs_observer_init(world, &(ecs_observer_desc_t) __VA_ARGS__ )

Shorthand for creating an observer with ecs_observer_init.

Example: ecs_observer(world, { .filter.terms = {{ ecs_id(Position) }}, .events = { EcsOnAdd }, .callback = AddPosition });

Definition at line 209 of file flecs_c.h.

◆ ECS_OBSERVER_DECLARE

#define ECS_OBSERVER_DECLARE (   id)    ecs_entity_t ecs_id(id)

Definition at line 126 of file flecs_c.h.

◆ ECS_OBSERVER_DEFINE

#define ECS_OBSERVER_DEFINE (   world,
  id_,
  kind,
  ... 
)
Value:
{\
ecs_observer_desc_t desc = {0};\
ecs_entity_desc_t edesc = {0}; \
edesc.id = ecs_id(id_); \
edesc.name = #id_; \
desc.entity = ecs_entity_init(world, &edesc); \
desc.callback = id_;\
desc.filter.expr = #__VA_ARGS__;\
desc.events[0] = kind;\
ecs_id(id_) = ecs_observer_init(world, &desc);\
ecs_assert(ecs_id(id_) != 0, ECS_INVALID_PARAMETER, NULL);\
}
ecs_entity_t ecs_observer_init(ecs_world_t *world, const ecs_observer_desc_t *desc)
Create observer.
const char * expr
Filter expression.
Definition: flecs.h:797
Used with ecs_observer_init.
Definition: flecs.h:864
ecs_entity_t entity
Existing entity to associate with observer (optional)
Definition: flecs.h:868
ecs_filter_desc_t filter
Filter for observer.
Definition: flecs.h:871
ecs_entity_t events[(8)]
Events to observe (OnAdd, OnRemove, OnSet, UnSet)
Definition: flecs.h:874
ecs_iter_action_t callback
Callback to invoke on an event, invoked when the observer matches.
Definition: flecs.h:882

Define a forward declared observer.

Example: ECS_OBSERVER_DEFINE(world, AddPosition, EcsOnAdd, Position);

Definition at line 133 of file flecs_c.h.

◆ ECS_PREFAB

#define ECS_PREFAB (   world,
  id,
  ... 
)    ECS_ENTITY(world, id, Prefab, __VA_ARGS__)

Declare & define a prefab.

Example: ECS_PREFAB(world, MyPrefab, Position, Velocity);

Definition at line 90 of file flecs_c.h.

◆ ECS_PREFAB_DECLARE

#define ECS_PREFAB_DECLARE   ECS_DECLARE

Forward declare a prefab.

Definition at line 76 of file flecs_c.h.

◆ ECS_PREFAB_DEFINE

#define ECS_PREFAB_DEFINE (   world,
  id,
  ... 
)    ECS_ENTITY_DEFINE(world, id, Prefab, __VA_ARGS__)

Define a forward declared prefab.

Example: ECS_PREFAB_DEFINE(world, MyPrefab, Position, Velocity);

Definition at line 83 of file flecs_c.h.

◆ ecs_query

#define ecs_query (   world,
  ... 
)     ecs_query_init(world, &(ecs_query_desc_t) __VA_ARGS__ )

Shorthand for creating a query with ecs_query_init.

Example: ecs_query(world, { .filter.terms = {{ ecs_id(Position) }} });

Definition at line 197 of file flecs_c.h.

◆ ECS_TAG

#define ECS_TAG (   world,
  id 
)    ECS_ENTITY(world, id, 0)

Declare & define a tag.

Example: ECS_TAG(world, MyTag);

Definition at line 73 of file flecs_c.h.

◆ ECS_TAG_DECLARE

#define ECS_TAG_DECLARE   ECS_DECLARE

Forward declare a tag.

Definition at line 59 of file flecs_c.h.

◆ ECS_TAG_DEFINE

#define ECS_TAG_DEFINE (   world,
  id 
)    ECS_ENTITY_DEFINE(world, id, 0)

Define a forward declared tag.

Example: ECS_TAG_DEFINE(world, MyTag);

Definition at line 66 of file flecs_c.h.