Flecs v3.2
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
untyped_component.inl
Go to the documentation of this file.
1
14untyped_component& member(flecs::entity_t type_id, flecs::entity_t unit, const char *name, int32_t count = 0, size_t offset = 0) {
15 ecs_entity_desc_t desc = {};
16 desc.name = name;
17 desc.add[0] = ecs_pair(flecs::ChildOf, m_id);
18 ecs_entity_t eid = ecs_entity_init(m_world, &desc);
19 ecs_assert(eid != 0, ECS_INTERNAL_ERROR, NULL);
20
21 flecs::entity e(m_world, eid);
22
23 Member m = {};
24 m.type = type_id;
25 m.unit = unit;
26 m.count = count;
27 m.offset = static_cast<int32_t>(offset);
28 e.set<Member>(m);
29
30 return *this;
31}
32
34untyped_component& member(flecs::entity_t type_id, const char* name, int32_t count = 0, size_t offset = 0) {
35 return member(type_id, 0, name, count, offset);
36}
37
39template <typename MemberType>
40untyped_component& member(const char *name, int32_t count = 0, size_t offset = 0) {
41 flecs::entity_t type_id = _::cpp_type<MemberType>::id(m_world);
42 return member(type_id, name, count, offset);
43}
44
46template <typename MemberType>
47untyped_component& member(flecs::entity_t unit, const char *name, int32_t count = 0, size_t offset = 0) {
48 flecs::entity_t type_id = _::cpp_type<MemberType>::id(m_world);
49 return member(type_id, unit, name, count, offset);
50}
51
53template <typename MemberType, typename UnitType>
54untyped_component& member(const char *name, int32_t count = 0, size_t offset = 0) {
55 flecs::entity_t type_id = _::cpp_type<MemberType>::id(m_world);
56 flecs::entity_t unit_id = _::cpp_type<UnitType>::id(m_world);
57 return member(type_id, unit_id, name, count, offset);
58}
59
61template <typename MemberType, typename ComponentType, typename RealType = typename std::remove_extent<MemberType>::type>
62untyped_component& member(const char* name, const MemberType ComponentType::* ptr) {
63 flecs::entity_t type_id = _::cpp_type<RealType>::id(m_world);
64 size_t offset = reinterpret_cast<size_t>(&(static_cast<ComponentType*>(nullptr)->*ptr));
65 return member(type_id, name, std::extent<MemberType>::value, offset);
66}
67
69template <typename MemberType, typename ComponentType, typename RealType = typename std::remove_extent<MemberType>::type>
70untyped_component& member(flecs::entity_t unit, const char* name, const MemberType ComponentType::* ptr) {
71 flecs::entity_t type_id = _::cpp_type<RealType>::id(m_world);
72 size_t offset = reinterpret_cast<size_t>(&(static_cast<ComponentType*>(nullptr)->*ptr));
73 return member(type_id, unit, name, std::extent<MemberType>::value, offset);
74}
75
77template <typename UnitType, typename MemberType, typename ComponentType, typename RealType = typename std::remove_extent<MemberType>::type>
78untyped_component& member(const char* name, const MemberType ComponentType::* ptr) {
79 flecs::entity_t type_id = _::cpp_type<RealType>::id(m_world);
80 flecs::entity_t unit_id = _::cpp_type<UnitType>::id(m_world);
81 size_t offset = reinterpret_cast<size_t>(&(static_cast<ComponentType*>(nullptr)->*ptr));
82 return member(type_id, unit_id, name, std::extent<MemberType>::value, offset);
83}
84
86untyped_component& constant(const char *name, int32_t value) {
87 ecs_add_id(m_world, m_id, _::cpp_type<flecs::Enum>::id(m_world));
88
89 ecs_entity_desc_t desc = {};
90 desc.name = name;
91 desc.add[0] = ecs_pair(flecs::ChildOf, m_id);
92 ecs_entity_t eid = ecs_entity_init(m_world, &desc);
93 ecs_assert(eid != 0, ECS_INTERNAL_ERROR, NULL);
94
95 ecs_set_id(m_world, eid,
96 ecs_pair(flecs::Constant, flecs::I32), sizeof(int32_t),
97 &value);
98
99 return *this;
100}
101
103untyped_component& bit(const char *name, uint32_t value) {
104 ecs_add_id(m_world, m_id, _::cpp_type<flecs::Bitmask>::id(m_world));
105
106 ecs_entity_desc_t desc = {};
107 desc.name = name;
108 desc.add[0] = ecs_pair(flecs::ChildOf, m_id);
109 ecs_entity_t eid = ecs_entity_init(m_world, &desc);
110 ecs_assert(eid != 0, ECS_INTERNAL_ERROR, NULL);
111
112 ecs_set_id(m_world, eid,
113 ecs_pair(flecs::Constant, flecs::U32), sizeof(uint32_t),
114 &value);
115
116 return *this;
117}
118
120template <typename Elem>
121untyped_component& array(int32_t elem_count) {
122 ecs_array_desc_t desc = {};
123 desc.entity = m_id;
124 desc.type = _::cpp_type<Elem>::id(m_world);
125 desc.count = elem_count;
126 ecs_array_init(m_world, &desc);
127 return *this;
128}
129
131untyped_component& range(double min, double max) {
132 const flecs::member_t *m = ecs_cpp_last_member(m_world, m_id);
133 if (!m) {
134 return *this;
135 }
136
137 flecs::world w(m_world);
138 flecs::entity me = w.entity(m->member);
140 mr->value.min = min;
141 mr->value.max = max;
143 return *this;
144}
145
147untyped_component& warning_range(double min, double max) {
148 const flecs::member_t *m = ecs_cpp_last_member(m_world, m_id);
149 if (!m) {
150 return *this;
151 }
152
153 flecs::world w(m_world);
154 flecs::entity me = w.entity(m->member);
156 mr->warning.min = min;
157 mr->warning.max = max;
159 return *this;
160}
161
163untyped_component& error_range(double min, double max) {
164 const flecs::member_t *m = ecs_cpp_last_member(m_world, m_id);
165 if (!m) {
166 return *this;
167 }
168
169 flecs::world w(m_world);
170 flecs::entity me = w.entity(m->member);
172 mr->error.min = min;
173 mr->error.max = max;
175 return *this;
176}
177
178
flecs::entity_t type_id()
Get id currently assigned to component.
void ecs_add_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Add a (component) id to an entity.
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:351
FLECS_API ecs_entity_t ecs_array_init(ecs_world_t *world, const ecs_array_desc_t *desc)
Create a new array type.
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:305
untyped_component & member(const char *name, int32_t count=0, size_t offset=0)
Add member.
untyped_component & member(flecs::entity_t type_id, const char *name, int32_t count=0, size_t offset=0)
Add member.
untyped_component & constant(const char *name, int32_t value)
Add constant.
untyped_component & member(flecs::entity_t unit, const char *name, const MemberType ComponentType::*ptr)
Add member with unit using pointer-to-member.
untyped_component & warning_range(double min, double max)
Add member warning range.
untyped_component & member(const char *name, int32_t count=0, size_t offset=0)
Add member with unit.
untyped_component & array(int32_t elem_count)
Register array metadata for component.
untyped_component & range(double min, double max)
Add member value range.
untyped_component & member(const char *name, const MemberType ComponentType::*ptr)
Add member using pointer-to-member.
untyped_component & member(const char *name, const MemberType ComponentType::*ptr)
Add member with unit using pointer-to-member.
untyped_component & bit(const char *name, uint32_t value)
Add bitmask constant.
untyped_component & error_range(double min, double max)
Add member error range.
untyped_component & member(flecs::entity_t unit, const char *name, int32_t count=0, size_t offset=0)
Add member with unit.
untyped_component & member(flecs::entity_t type_id, flecs::entity_t unit, const char *name, int32_t count=0, size_t offset=0)
Add member with unit.
flecs::entity entity(Args &&... args) const
Create an entity.
ecs_entity_t ecs_entity_init(ecs_world_t *world, const ecs_entity_desc_t *desc)
Find or create an entity.
ecs_entity_t ecs_set_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id, size_t size, const void *ptr)
Set the value of a component.
Component added to member entities to express valid value ranges.
Definition meta.h:211
Component added to member entities.
Definition meta.h:197
Used with ecs_array_init().
Definition meta.h:796
ecs_entity_t entity
Existing entity to use for type (optional)
Definition meta.h:797
Used with ecs_entity_init().
Definition flecs.h:898
const char * name
Name of the entity.
Definition flecs.h:903
ecs_id_t add[(32)]
Array of ids to add to the new or existing entity.
Definition flecs.h:929
Element type of members vector in EcsStruct.
Definition meta.h:218
Entity.
Definition entity.hpp:30
T * get_mut() const
Get mutable component value.
Definition entity.hpp:92
void modified() const
Signal that component was modified.
Definition entity.hpp:170
Untyped component class.
The world.
Definition world.hpp:132