Flecs v4.0
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
11inline void world::init_builtin_components() {
12 this->component<Component>();
13 this->component<Identifier>();
14 this->component<Poly>();
15
16 /* If meta is not defined and we're using enum reflection, make sure that
17 * primitive types are registered. This makes sure we can set components of
18 * underlying_type_t<E> when registering constants. */
19# if !defined(FLECS_META) && !defined(FLECS_CPP_NO_ENUM_REFLECTION)
20 this->component<uint8_t>("flecs::meta::u8");
21 this->component<uint16_t>("flecs::meta::u16");
22 this->component<uint32_t>("flecs::meta::u32");
23 this->component<uint64_t>("flecs::meta::u64");
24 this->component<int8_t>("flecs::meta::i8");
25 this->component<int16_t>("flecs::meta::i16");
26 this->component<int32_t>("flecs::meta::i32");
27 this->component<int64_t>("flecs::meta::i64");
28# endif
29
30# ifdef FLECS_SYSTEM
31 _::system_init(*this);
32# endif
33# ifdef FLECS_TIMER
34 _::timer_init(*this);
35# endif
36# ifdef FLECS_DOC
37 doc::_::init(*this);
38# endif
39# ifdef FLECS_REST
40 rest::_::init(*this);
41# endif
42# ifdef FLECS_META
43 meta::_::init(*this);
44# endif
45# ifdef FLECS_SCRIPT
46 script::_::init(*this);
47# endif
48}
49
50template <typename T>
51inline flecs::entity world::use(const char *alias) const {
52 entity_t e = _::type<T>::id(world_);
53 const char *name = alias;
54 if (!name) {
55 // If no name is defined, use the entity name without the scope
56 name = ecs_get_name(world_, e);
57 }
58 ecs_set_alias(world_, e, name);
59 return flecs::entity(world_, e);
60}
61
62inline flecs::entity world::use(const char *name, const char *alias) const {
63 entity_t e = ecs_lookup_path_w_sep(world_, 0, name, "::", "::", true);
64 ecs_assert(e != 0, ECS_INVALID_PARAMETER, NULL);
65
66 ecs_set_alias(world_, e, alias);
67 return flecs::entity(world_, e);
68}
69
70inline void world::use(flecs::entity e, const char *alias) const {
71 entity_t eid = e.id();
72 const char *name = alias;
73 if (!name) {
74 // If no name is defined, use the entity name without the scope
75 name = ecs_get_name(world_, eid);
76 }
77 ecs_set_alias(world_, eid, name);
78}
79
80inline flecs::entity world::set_scope(const flecs::entity_t s) const {
81 return flecs::entity(ecs_set_scope(world_, s));
82}
83
85 return flecs::entity(world_, ecs_get_scope(world_));
86}
87
88template <typename T>
90 return set_scope( _::type<T>::id(world_) );
91}
92
93inline entity world::lookup(const char *name, const char *sep, const char *root_sep, bool recursive) const {
94 auto e = ecs_lookup_path_w_sep(world_, 0, name, sep, root_sep, recursive);
95 return flecs::entity(*this, e);
96}
97
98#ifndef ensure
99template <typename T>
100inline T& world::ensure() const {
101 flecs::entity e(world_, _::type<T>::id(world_));
102 return e.ensure<T>();
103}
104#endif
105
106template <typename T>
107inline void world::modified() const {
108 flecs::entity e(world_, _::type<T>::id(world_));
109 e.modified<T>();
110}
111
112template <typename First, typename Second>
113inline void world::set(Second second, const First& value) const {
114 flecs::entity e(world_, _::type<First>::id(world_));
115 e.set<First>(second, value);
116}
117
118template <typename First, typename Second>
119inline void world::set(Second second, First&& value) const {
120 flecs::entity e(world_, _::type<First>::id(world_));
121 e.set<First>(second, value);
122}
123
124template <typename T>
125inline ref<T> world::get_ref() const {
126 flecs::entity e(world_, _::type<T>::id(world_));
127 return e.get_ref<T>();
128}
129
130inline const void* world::try_get(flecs::id_t id) const {
131 flecs::entity e(world_, id);
132 return e.try_get(id);
133}
134
135inline const void* world::try_get(flecs::entity_t r, flecs::entity_t t) const {
136 flecs::entity e(world_, r);
137 return e.try_get(r, t);
138}
139
140template <typename T>
141inline const T* world::try_get() const {
142 flecs::entity e(world_, _::type<T>::id(world_));
143 return e.try_get<T>();
144}
145
146template <typename First, typename Second, typename P, typename A>
147inline const A* world::try_get() const {
148 flecs::entity e(world_, _::type<First>::id(world_));
149 return e.try_get<First, Second>();
150}
151
152template <typename First, typename Second>
153inline const First* world::try_get(Second second) const {
154 flecs::entity e(world_, _::type<First>::id(world_));
155 return e.try_get<First>(second);
156}
157
158inline const void* world::get(flecs::id_t id) const {
159 flecs::entity e(world_, id);
160 return e.get(id);
161}
162
163inline const void* world::get(flecs::entity_t r, flecs::entity_t t) const {
164 flecs::entity e(world_, r);
165 return e.get(r, t);
166}
167
168template <typename T>
169inline const T& world::get() const {
170 flecs::entity e(world_, _::type<T>::id(world_));
171 return e.get<T>();
172}
173
174template <typename First, typename Second, typename P, typename A>
175inline const A& world::get() const {
176 flecs::entity e(world_, _::type<First>::id(world_));
177 return e.get<First, Second>();
178}
179
180template <typename First, typename Second>
181const First& world::get(Second second) const {
182 flecs::entity e(world_, _::type<First>::id(world_));
183 return e.get<First>(second);
184}
185
186inline void* world::try_get_mut(flecs::id_t id) const {
187 flecs::entity e(world_, id);
188 return e.try_get_mut(id);
189}
190
191inline void* world::try_get_mut(flecs::entity_t r, flecs::entity_t t) const {
192 flecs::entity e(world_, r);
193 return e.try_get_mut(r, t);
194}
195
196template <typename T>
197inline T* world::try_get_mut() const {
198 flecs::entity e(world_, _::type<T>::id(world_));
199 return e.try_get_mut<T>();
200}
201
202template <typename First, typename Second, typename P, typename A>
203inline A* world::try_get_mut() const {
204 flecs::entity e(world_, _::type<First>::id(world_));
205 return e.try_get_mut<First, Second>();
206}
207
208template <typename First, typename Second>
209inline First* world::try_get_mut(Second second) const {
210 flecs::entity e(world_, _::type<First>::id(world_));
211 return e.try_get_mut<First>(second);
212}
213
214inline void* world::get_mut(flecs::id_t id) const {
215 flecs::entity e(world_, id);
216 return e.get_mut(id);
217}
218
219inline void* world::get_mut(flecs::entity_t r, flecs::entity_t t) const {
220 flecs::entity e(world_, r);
221 return e.get_mut(r, t);
222}
223
224template <typename T>
225inline T& world::get_mut() const {
226 flecs::entity e(world_, _::type<T>::id(world_));
227 return e.get_mut<T>();
228}
229
230template <typename First, typename Second, typename P, typename A>
231inline A& world::get_mut() const {
232 flecs::entity e(world_, _::type<First>::id(world_));
233 return e.get_mut<First, Second>();
234}
235
236template <typename First, typename Second>
237inline First& world::get_mut(Second second) const {
238 flecs::entity e(world_, _::type<First>::id(world_));
239 return e.get_mut<First>(second);
240}
241
242template <typename T>
243inline bool world::has() const {
244 flecs::entity e(world_, _::type<T>::id(world_));
245 return e.has<T>();
246}
247
248template <typename First, typename Second>
249inline bool world::has() const {
250 flecs::entity e(world_, _::type<First>::id(world_));
251 return e.has<First, Second>();
252}
253
254template <typename First>
255inline bool world::has(flecs::id_t second) const {
256 flecs::entity e(world_, _::type<First>::id(world_));
257 return e.has<First>(second);
258}
259
260inline bool world::has(flecs::id_t first, flecs::id_t second) const {
261 flecs::entity e(world_, first);
262 return e.has(first, second);
263}
264
265template <typename T>
266inline void world::add() const {
267 flecs::entity e(world_, _::type<T>::id(world_));
268 e.add<T>();
269}
270
271template <typename First, typename Second>
272inline void world::add() const {
273 flecs::entity e(world_, _::type<First>::id(world_));
274 e.add<First, Second>();
275}
276
277template <typename First>
278inline void world::add(flecs::entity_t second) const {
279 flecs::entity e(world_, _::type<First>::id(world_));
280 e.add<First>(second);
281}
282
283inline void world::add(flecs::entity_t first, flecs::entity_t second) const {
284 flecs::entity e(world_, first);
285 e.add(first, second);
286}
287
288template <typename T>
289inline void world::remove() const {
290 flecs::entity e(world_, _::type<T>::id(world_));
291 e.remove<T>();
292}
293
294template <typename First, typename Second>
295inline void world::remove() const {
296 flecs::entity e(world_, _::type<First>::id(world_));
297 e.remove<First, Second>();
298}
299
300template <typename First>
301inline void world::remove(flecs::entity_t second) const {
302 flecs::entity e(world_, _::type<First>::id(world_));
303 e.remove<First>(second);
304}
305
306inline void world::remove(flecs::entity_t first, flecs::entity_t second) const {
307 flecs::entity e(world_, first);
308 e.remove(first, second);
309}
310
311template <typename Func>
312inline void world::children(Func&& f) const {
313 this->entity(0).children(FLECS_FWD(f));
314}
315
316template <typename T>
318 return flecs::entity(world_, _::type<T>::id(world_));
319}
320
321template <typename First>
322inline flecs::entity world::target(int32_t index) const
323{
324 return flecs::entity(world_,
325 ecs_get_target(world_, _::type<First>::id(world_), _::type<First>::id(world_), index));
326}
327
328template <typename T>
330 flecs::entity_t relationship,
331 int32_t index) const
332{
333 return flecs::entity(world_,
334 ecs_get_target(world_, _::type<T>::id(world_), relationship, index));
335}
336
338 flecs::entity_t relationship,
339 int32_t index) const
340{
341 return flecs::entity(world_,
342 ecs_get_target(world_, relationship, relationship, index));
343}
344
345template <typename Func, if_t< is_callable<Func>::value > >
346inline void world::get(const Func& func) const {
347 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
349 this->world_, this->singleton<first_arg_t<Func>>(), func);
350}
351
352template <typename Func, if_t< is_callable<Func>::value > >
353inline void world::set(const Func& func) const {
354 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
356 this->world_, this->singleton<first_arg_t<Func>>(), func);
357}
358
359inline flecs::entity world::get_alive(flecs::entity_t e) const {
360 e = ecs_get_alive(world_, e);
361 return flecs::entity(world_, e);
362}
363
364inline flecs::entity world::make_alive(flecs::entity_t e) const {
365 ecs_make_alive(world_, e);
366 return flecs::entity(world_, e);
367}
368
369template <typename E>
370inline flecs::entity enum_data<E>::entity() const {
371 return flecs::entity(world_, _::type<E>::id(world_));
372}
373
374template <typename E>
375inline flecs::entity enum_data<E>::entity(underlying_type_t<E> value) const {
376 int index = index_by_value(value);
377 if (index >= 0) {
378 int32_t constant_i = impl_.constants[index].index;
379 flecs::entity_t entity = flecs_component_ids_get(world_, constant_i);
380 return flecs::entity(world_, entity);
381 }
382#ifdef FLECS_META
383 // Reflection data lookup failed. Try value lookup amongst flecs::Constant relationships
384 flecs::world world = flecs::world(world_);
385 return world.query_builder()
386 .with(flecs::ChildOf, world.id<E>())
387 .with(flecs::Constant, world.id<int32_t>())
388 .build()
389 .find([value](flecs::entity constant) {
390 const int32_t& constant_value = constant.get_second<int32_t>(flecs::Constant);
391 return value == static_cast<underlying_type_t<E>>(constant_value);
392 });
393#else
394 return flecs::entity::null(world_);
395#endif
396}
397
398template <typename E>
399inline flecs::entity enum_data<E>::entity(E value) const {
400 return entity(static_cast<underlying_type_t<E>>(value));
401}
402
406inline flecs::scoped_world world::scope(id_t parent) const {
407 return scoped_world(world_, parent);
408}
409
410template <typename T>
411inline flecs::scoped_world world::scope() const {
412 flecs::id_t parent = _::type<T>::id(world_);
413 return scoped_world(world_, parent);
414}
415
416inline flecs::scoped_world world::scope(const char* name) const {
417 return scope(entity(name));
418}
419
420} // namespace flecs
component< T > & constant(const char *name, T value)
Add constant.
Definition component.inl:31
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:368
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 enum constant to entity.
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 id is alive.
ecs_entity_t ecs_get_alive(const ecs_world_t *world, ecs_entity_t e)
Get alive identifier.
void ecs_set_alias(ecs_world_t *world, ecs_entity_t entity, const char *alias)
Set alias for 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)
Lookup 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.
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:694
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 entity.
Entity.
Definition entity.hpp:30
ref< T > get_ref() const
Get reference to component.
Definition entity.hpp:297
void modified() const
Signal that component was modified.
Definition entity.hpp:212
T & ensure() const
Get mutable component value.
Definition entity.hpp:122
static flecs::entity null(const flecs::world_t *world)
Entity id 0.
Definition entity.hpp:412
Convenience type with enum reflection data.
Definition enum.hpp:450
Component reference.
Definition ref.hpp:85
Scoped world.
Definition world.hpp:1388
The world.
Definition world.hpp:150
flecs::entity get_scope() const
Get current scope.
Definition world.hpp:84
void remove() const
Remove singleton component.
Definition world.hpp:289
flecs::entity lookup(const char *name, const char *sep="::", const char *root_sep="::", bool recursive=true) const
Lookup entity by name.
Definition world.hpp:93
flecs::entity get_alive(flecs::entity_t e) const
Get alive entity for id.
Definition world.hpp:359
flecs::entity make_alive(flecs::entity_t e) const
Definition world.hpp:364
flecs::entity target(int32_t index=0) const
Get target for a given pair from a singleton entity.
Definition world.hpp:322
const T * try_get() const
Get singleton component.
Definition world.hpp:141
flecs::entity set_scope() const
Same as set_scope but with type.
Definition world.hpp:89
void children(Func &&f) const
Iterate entities in root of world Accepts a callback with the following signature:
Definition world.hpp:312
void modified() const
Mark singleton component as modified.
Definition world.hpp:107
flecs::entity use(const char *alias=nullptr) const
Create alias for component.
Definition world.hpp:51
void add() const
Add singleton component.
Definition world.hpp:266
T & ensure() const
Ensure singleton component.
Definition world.hpp:100
void set(const T &value) const
Set singleton component.
Definition world.hpp:662
bool has() const
Test if world has singleton component.
Definition world.hpp:243
flecs::entity singleton() const
Get singleton entity for type.
Definition world.hpp:317
ref< T > get_ref() const
Get ref singleton component.
Definition world.hpp:125