Flecs v4.1
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
world.hpp
Go to the documentation of this file.
1
6#pragma once
7
8namespace flecs
9{
10
15 this->component<Poly>();
16 this->component<Parent>();
17
18 /* If meta is not defined and we're using enum reflection, make sure that
19 * primitive types are registered. This ensures we can set components of
20 * underlying_type_t<E> when registering constants. */
21# if !defined(FLECS_META) && !defined(FLECS_CPP_NO_ENUM_REFLECTION)
22 this->component<uint8_t>("flecs::meta::u8");
23 this->component<uint16_t>("flecs::meta::u16");
24 this->component<uint32_t>("flecs::meta::u32");
25 this->component<uint64_t>("flecs::meta::u64");
26 this->component<int8_t>("flecs::meta::i8");
27 this->component<int16_t>("flecs::meta::i16");
28 this->component<int32_t>("flecs::meta::i32");
29 this->component<int64_t>("flecs::meta::i64");
30# endif
31
32# ifdef FLECS_SYSTEM
33 _::system_init(*this);
34# endif
35# ifdef FLECS_TIMER
36 _::timer_init(*this);
37# endif
38# ifdef FLECS_DOC
39 doc::_::init(*this);
40# endif
41# ifdef FLECS_REST
42 rest::_::init(*this);
43# endif
44# ifdef FLECS_META
45 meta::_::init(*this);
46# endif
47# ifdef FLECS_SCRIPT
48 script::_::init(*this);
49# endif
50}
51
53template <typename T>
54inline flecs::entity world::use(const char *alias) const {
56 const char *name = alias;
57 if (!name) {
58 // If no name is defined, use the entity name without the scope.
59 name = ecs_get_name(world_, e);
60 }
61 ecs_set_alias(world_, e, name);
62 return flecs::entity(world_, e);
63}
64
66inline flecs::entity world::use(const char *name, const char *alias) const {
67 entity_t e = ecs_lookup_path_w_sep(world_, 0, name, "::", "::", true);
68 ecs_assert(e != 0, ECS_INVALID_PARAMETER, NULL);
69
70 ecs_set_alias(world_, e, alias);
71 return flecs::entity(world_, e);
72}
73
75inline void world::use(flecs::entity e, const char *alias) const {
76 entity_t eid = e.id();
77 const char *name = alias;
78 if (!name) {
79 // If no name is defined, use the entity name without the scope.
80 name = ecs_get_name(world_, eid);
81 }
82 ecs_set_alias(world_, eid, name);
83}
84
89
94
96template <typename T>
99}
100
102inline entity world::lookup(const char *name, const char *sep, const char *root_sep, bool recursive) const {
103 auto e = ecs_lookup_path_w_sep(world_, 0, name, sep, root_sep, recursive);
104 return flecs::entity(*this, e);
105}
106
107#ifndef ensure
109template <typename T>
110inline T& world::ensure() const {
112 return e.ensure<T>();
113}
114#endif
115
117template <typename T>
118inline void world::modified() const {
120 e.modified<T>();
121}
122
124template <typename First, typename Second>
125inline void world::set(Second second, const First& value) const {
127 e.set<First>(second, value);
128}
129
131template <typename First, typename Second>
132inline void world::set(Second second, First&& value) const {
134 e.set<First>(second, value);
135}
136
138template <typename T>
139inline ref<T> world::get_ref() const {
141 return e.get_ref<T>();
142}
143
145inline const void* world::try_get(flecs::id_t id) const {
146 flecs::entity e(world_, id);
147 return e.try_get(id);
148}
149
151inline const void* world::try_get(flecs::entity_t r, flecs::entity_t t) const {
152 flecs::entity e(world_, r);
153 return e.try_get(r, t);
154}
155
157template <typename T>
158inline const T* world::try_get() const {
160 return e.try_get<T>();
161}
162
164template <typename First, typename Second, typename P, typename A>
165inline const A* world::try_get() const {
167 return e.try_get<First, Second>();
168}
169
171template <typename First, typename Second>
172inline const First* world::try_get(Second second) const {
174 return e.try_get<First>(second);
175}
176
178inline const void* world::get(flecs::id_t id) const {
179 flecs::entity e(world_, id);
180 return e.get(id);
181}
182
184inline const void* world::get(flecs::entity_t r, flecs::entity_t t) const {
185 flecs::entity e(world_, r);
186 return e.get(r, t);
187}
188
190template <typename T>
191inline const T& world::get() const {
193 return e.get<T>();
194}
195
197template <typename First, typename Second, typename P, typename A>
198inline const A& world::get() const {
200 return e.get<First, Second>();
201}
202
204template <typename First, typename Second>
205const First& world::get(Second second) const {
207 return e.get<First>(second);
208}
209
211inline void* world::try_get_mut(flecs::id_t id) const {
212 flecs::entity e(world_, id);
213 return e.try_get_mut(id);
214}
215
218 flecs::entity e(world_, r);
219 return e.try_get_mut(r, t);
220}
221
223template <typename T>
224inline T* world::try_get_mut() const {
226 return e.try_get_mut<T>();
227}
228
230template <typename First, typename Second, typename P, typename A>
231inline A* world::try_get_mut() const {
233 return e.try_get_mut<First, Second>();
234}
235
237template <typename First, typename Second>
238inline First* world::try_get_mut(Second second) const {
240 return e.try_get_mut<First>(second);
241}
242
244inline void* world::get_mut(flecs::id_t id) const {
245 flecs::entity e(world_, id);
246 return e.get_mut(id);
247}
248
251 flecs::entity e(world_, r);
252 return e.get_mut(r, t);
253}
254
256template <typename T>
257inline T& world::get_mut() const {
259 return e.get_mut<T>();
260}
261
263template <typename First, typename Second, typename P, typename A>
264inline A& world::get_mut() const {
266 return e.get_mut<First, Second>();
267}
268
270template <typename First, typename Second>
271inline First& world::get_mut(Second second) const {
273 return e.get_mut<First>(second);
274}
275
277template <typename T>
278inline bool world::has() const {
280 return e.has<T>();
281}
282
284template <typename First, typename Second>
285inline bool world::has() const {
287 return e.has<First, Second>();
288}
289
291template <typename First>
292inline bool world::has(flecs::id_t second) const {
294 return e.has<First>(second);
295}
296
298inline bool world::has(flecs::id_t first, flecs::id_t second) const {
299 flecs::entity e(world_, first);
300 return e.has(first, second);
301}
302
304template <typename E, if_t< is_enum<E>::value > >
305inline bool world::has(E value) const {
307 return e.has(value);
308}
309
311template <typename T>
312inline void world::add() const {
314 e.add<T>();
315}
316
318template <typename First, typename Second>
319inline void world::add() const {
321 e.add<First, Second>();
322}
323
325template <typename First>
326inline void world::add(flecs::entity_t second) const {
328 e.add<First>(second);
329}
330
332inline void world::add(flecs::entity_t first, flecs::entity_t second) const {
333 flecs::entity e(world_, first);
334 e.add(first, second);
335}
336
338template <typename E, if_t< is_enum<E>::value > >
339inline void world::add(E value) const {
341 e.add(value);
342}
343
345template <typename T>
346inline void world::remove() const {
348 e.remove<T>();
349}
350
352template <typename First, typename Second>
353inline void world::remove() const {
355 e.remove<First, Second>();
356}
357
359template <typename First>
360inline void world::remove(flecs::entity_t second) const {
362 e.remove<First>(second);
363}
364
366inline void world::remove(flecs::entity_t first, flecs::entity_t second) const {
367 flecs::entity e(world_, first);
368 e.remove(first, second);
369}
370
372template <typename Func>
373inline void world::children(Func&& f) const {
374 this->entity(0).children(FLECS_FWD(f));
375}
376
378template <typename T>
382
384template <typename First>
385inline flecs::entity world::target(int32_t index) const
386{
387 return flecs::entity(world_,
389}
390
392template <typename T>
394 flecs::entity_t relationship,
395 int32_t index) const
396{
397 return flecs::entity(world_,
398 ecs_get_target(world_, _::type<T>::id(world_), relationship, index));
399}
400
403 flecs::entity_t relationship,
404 int32_t index) const
405{
406 return flecs::entity(world_,
407 ecs_get_target(world_, relationship, relationship, index));
408}
409
411template <typename Func, if_t< is_callable<Func>::value > >
412inline void world::get(const Func& func) const {
413 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
415 this->world_, this->singleton<first_arg_t<Func>>(), func);
416}
417
419template <typename Func, if_t< is_callable<Func>::value > >
420inline void world::set(const Func& func) const {
421 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
423 this->world_, this->singleton<first_arg_t<Func>>(), func);
424}
425
428 e = ecs_get_alive(world_, e);
429 return flecs::entity(world_, e);
430}
431
437
439template <typename E>
441 return flecs::entity(world_, _::type<E>::id(world_));
442}
443
445template <typename E>
446inline flecs::entity enum_data<E>::entity(underlying_type_t<E> value) const {
447 int index = index_by_value(value);
448 if (index >= 0) {
449 int32_t constant_i = impl_.constants[index].index;
450 flecs::entity_t entity = flecs_component_ids_get(world_, constant_i);
451 return flecs::entity(world_, entity);
452 }
453#ifdef FLECS_META
454 // Reflection data lookup failed. Try a value lookup among flecs::Constant relationships.
456 return world.query_builder()
457 .with(flecs::ChildOf, world.id<E>())
458 .with(flecs::Constant, world.id<int32_t>())
459 .build()
460 .find([value](flecs::entity constant) {
461 const int32_t& constant_value = constant.get_second<int32_t>(flecs::Constant);
462 return value == static_cast<underlying_type_t<E>>(constant_value);
463 });
464#else
465 return flecs::entity::null(world_);
466#endif
467}
468
470template <typename E>
471inline flecs::entity enum_data<E>::entity(E value) const {
472 return entity(static_cast<underlying_type_t<E>>(value));
473}
474
479 return scoped_world(world_, parent);
480}
481
483template <typename T>
486 return scoped_world(world_, parent);
487}
488
490inline flecs::scoped_world world::scope(const char* name) const {
491 return scope(entity(name));
492}
493
494} // namespace flecs
component< T > & constant(const char *name, T value)
Add a constant.
Definition component.inl:38
typename first_arg< Func >::type first_arg_t
Convenience alias for the first argument type of a callable.
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:661
flecs::query_builder< Comps... > query_builder(Args &&... args) const
Create a query builder.
flecs::entity entity(Args &&... args) const
Create an entity.
flecs::id id(E value) const
Convert an enum constant to an entity.
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
ecs_entity_t ecs_get_target(const ecs_world_t *world, ecs_entity_t entity, ecs_entity_t rel, int32_t index)
Get the target of a relationship.
void ecs_make_alive(ecs_world_t *world, ecs_entity_t entity)
Ensure an ID is alive.
ecs_entity_t ecs_get_alive(const ecs_world_t *world, ecs_entity_t e)
Get an alive identifier.
void ecs_set_alias(ecs_world_t *world, ecs_entity_t entity, const char *alias)
Set an alias for an entity.
ecs_entity_t ecs_get_scope(const ecs_world_t *world)
Get the current scope.
ecs_entity_t ecs_lookup_path_w_sep(const ecs_world_t *world, ecs_entity_t parent, const char *path, const char *sep, const char *prefix, bool recursive)
Look up an entity from a path.
const char * ecs_get_name(const ecs_world_t *world, ecs_entity_t entity)
Get the name of an entity.
ecs_entity_t ecs_set_scope(ecs_world_t *world, ecs_entity_t scope)
Set the current scope.
Trait to get the number of arguments of a callable.
Component class.
const Self & remove() const
Remove a component from an entity.
Definition builder.hpp:303
const Self & set(T &&value) const
Set a component for an entity.
Definition builder.hpp:705
const Self & add() const
Add a component to an entity.
Definition builder.hpp:25
T & get_mut() const
Get mutable component value.
const T & get() const
Get component value.
bool has(flecs::id_t e) const
Check if entity has the provided entity.
T * try_get_mut() const
Get mutable component value.
entity_t id() const
Get entity ID.
const T * try_get() const
Get component value.
void children(flecs::entity_t rel, Func &&func) const
Iterate children for an entity.
Entity.
Definition entity.hpp:30
static flecs::entity null()
Entity ID 0 without a world.
Definition entity.hpp:491
ref< T > get_ref() const
Get reference to component.
Definition entity.hpp:319
void modified() const
Signal that component was modified.
Definition entity.hpp:233
T & ensure() const
Get mutable component value.
Definition entity.hpp:139
flecs::entity entity() const
Get entity for the enum type.
Definition world.hpp:440
Component reference.
Definition ref.hpp:108
Scoped world.
Definition world.hpp:1551
The world.
Definition world.hpp:246
const T & get() const
Get a singleton component.
Definition world.hpp:191
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:1545
flecs::entity get_scope() const
Get current scope.
Definition world.hpp:91
T & get_mut() const
Get a mutable singleton component.
Definition world.hpp:257
void remove() const
Remove singleton component.
Definition world.hpp:346
flecs::entity lookup(const char *name, const char *sep="::", const char *root_sep="::", bool recursive=true) const
Lookup entity by name.
Definition world.hpp:102
flecs::entity get_alive(flecs::entity_t e) const
Get alive entity for ID.
Definition world.hpp:427
flecs::entity make_alive(flecs::entity_t e) const
Ensure an entity ID is alive.
Definition world.hpp:433
flecs::entity target(int32_t index=0) const
Get target for a given pair from a singleton entity.
Definition world.hpp:385
const T * try_get() const
Get singleton component.
Definition world.hpp:158
void init_builtin_components()
Initialize built-in components.
Definition world.hpp:12
flecs::entity set_scope() const
Same as set_scope(), but with type.
Definition world.hpp:97
flecs::scoped_world scope() const
Use the provided scope (by type) for operations run on the returned world.
Definition world.hpp:484
void children(Func &&f) const
Iterate entities in root of world.
Definition world.hpp:373
T * try_get_mut() const
Try to get a mutable singleton component (returns nullptr if not found).
Definition world.hpp:224
void modified() const
Mark singleton component as modified.
Definition world.hpp:118
flecs::entity set_scope(const flecs::entity_t scope) const
Set current scope.
Definition world.hpp:86
flecs::entity use(const char *alias=nullptr) const
Create an alias for a component.
Definition world.hpp:54
void add() const
Add singleton component.
Definition world.hpp:312
T & ensure() const
Ensure singleton component.
Definition world.hpp:110
void set(const T &value) const
Set singleton component.
Definition world.hpp:784
bool has() const
Test if world has singleton component.
Definition world.hpp:278
flecs::entity singleton() const
Get singleton entity for type.
Definition world.hpp:379
ref< T > get_ref() const
Get ref singleton component.
Definition world.hpp:139