Flecs v4.0
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
entity.hpp
Go to the documentation of this file.
1
8#pragma once
9
10#include "entity_view.hpp"
12
21namespace flecs
22{
23
29struct entity : entity_builder<entity>
30{
32
37 explicit entity(world_t *world)
39 {
40 world_ = world;
41 if (!ecs_get_scope(world_) && !ecs_get_with(world_)) {
42 id_ = ecs_new(world);
43 } else {
44 ecs_entity_desc_t desc = {};
45 id_ = ecs_entity_init(world_, &desc);
46 }
47 }
48
54 explicit entity(const flecs::world_t *world, flecs::entity_t id) {
55 world_ = const_cast<flecs::world_t*>(world);
56 id_ = id;
57 }
58
68 explicit entity(world_t *world, const char *name)
70 {
71 world_ = world;
72
73 ecs_entity_desc_t desc = {};
74 desc.name = name;
75 desc.sep = "::";
76 desc.root_sep = "::";
77 id_ = ecs_entity_init(world, &desc);
78 }
79
84 explicit entity(entity_t id)
85 : entity_builder( nullptr, id ) { }
86
87 #ifndef ensure
88
98 template <typename T>
99 T& ensure() const {
100 auto comp_id = _::type<T>::id(world_);
101 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
102 "operation invalid for empty type");
103 return *static_cast<T*>(ecs_ensure_id(world_, id_, comp_id));
104 }
105
115 void* ensure(entity_t comp) const {
116 return ecs_ensure_id(world_, id_, comp);
117 }
118
125 template <typename First, typename Second, typename P = pair<First, Second>,
126 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
127 A& ensure() const {
128 return *static_cast<A*>(ecs_ensure_id(world_, id_, ecs_pair(
129 _::type<First>::id(world_),
130 _::type<Second>::id(world_))));
131 }
132
139 template <typename First>
140 First& ensure(entity_t second) const {
141 auto first = _::type<First>::id(world_);
142 ecs_assert(_::type<First>::size() != 0, ECS_INVALID_PARAMETER,
143 "operation invalid for empty type");
144 return *static_cast<First*>(
145 ecs_ensure_id(world_, id_, ecs_pair(first, second)));
146 }
147
156 void* ensure(entity_t first, entity_t second) const {
157 return ecs_ensure_id(world_, id_, ecs_pair(first, second));
158 }
159
166 template <typename Second>
167 Second& ensure_second(entity_t first) const {
168 auto second = _::type<Second>::id(world_);
169 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second)) != NULL,
170 ECS_INVALID_PARAMETER, "pair is not a component");
171 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second))->component == second,
172 ECS_INVALID_PARAMETER, "type of pair is not Second");
173 ecs_assert(_::type<Second>::size() != 0, ECS_INVALID_PARAMETER,
174 "operation invalid for empty type");
175 return *static_cast<Second*>(
176 ecs_ensure_id(world_, id_, ecs_pair(first, second)));
177 }
178
179 #endif
180
185 template <typename T>
186 void modified() const {
187 auto comp_id = _::type<T>::id(world_);
188 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
189 "operation invalid for empty type");
190 this->modified(comp_id);
191 }
192
198 template <typename First, typename Second, typename A = actual_type_t<flecs::pair<First, Second>>>
199 void modified() const {
200 auto first = _::type<First>::id(world_);
201 auto second = _::type<Second>::id(world_);
202 ecs_assert(_::type<A>::size() != 0, ECS_INVALID_PARAMETER,
203 "operation invalid for empty type");
204 this->modified(first, second);
205 }
206
212 template <typename First>
213 void modified(entity_t second) const {
214 auto first = _::type<First>::id(world_);
215 ecs_assert(_::type<First>::size() != 0, ECS_INVALID_PARAMETER,
216 "operation invalid for empty type");
217 this->modified(first, second);
218 }
219
227 void modified(entity_t first, entity_t second) const {
228 this->modified(ecs_pair(first, second));
229 }
230
235 void modified(entity_t comp) const {
236 ecs_modified_id(world_, id_, comp);
237 }
238
246 template <typename T, if_t< is_actual<T>::value > = 0>
247 ref<T> get_ref() const {
248 return ref<T>(world_, id_, _::type<T>::id(world_));
249 }
250
260 template <typename T, typename A = actual_type_t<T>, if_t< flecs::is_pair<T>::value > = 0>
261 ref<A> get_ref() const {
262 return ref<A>(world_, id_,
263 ecs_pair(_::type<typename T::first>::id(world_),
265 }
266
267
268 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
269 typename A = actual_type_t<P>>
270 ref<A> get_ref() const {
271 return ref<A>(world_, id_,
272 ecs_pair(_::type<First>::id(world_), _::type<Second>::id(world_)));
273 }
274
275 template <typename First>
276 ref<First> get_ref(flecs::entity_t second) const {
277 auto first = _::type<First>::id(world_);
278 return ref<First>(world_, id_, ecs_pair(first, second));
279 }
280
281 template <typename Second>
282 ref<Second> get_ref_second(flecs::entity_t first) const {
283 auto second = _::type<Second>::id(world_);
284 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second)) != NULL,
285 ECS_INVALID_PARAMETER, "pair is not a component");
286 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second))->component == second,
287 ECS_INVALID_PARAMETER, "type of pair is not Second");
288 return ref<Second>(world_, id_, ecs_pair(first, second));
289 }
290
297 void clear() const {
298 ecs_clear(world_, id_);
299 }
300
307 void destruct() const {
308 ecs_delete(world_, id_);
309 }
310
320 return flecs::entity_view(
321 const_cast<flecs::world_t*>(ecs_get_world(world_)), id_);
322 }
323
330 static
331 flecs::entity null(const flecs::world_t *world) {
332 flecs::entity result;
333 result.world_ = const_cast<flecs::world_t*>(world);
334 return result;
335 }
336
337 static
338 flecs::entity null() {
339 return flecs::entity();
340 }
341
342# ifdef FLECS_JSON
343# include "mixins/json/entity.inl"
344# endif
345};
346
347} // namespace flecs
348
Entity class with only readonly operations.
void ecs_clear(ecs_world_t *world, ecs_entity_t entity)
Clear all components.
ecs_id_t ecs_get_with(const ecs_world_t *world)
Get current with id.
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:352
const ecs_type_info_t * ecs_get_type_info(const ecs_world_t *world, ecs_id_t id)
Get the type for an id.
ecs_entity_t ecs_new(ecs_world_t *world)
Create new entity id.
ecs_entity_t ecs_entity_init(ecs_world_t *world, const ecs_entity_desc_t *desc)
Find or create an entity.
void ecs_delete(ecs_world_t *world, ecs_entity_t entity)
Delete an entity.
void * ecs_ensure_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Get a mutable pointer to a component.
void ecs_modified_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Signal that a component has been modified.
ecs_entity_t ecs_get_scope(const ecs_world_t *world)
Get the current scope.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
Entity builder.
Used with ecs_entity_init().
Definition flecs.h:938
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:950
const char * root_sep
Optional, used for identifiers relative to root.
Definition flecs.h:954
const char * name
Name of the entity.
Definition flecs.h:945
Component class.
Entity builder.
Definition builder.hpp:15
flecs::string_view name() const
Return the entity name.
entity_t id() const
Get entity id.
Entity.
Definition entity.hpp:30
void destruct() const
Delete an entity.
Definition entity.hpp:307
ref< T > get_ref() const
Get reference to component.
Definition entity.hpp:247
void modified(entity_t second) const
Signal that the first part of a pair was modified.
Definition entity.hpp:213
void modified() const
Signal that the first element of a pair was modified.
Definition entity.hpp:199
First & ensure(entity_t second) const
Get mutable pointer for the first element of a pair.
Definition entity.hpp:140
void modified(entity_t comp) const
Signal that component was modified.
Definition entity.hpp:235
void clear() const
Clear an entity.
Definition entity.hpp:297
void modified(entity_t first, entity_t second) const
Signal that a pair has modified (untyped).
Definition entity.hpp:227
entity(world_t *world, const char *name)
Create a named entity.
Definition entity.hpp:68
void * ensure(entity_t first, entity_t second) const
Get mutable pointer for a pair (untyped).
Definition entity.hpp:156
entity(const flecs::world_t *world, flecs::entity_t id)
Wrap an existing entity id.
Definition entity.hpp:54
void modified() const
Signal that component was modified.
Definition entity.hpp:186
T & ensure() const
Get mutable component value.
Definition entity.hpp:99
static flecs::entity null(const flecs::world_t *world)
Entity id 0.
Definition entity.hpp:331
Second & ensure_second(entity_t first) const
Get mutable pointer for the second element of a pair.
Definition entity.hpp:167
void * ensure(entity_t comp) const
Get mutable component value (untyped).
Definition entity.hpp:115
entity(entity_t id)
Conversion from flecs::entity_t to flecs::entity.
Definition entity.hpp:84
entity(world_t *world)
Create entity.
Definition entity.hpp:37
flecs::entity_view view() const
Return entity as entity_view.
Definition entity.hpp:319
ref< A > get_ref() const
Get reference to component.
Definition entity.hpp:261
A & ensure() const
Get mutable pointer for a pair.
Definition entity.hpp:127
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
flecs::entity second() const
Get second element from a pair.
Definition impl.hpp:31
flecs::entity first() const
Get first element from a pair.
Definition impl.hpp:20
Component reference.
Definition ref.hpp:23
The world.
Definition world.hpp:137