Flecs v3.2
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...

Collaboration diagram for Creation macro's:

Macros

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

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:305

Declare & define a component.

Example:

ECS_COMPONENT(world, Position);
#define ECS_COMPONENT(world, id)
Declare & define a component.
Definition flecs_c.h:145

Definition at line 145 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
});
#define ecs_component(world,...)
Shorthand for creating a component with ecs_component_init().
Definition flecs_c.h:214

Definition at line 214 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 112 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:969
ecs_type_info_t type
Parameters for type (size, hooks, ...)
Definition flecs.h:976
ecs_entity_t entity
Existing entity to associate with observer (optional)
Definition flecs.h:973
Used with ecs_entity_init().
Definition flecs.h:898
const char * name
Name of the entity.
Definition flecs.h:903
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:924
const char * symbol
Optional entity symbol.
Definition flecs.h:914
ecs_entity_t id
Set to modify existing entity (optional)
Definition flecs.h:901
ecs_size_t alignment
Alignment of type.
Definition flecs.h:883
ecs_size_t size
Size of type.
Definition flecs.h:882

Define a forward declared component.

Example:

ECS_COMPONENT_DEFINE(world, Position);
#define ECS_COMPONENT_DEFINE(world, id_)
Define a forward declared component.
Definition flecs_c.h:122

Definition at line 122 of file flecs_c.h.

◆ ecs_component_t

#define ecs_component_t ( world,
T )
Value:
.entity = ecs_entity(world, { \
.name = #T, \
.symbol = #T, \
.use_low_id = true \
}), \
.type.size = ECS_SIZEOF(T), \
.type.alignment = ECS_ALIGNOF(T) \
})
#define ecs_entity(world,...)
Shorthand for creating an entity with ecs_entity_init().
Definition flecs_c.h:200

Shorthand for creating a component from a type.

Example:

ecs_component_t(world, Position);
#define ecs_component_t(world, T)
Shorthand for creating a component from a type.
Definition flecs_c.h:225

Definition at line 225 of file flecs_c.h.

◆ ECS_DECLARE

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

Definition at line 25 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);
#define ECS_ENTITY(world, id,...)
Declare & define an entity.
Definition flecs_c.h:60

Definition at line 60 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 200 of file flecs_c.h.

◆ ECS_ENTITY_DECLARE

#define ECS_ENTITY_DECLARE   ECS_DECLARE

Forward declare an entity.

Definition at line 29 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:932

Define a forward declared entity.

Example:

ECS_ENTITY_DEFINE(world, MyEntity, Position, Velocity);
#define ECS_ENTITY_DEFINE(world, id_,...)
Define a forward declared entity.
Definition flecs_c.h:39

Definition at line 39 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) }}
});
#define ecs_filter(world,...)
Shorthand for creating a filter with ecs_filter_init().
Definition flecs_c.h:246

Definition at line 246 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);
const ecs_entity_t EcsOnAdd
Event that triggers when an id is added to an entity.
#define ECS_OBSERVER(world, id, kind,...)
Declare & define an observer.
Definition flecs_c.h:183

Definition at line 183 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
});
#define ecs_observer(world,...)
Shorthand for creating an observer with ecs_observer_init().
Definition flecs_c.h:274

Definition at line 274 of file flecs_c.h.

◆ ECS_OBSERVER_DECLARE

#define ECS_OBSERVER_DECLARE ( id)    ecs_entity_t ecs_id(id)

Definition at line 151 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:1010
Used with ecs_observer_init().
Definition flecs.h:1089
ecs_entity_t entity
Existing entity to associate with observer (optional)
Definition flecs.h:1093
ecs_filter_desc_t filter
Filter for observer.
Definition flecs.h:1096
ecs_entity_t events[(8)]
Events to observe (OnAdd, OnRemove, OnSet, UnSet)
Definition flecs.h:1099
ecs_iter_action_t callback
Callback to invoke on an event, invoked when the observer matches.
Definition flecs.h:1107

Define a forward declared observer.

Example:

ECS_OBSERVER_DEFINE(world, AddPosition, EcsOnAdd, Position);
#define ECS_OBSERVER_DEFINE(world, id_, kind,...)
Define a forward declared observer.
Definition flecs_c.h:161

Definition at line 161 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);
#define ECS_PREFAB(world, id,...)
Declare & define a prefab.
Definition flecs_c.h:109

Definition at line 109 of file flecs_c.h.

◆ ECS_PREFAB_DECLARE

#define ECS_PREFAB_DECLARE   ECS_DECLARE

Forward declare a prefab.

Definition at line 89 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);
#define ECS_PREFAB_DEFINE(world, id,...)
Define a forward declared prefab.
Definition flecs_c.h:99

Definition at line 99 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) }}
});
#define ecs_query(world,...)
Shorthand for creating a query with ecs_query_init().
Definition flecs_c.h:259

Definition at line 259 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);
#define ECS_TAG(world, id)
Declare & define a tag.
Definition flecs_c.h:86

Definition at line 86 of file flecs_c.h.

◆ ECS_TAG_DECLARE

#define ECS_TAG_DECLARE   ECS_DECLARE

Forward declare a tag.

Definition at line 66 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);
#define ECS_TAG_DEFINE(world, id)
Define a forward declared tag.
Definition flecs_c.h:76

Definition at line 76 of file flecs_c.h.