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
91 explicit entity(world_t *world, const char *name, const char *sep, const char *root_sep)
93 {
94 world_ = world;
95
96 ecs_entity_desc_t desc = {};
97 desc.name = name;
98 desc.sep = sep;
99 desc.root_sep = root_sep;
100 id_ = ecs_entity_init(world, &desc);
101 }
102
107 explicit entity(entity_t id)
108 : entity_builder( nullptr, id ) { }
109
110 #ifndef ensure
111
121 template <typename T>
122 T& ensure() const {
123 auto comp_id = _::type<T>::id(world_);
124 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
125 "operation invalid for empty type");
126 return *static_cast<T*>(ecs_ensure_id(world_, id_, comp_id));
127 }
128
138 void* ensure(entity_t comp) const {
139 return ecs_ensure_id(world_, id_, comp);
140 }
141
148 template <typename First, typename Second, typename P = pair<First, Second>,
149 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
150 A& ensure() const {
151 return *static_cast<A*>(ecs_ensure_id(world_, id_, ecs_pair(
152 _::type<First>::id(world_),
153 _::type<Second>::id(world_))));
154 }
155
162 template <typename First>
163 First& ensure(entity_t second) const {
164 auto first = _::type<First>::id(world_);
165 ecs_assert(_::type<First>::size() != 0, ECS_INVALID_PARAMETER,
166 "operation invalid for empty type");
167 return *static_cast<First*>(
168 ecs_ensure_id(world_, id_, ecs_pair(first, second)));
169 }
170
179 void* ensure(entity_t first, entity_t second) const {
180 return ecs_ensure_id(world_, id_, ecs_pair(first, second));
181 }
182
189 template <typename Second>
190 Second& ensure_second(entity_t first) const {
191 auto second = _::type<Second>::id(world_);
192 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second)) != NULL,
193 ECS_INVALID_PARAMETER, "pair is not a component");
194 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second))->component == second,
195 ECS_INVALID_PARAMETER, "type of pair is not Second");
196 ecs_assert(_::type<Second>::size() != 0, ECS_INVALID_PARAMETER,
197 "operation invalid for empty type");
198 return *static_cast<Second*>(
199 ecs_ensure_id(world_, id_, ecs_pair(first, second)));
200 }
201
202 #endif
203
208 template <typename T>
209 void modified() const {
210 auto comp_id = _::type<T>::id(world_);
211 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
212 "operation invalid for empty type");
213 this->modified(comp_id);
214 }
215
221 template <typename First, typename Second, typename A = actual_type_t<flecs::pair<First, Second>>>
222 void modified() const {
223 auto first = _::type<First>::id(world_);
224 auto second = _::type<Second>::id(world_);
225 ecs_assert(_::type<A>::size() != 0, ECS_INVALID_PARAMETER,
226 "operation invalid for empty type");
227 this->modified(first, second);
228 }
229
235 template <typename First>
236 void modified(entity_t second) const {
237 auto first = _::type<First>::id(world_);
238 ecs_assert(_::type<First>::size() != 0, ECS_INVALID_PARAMETER,
239 "operation invalid for empty type");
240 this->modified(first, second);
241 }
242
250 void modified(entity_t first, entity_t second) const {
251 this->modified(ecs_pair(first, second));
252 }
253
258 void modified(entity_t comp) const {
259 ecs_modified_id(world_, id_, comp);
260 }
261
280 template <typename T, if_t< is_actual<T>::value > = 0>
281 ref<T> get_ref_w_id(flecs::id_t component) const {
282 _::type<T>::id(world_); // ensure type is registered
283 return ref<T>(world_, id_, component);
284 }
285
293 template <typename T, if_t< is_actual<T>::value > = 0>
294 ref<T> get_ref() const {
295 return ref<T>(world_, id_, _::type<T>::id(world_));
296 }
297
307 template <typename T, typename A = actual_type_t<T>, if_t< flecs::is_pair<T>::value > = 0>
308 ref<A> get_ref() const {
309 return ref<A>(world_, id_,
310 ecs_pair(_::type<typename T::first>::id(world_),
312 }
313
314
315 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
316 typename A = actual_type_t<P>>
317 ref<A> get_ref() const {
318 return ref<A>(world_, id_,
319 ecs_pair(_::type<First>::id(world_), _::type<Second>::id(world_)));
320 }
321
322 template <typename First>
323 ref<First> get_ref(flecs::entity_t second) const {
324 auto first = _::type<First>::id(world_);
325 return ref<First>(world_, id_, ecs_pair(first, second));
326 }
327
328 template <typename Second>
329 ref<Second> get_ref_second(flecs::entity_t first) const {
330 auto second = _::type<Second>::id(world_);
331 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second)) != NULL,
332 ECS_INVALID_PARAMETER, "pair is not a component");
333 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second))->component == second,
334 ECS_INVALID_PARAMETER, "type of pair is not Second");
335 return ref<Second>(world_, id_, ecs_pair(first, second));
336 }
337
344 void clear() const {
345 ecs_clear(world_, id_);
346 }
347
354 void destruct() const {
355 ecs_delete(world_, id_);
356 }
357
367 return flecs::entity_view(
368 const_cast<flecs::world_t*>(ecs_get_world(world_)), id_);
369 }
370
377 static
378 flecs::entity null(const flecs::world_t *world) {
379 flecs::entity result;
380 result.world_ = const_cast<flecs::world_t*>(world);
381 return result;
382 }
383
384 static
385 flecs::entity null() {
386 return flecs::entity();
387 }
388
389# ifdef FLECS_JSON
390# include "mixins/json/entity.inl"
391# endif
392};
393
394} // namespace flecs
395
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:368
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:1004
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:1016
const char * root_sep
Optional, used for identifiers relative to root.
Definition flecs.h:1020
const char * name
Name of the entity.
Definition flecs.h:1011
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:354
ref< T > get_ref() const
Get reference to component.
Definition entity.hpp:294
void modified(entity_t second) const
Signal that the first part of a pair was modified.
Definition entity.hpp:236
void modified() const
Signal that the first element of a pair was modified.
Definition entity.hpp:222
First & ensure(entity_t second) const
Get mutable pointer for the first element of a pair.
Definition entity.hpp:163
void modified(entity_t comp) const
Signal that component was modified.
Definition entity.hpp:258
void clear() const
Clear an entity.
Definition entity.hpp:344
ref< T > get_ref_w_id(flecs::id_t component) const
Get reference to component specified by id.
Definition entity.hpp:281
void modified(entity_t first, entity_t second) const
Signal that a pair has modified (untyped).
Definition entity.hpp:250
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:179
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:209
T & ensure() const
Get mutable component value.
Definition entity.hpp:122
static flecs::entity null(const flecs::world_t *world)
Entity id 0.
Definition entity.hpp:378
Second & ensure_second(entity_t first) const
Get mutable pointer for the second element of a pair.
Definition entity.hpp:190
void * ensure(entity_t comp) const
Get mutable component value (untyped).
Definition entity.hpp:138
entity(entity_t id)
Conversion from flecs::entity_t to flecs::entity.
Definition entity.hpp:107
entity(world_t *world, const char *name, const char *sep, const char *root_sep)
Create a named entity.
Definition entity.hpp:91
entity(world_t *world)
Create entity.
Definition entity.hpp:37
flecs::entity_view view() const
Return entity as entity_view.
Definition entity.hpp:366
ref< A > get_ref() const
Get reference to component.
Definition entity.hpp:308
A & ensure() const
Get mutable pointer for a pair.
Definition entity.hpp:150
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