Flecs v3.2
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 m_world = world;
41 m_id = ecs_new(world, 0);
42 }
43
49 explicit entity(const flecs::world_t *world, flecs::entity_t id) {
50 m_world = const_cast<flecs::world_t*>(world);
51 m_id = id;
52 }
53
63 explicit entity(world_t *world, const char *name)
65 {
66 m_world = world;
67
68 ecs_entity_desc_t desc = {};
69 desc.name = name;
70 desc.sep = "::";
71 desc.root_sep = "::";
72 m_id = ecs_entity_init(world, &desc);
73 }
74
79 explicit entity(entity_t id)
80 : entity_builder( nullptr, id ) { }
81
82 #ifndef ensure
83
93 template <typename T>
94 T& ensure() const {
95 auto comp_id = _::cpp_type<T>::id(m_world);
96 ecs_assert(_::cpp_type<T>::size() != 0, ECS_INVALID_PARAMETER, NULL);
97 return *static_cast<T*>(ecs_ensure_id(m_world, m_id, comp_id));
98 }
99
109 void* ensure(entity_t comp) const {
110 return ecs_ensure_id(m_world, m_id, comp);
111 }
112
119 template <typename First, typename Second, typename P = pair<First, Second>,
120 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
121 A& ensure() const {
122 return *static_cast<A*>(ecs_ensure_id(m_world, m_id, ecs_pair(
123 _::cpp_type<First>::id(m_world),
124 _::cpp_type<Second>::id(m_world))));
125 }
126
133 template <typename First>
134 First& ensure(entity_t second) const {
135 auto comp_id = _::cpp_type<First>::id(m_world);
136 ecs_assert(_::cpp_type<First>::size() != 0, ECS_INVALID_PARAMETER, NULL);
137 return *static_cast<First*>(
138 ecs_ensure_id(m_world, m_id, ecs_pair(comp_id, second)));
139 }
140
149 void* ensure(entity_t first, entity_t second) const {
150 return ecs_ensure_id(m_world, m_id, ecs_pair(first, second));
151 }
152
153 #endif
154
161 template <typename Second>
162 Second& ensure_second(entity_t first) const {
163 auto second = _::cpp_type<Second>::id(m_world);
164 ecs_assert(_::cpp_type<Second>::size() != 0, ECS_INVALID_PARAMETER, NULL);
165 return *static_cast<Second*>(
166 ecs_ensure_id(m_world, m_id, ecs_pair(first, second)));
167 }
168
173 template <typename T>
174 void modified() const {
175 auto comp_id = _::cpp_type<T>::id(m_world);
176 ecs_assert(_::cpp_type<T>::size() != 0, ECS_INVALID_PARAMETER, NULL);
177 this->modified(comp_id);
178 }
179
185 template <typename First, typename Second>
186 void modified() const {
187 this->modified<First>(_::cpp_type<Second>::id(m_world));
188 }
189
195 template <typename First>
196 void modified(entity_t second) const {
197 auto first = _::cpp_type<First>::id(m_world);
198 ecs_assert(_::cpp_type<First>::size() != 0, ECS_INVALID_PARAMETER, NULL);
199 this->modified(first, second);
200 }
201
209 void modified(entity_t first, entity_t second) const {
210 this->modified(ecs_pair(first, second));
211 }
212
217 void modified(entity_t comp) const {
218 ecs_modified_id(m_world, m_id, comp);
219 }
220
228 template <typename T, if_t< is_actual<T>::value > = 0>
229 ref<T> get_ref() const {
230 return ref<T>(m_world, m_id, _::cpp_type<T>::id(m_world));
231 }
232
242 template <typename T, typename A = actual_type_t<T>, if_t< flecs::is_pair<T>::value > = 0>
243 ref<A> get_ref() const {
244 return ref<A>(m_world, m_id,
245 ecs_pair(_::cpp_type<typename T::first>::id(m_world),
247 }
248
249
250 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
251 typename A = actual_type_t<P>>
252 ref<A> get_ref() const {
253 return ref<A>(m_world, m_id,
254 ecs_pair(_::cpp_type<First>::id(m_world),
255 _::cpp_type<Second>::id(m_world)));
256 }
257
258 template <typename First>
259 ref<First> get_ref(flecs::entity_t second) const {
260 return ref<First>(m_world, m_id,
261 ecs_pair(_::cpp_type<First>::id(m_world), second));
262 }
263
264 template <typename Second>
265 ref<Second> get_ref_second(flecs::entity_t first) const {
266 return ref<Second>(m_world, m_id,
267 ecs_pair(first, _::cpp_type<Second>::id(m_world)));
268 }
269
273 void flatten(flecs::entity_t r, const ecs_flatten_desc_t *desc = nullptr) {
274 ecs_flatten(m_world, ecs_pair(r, m_id), desc);
275 }
276
281 void clear() const {
282 ecs_clear(m_world, m_id);
283 }
284
289 void destruct() const {
290 ecs_delete(m_world, m_id);
291 }
292
302 return flecs::entity_view(
303 const_cast<flecs::world_t*>(ecs_get_world(m_world)), m_id);
304 }
305
312 static
313 flecs::entity null(const flecs::world_t *world) {
314 flecs::entity result;
315 result.m_world = const_cast<flecs::world_t*>(world);
316 return result;
317 }
318
319 static
320 flecs::entity null() {
321 return flecs::entity();
322 }
323
324# ifdef FLECS_JSON
325# include "mixins/json/entity.inl"
326# endif
327};
328
329} // namespace flecs
330
Entity class with only readonly operations.
void ecs_clear(ecs_world_t *world, ecs_entity_t entity)
Clear all components.
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:351
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_flatten(ecs_world_t *world, ecs_id_t pair, const ecs_flatten_desc_t *desc)
Recursively flatten relationship for target entity (experimental).
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.
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:913
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:923
const char * root_sep
Optional, used for identifiers relative to root.
Definition flecs.h:927
const char * name
Name of the entity.
Definition flecs.h:918
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:289
ref< T > get_ref() const
Get reference to component.
Definition entity.hpp:229
void modified(entity_t second) const
Signal that the first part of a pair was modified.
Definition entity.hpp:196
First & ensure(entity_t second) const
Get mutable pointer for the first element of a pair.
Definition entity.hpp:134
void modified(entity_t comp) const
Signal that component was modified.
Definition entity.hpp:217
void clear() const
Clear an entity.
Definition entity.hpp:281
void modified(entity_t first, entity_t second) const
Signal that a pair has modified (untyped).
Definition entity.hpp:209
entity(world_t *world, const char *name)
Create a named entity.
Definition entity.hpp:63
void * ensure(entity_t first, entity_t second) const
Get mutable pointer for a pair (untyped).
Definition entity.hpp:149
void modified() const
Signal that the first element of a pair was modified.
Definition entity.hpp:186
entity(const flecs::world_t *world, flecs::entity_t id)
Wrap an existing entity id.
Definition entity.hpp:49
void modified() const
Signal that component was modified.
Definition entity.hpp:174
T & ensure() const
Get mutable component value.
Definition entity.hpp:94
static flecs::entity null(const flecs::world_t *world)
Entity id 0.
Definition entity.hpp:313
Second & ensure_second(entity_t first) const
Get mutable pointer for the second element of a pair.
Definition entity.hpp:162
void * ensure(entity_t comp) const
Get mutable component value (untyped).
Definition entity.hpp:109
entity(entity_t id)
Conversion from flecs::entity_t to flecs::entity.
Definition entity.hpp:79
void flatten(flecs::entity_t r, const ecs_flatten_desc_t *desc=nullptr)
Recursively flatten relationship.
Definition entity.hpp:273
entity(world_t *world)
Create entity.
Definition entity.hpp:37
flecs::entity_view view() const
Return entity as entity_view.
Definition entity.hpp:301
ref< A > get_ref() const
Get reference to component.
Definition entity.hpp:243
A & ensure() const
Get mutable pointer for a pair.
Definition entity.hpp:121
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:132