Flecs v3.1
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 // Ensure component is registered
227 _::cpp_type<T>::id(m_world);
228 return ref<T>(m_world, m_id);
229 }
230
231 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
232 typename A = actual_type_t<P>>
233 ref<A> get_ref() const {
234 // Ensure component is registered
235 _::cpp_type<A>::id(m_world);
236 return ref<A>(m_world, m_id,
237 ecs_pair(_::cpp_type<First>::id(m_world),
238 _::cpp_type<Second>::id(m_world)));
239 }
240
241 template <typename First>
242 ref<First> get_ref(flecs::entity_t second) const {
243 // Ensure component is registered
244 _::cpp_type<First>::id(m_world);
245 return ref<First>(m_world, m_id,
246 ecs_pair(_::cpp_type<First>::id(m_world), second));
247 }
248
249 template <typename Second>
250 ref<Second> get_ref_second(flecs::entity_t first) const {
251 // Ensure component is registered
252 _::cpp_type<Second>::id(m_world);
253 return ref<Second>(m_world, m_id,
254 ecs_pair(first, _::cpp_type<Second>::id(m_world)));
255 }
256
261 void clear() const {
262 ecs_clear(m_world, m_id);
263 }
264
269 void destruct() const {
270 ecs_delete(m_world, m_id);
271 }
272
279 static
280 flecs::entity null(const flecs::world_t *world) {
281 flecs::entity result;
282 result.m_world = const_cast<flecs::world_t*>(world);
283 return result;
284 }
285
286 static
287 flecs::entity null() {
288 return flecs::entity();
289 }
290
291# ifdef FLECS_JSON
292# include "mixins/json/entity.inl"
293# endif
294};
295
296} // namespace flecs
297
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:352
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_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.
Entity builder.
Used with ecs_entity_init.
Definition: flecs.h:685
const char * sep
Optional custom separator for hierarchical names.
Definition: flecs.h:695
const char * root_sep
Optional, used for identifiers relative to root.
Definition: flecs.h:699
const char * name
Name of the entity.
Definition: flecs.h:690
Entity builder.
Definition: builder.hpp:15
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:269
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:261
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 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:280
entity(entity_t id)
Conversion from flecs::entity_t to flecs::entity.
Definition: entity.hpp:79
entity(world_t *world)
Create entity.
Definition: entity.hpp:37
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:113