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 comp_id = _::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(comp_id, 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(_::type<Second>::size() != 0, ECS_INVALID_PARAMETER,
170 "operation invalid for empty type");
171 return *static_cast<Second*>(
172 ecs_ensure_id(world_, id_, ecs_pair(first, second)));
173 }
174
175 #endif
176
181 template <typename T>
182 void modified() const {
183 auto comp_id = _::type<T>::id(world_);
184 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
185 "operation invalid for empty type");
186 this->modified(comp_id);
187 }
188
194 template <typename First, typename Second>
195 void modified() const {
196 this->modified<First>(_::type<Second>::id(world_));
197 }
198
204 template <typename First>
205 void modified(entity_t second) const {
206 auto first = _::type<First>::id(world_);
207 ecs_assert(_::type<First>::size() != 0, ECS_INVALID_PARAMETER,
208 "operation invalid for empty type");
209 this->modified(first, second);
210 }
211
219 void modified(entity_t first, entity_t second) const {
220 this->modified(ecs_pair(first, second));
221 }
222
227 void modified(entity_t comp) const {
228 ecs_modified_id(world_, id_, comp);
229 }
230
238 template <typename T, if_t< is_actual<T>::value > = 0>
239 ref<T> get_ref() const {
240 return ref<T>(world_, id_, _::type<T>::id(world_));
241 }
242
252 template <typename T, typename A = actual_type_t<T>, if_t< flecs::is_pair<T>::value > = 0>
253 ref<A> get_ref() const {
254 return ref<A>(world_, id_,
255 ecs_pair(_::type<typename T::first>::id(world_),
257 }
258
259
260 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
261 typename A = actual_type_t<P>>
262 ref<A> get_ref() const {
263 return ref<A>(world_, id_,
264 ecs_pair(_::type<First>::id(world_),
265 _::type<Second>::id(world_)));
266 }
267
268 template <typename First>
269 ref<First> get_ref(flecs::entity_t second) const {
270 return ref<First>(world_, id_,
271 ecs_pair(_::type<First>::id(world_), second));
272 }
273
274 template <typename Second>
275 ref<Second> get_ref_second(flecs::entity_t first) const {
276 return ref<Second>(world_, id_,
277 ecs_pair(first, _::type<Second>::id(world_)));
278 }
279
286 void clear() const {
287 ecs_clear(world_, id_);
288 }
289
296 void destruct() const {
297 ecs_delete(world_, id_);
298 }
299
309 return flecs::entity_view(
310 const_cast<flecs::world_t*>(ecs_get_world(world_)), id_);
311 }
312
319 static
320 flecs::entity null(const flecs::world_t *world) {
321 flecs::entity result;
322 result.world_ = const_cast<flecs::world_t*>(world);
323 return result;
324 }
325
326 static
327 flecs::entity null() {
328 return flecs::entity();
329 }
330
331# ifdef FLECS_JSON
332# include "mixins/json/entity.inl"
333# endif
334};
335
336} // namespace flecs
337
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
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:901
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:913
const char * root_sep
Optional, used for identifiers relative to root.
Definition flecs.h:917
const char * name
Name of the entity.
Definition flecs.h:908
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:296
ref< T > get_ref() const
Get reference to component.
Definition entity.hpp:239
void modified(entity_t second) const
Signal that the first part of a pair was modified.
Definition entity.hpp:205
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:227
void clear() const
Clear an entity.
Definition entity.hpp:286
void modified(entity_t first, entity_t second) const
Signal that a pair has modified (untyped).
Definition entity.hpp:219
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
void modified() const
Signal that the first element of a pair was modified.
Definition entity.hpp:195
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:182
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:320
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:308
ref< A > get_ref() const
Get reference to component.
Definition entity.hpp:253
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