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::id_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
91 template <typename T>
92 T* get_mut() const {
93 auto comp_id = _::cpp_type<T>::id(m_world);
94 ecs_assert(_::cpp_type<T>::size() != 0, ECS_INVALID_PARAMETER, NULL);
95 return static_cast<T*>(ecs_get_mut_id(m_world, m_id, comp_id));
96 }
97
107 void* get_mut(entity_t comp) const {
108 return ecs_get_mut_id(m_world, m_id, comp);
109 }
110
117 template <typename First, typename Second, typename P = pair<First, Second>,
118 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
119 A* get_mut() const {
120 return static_cast<A*>(ecs_get_mut_id(m_world, m_id, ecs_pair(
121 _::cpp_type<First>::id(m_world),
122 _::cpp_type<Second>::id(m_world))));
123 }
124
131 template <typename First>
132 First* get_mut(entity_t second) const {
133 auto comp_id = _::cpp_type<First>::id(m_world);
134 ecs_assert(_::cpp_type<First>::size() != 0, ECS_INVALID_PARAMETER, NULL);
135 return static_cast<First*>(
136 ecs_get_mut_id(m_world, m_id, ecs_pair(comp_id, second)));
137 }
138
147 void* get_mut(entity_t first, entity_t second) const {
148 return ecs_get_mut_id(m_world, m_id, ecs_pair(first, second));
149 }
150
157 template <typename Second>
158 Second* get_mut_second(entity_t first) const {
159 auto second = _::cpp_type<Second>::id(m_world);
160 ecs_assert(_::cpp_type<Second>::size() != 0, ECS_INVALID_PARAMETER, NULL);
161 return static_cast<Second*>(
162 ecs_get_mut_id(m_world, m_id, ecs_pair(first, second)));
163 }
164
169 template <typename T>
170 void modified() const {
171 auto comp_id = _::cpp_type<T>::id(m_world);
172 ecs_assert(_::cpp_type<T>::size() != 0, ECS_INVALID_PARAMETER, NULL);
173 this->modified(comp_id);
174 }
175
181 template <typename First, typename Second>
182 void modified() const {
183 this->modified<First>(_::cpp_type<Second>::id(m_world));
184 }
185
191 template <typename First>
192 void modified(entity_t second) const {
193 auto first = _::cpp_type<First>::id(m_world);
194 ecs_assert(_::cpp_type<First>::size() != 0, ECS_INVALID_PARAMETER, NULL);
195 this->modified(first, second);
196 }
197
205 void modified(entity_t first, entity_t second) const {
206 this->modified(ecs_pair(first, second));
207 }
208
213 void modified(entity_t comp) const {
214 ecs_modified_id(m_world, m_id, comp);
215 }
216
224 template <typename T>
225 ref<T> get_ref() const {
226 return ref<T>(m_world, m_id, _::cpp_type<T>::id(m_world));
227 }
228
229 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
230 typename A = actual_type_t<P>>
231 ref<A> get_ref() const {
232 return ref<A>(m_world, m_id,
233 ecs_pair(_::cpp_type<First>::id(m_world),
234 _::cpp_type<Second>::id(m_world)));
235 }
236
237 template <typename First>
238 ref<First> get_ref(flecs::entity_t second) const {
239 return ref<First>(m_world, m_id,
240 ecs_pair(_::cpp_type<First>::id(m_world), second));
241 }
242
243 template <typename Second>
244 ref<Second> get_ref_second(flecs::entity_t first) const {
245 return ref<Second>(m_world, m_id,
246 ecs_pair(first, _::cpp_type<Second>::id(m_world)));
247 }
248
252 void flatten(flecs::entity_t r, const ecs_flatten_desc_t *desc = nullptr) {
253 ecs_flatten(m_world, ecs_pair(r, m_id), desc);
254 }
255
260 void clear() const {
261 ecs_clear(m_world, m_id);
262 }
263
268 void destruct() const {
269 ecs_delete(m_world, m_id);
270 }
271
281 return flecs::entity_view(
282 const_cast<flecs::world_t*>(ecs_get_world(m_world)), m_id);
283 }
284
291 static
292 flecs::entity null(const flecs::world_t *world) {
293 flecs::entity result;
294 result.m_world = const_cast<flecs::world_t*>(world);
295 return result;
296 }
297
298 static
299 flecs::entity null() {
300 return flecs::entity();
301 }
302
303# ifdef FLECS_JSON
304# include "mixins/json/entity.inl"
305# endif
306};
307
308} // namespace flecs
309
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_modified_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Signal that a component has been modified.
void * ecs_get_mut_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Get a mutable pointer to a component.
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:879
const char * sep
Optional custom separator for hierarchical names.
Definition: flecs.h:889
const char * root_sep
Optional, used for identifiers relative to root.
Definition: flecs.h:893
const char * name
Name of the entity.
Definition: flecs.h:884
Entity builder.
Definition: builder.hpp:15
Entity view.
Definition: entity_view.hpp:28
flecs::string_view name() const
Return the entity name.
Definition: entity_view.hpp:78
Entity.
Definition: entity.hpp:30
A * get_mut() const
Get mutable pointer for a pair.
Definition: entity.hpp:119
void destruct() const
Delete an entity.
Definition: entity.hpp:268
void modified(entity_t second) const
Signal that the first part of a pair was modified.
Definition: entity.hpp:192
void * get_mut(entity_t first, entity_t second) const
Get mutable pointer for a pair (untyped).
Definition: entity.hpp:147
void modified(entity_t comp) const
Signal that component was modified.
Definition: entity.hpp:213
void clear() const
Clear an entity.
Definition: entity.hpp:260
void modified(entity_t first, entity_t second) const
Signal that a pair has modified (untyped).
Definition: entity.hpp:205
Second * get_mut_second(entity_t first) const
Get mutable pointer for the second element of a pair.
Definition: entity.hpp:158
entity(world_t *world, const char *name)
Create a named entity.
Definition: entity.hpp:63
T * get_mut() const
Get mutable component value.
Definition: entity.hpp:92
ref< T > get_ref() const
Get reference to component.
Definition: entity.hpp:225
First * get_mut(entity_t second) const
Get mutable pointer for the first element of a pair.
Definition: entity.hpp:132
void modified() const
Signal that the first element of a pair was modified.
Definition: entity.hpp:182
void modified() const
Signal that component was modified.
Definition: entity.hpp:170
entity(const flecs::world_t *world, flecs::id_t id)
Wrap an existing entity id.
Definition: entity.hpp:49
static flecs::entity null(const flecs::world_t *world)
Entity id 0.
Definition: entity.hpp:292
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:252
entity(world_t *world)
Create entity.
Definition: entity.hpp:37
flecs::entity_view view() const
Return entity as entity_view.
Definition: entity.hpp:280
void * get_mut(entity_t comp) const
Get mutable component value (untyped).
Definition: entity.hpp:107
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