Flecs v4.1
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
component.inl
Go to the documentation of this file.
1
7template <typename Func>
8component& opaque(const Func& type_support) {
9 flecs::world world(world_);
10 auto ts = type_support(world);
11 ts.desc.entity = _::type<T>::id(world_);
12 ecs_opaque_init(world_, &ts.desc);
13 return *this;
14}
15
18 return flecs::opaque<T>(world_).as_type(as_type);
19}
20
23 return this->opaque(as_type.id());
24}
25
28 return this->opaque(as_type.id());
29}
30
32template <typename ElemType>
36
38component<T>& constant(const char *name, T value) {
39 using U = typename std::underlying_type<T>::type;
40
41 ecs_add_id(world_, id_, _::type<flecs::Enum>::id(world_));
42
43 ecs_entity_desc_t desc = {};
44 desc.name = name;
45 desc.parent = id_;
46 ecs_entity_t eid = ecs_entity_init(world_, &desc);
47 ecs_assert(eid != 0, ECS_INTERNAL_ERROR, NULL);
48
49 flecs::id_t pair = ecs_pair(flecs::Constant, _::type<U>::id(world_));
50 U *ptr = static_cast<U*>(ecs_ensure_id(world_, eid, pair, sizeof(U)));
51 *ptr = static_cast<U>(value);
52 ecs_modified_id(world_, eid, pair);
53
54 // If we're not using automatic enum reflection, manually set static data.
55 #ifdef FLECS_CPP_NO_ENUM_REFLECTION
56 auto et = enum_type<T>(world_);
57 et.register_constant(world_, static_cast<U>(value), eid);
58 #endif
59
60 return *this;
61}
component & opaque(const Func &type_support)
Register an opaque type interface.
Definition component.inl:8
component< T > & constant(const char *name, T value)
Add a constant.
Definition component.inl:38
void ecs_add_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Add a (component) ID to an entity.
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_INTERNAL_ERROR
Internal error code.
Definition log.h:671
FLECS_API ecs_entity_t ecs_opaque_init(ecs_world_t *world, const ecs_opaque_desc_t *desc)
Create a new opaque type.
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:381
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
ecs_entity_t ecs_entity_init(ecs_world_t *world, const ecs_entity_desc_t *desc)
Find or create an entity.
void ecs_modified_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Signal that a component has been modified.
void * ecs_ensure_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component, size_t size)
Ensure an entity has a component and return a pointer.
Used with ecs_entity_init().
Definition flecs.h:1042
const char * name
Name of the entity.
Definition flecs.h:1049
ecs_entity_t parent
Parent entity.
Definition flecs.h:1047
entity_t id() const
Get entity ID.
Entity.
Definition entity.hpp:30
Type-safe interface for opaque types.
Definition opaque.hpp:39
opaque & as_type(flecs::id_t func)
Set the type that describes the kind/structure of the opaque type.
Definition opaque.hpp:47
Untyped component class.
The world.
Definition world.hpp:246