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# ifdef FLECS_SYSTEM
17 _::system_init(*this);
18# endif
19# ifdef FLECS_TIMER
20 _::timer_init(*this);
21# endif
22# ifdef FLECS_DOC
23 doc::_::init(*this);
24# endif
25# ifdef FLECS_REST
26 rest::_::init(*this);
27# endif
28# ifdef FLECS_META
29 meta::_::init(*this);
30# endif
31}
32
33template <typename T>
34inline flecs::entity world::use(const char *alias) const {
35 entity_t e = _::type<T>::id(world_);
36 const char *name = alias;
37 if (!name) {
38 // If no name is defined, use the entity name without the scope
39 name = ecs_get_name(world_, e);
40 }
41 ecs_set_alias(world_, e, name);
42 return flecs::entity(world_, e);
43}
44
45inline flecs::entity world::use(const char *name, const char *alias) const {
46 entity_t e = ecs_lookup_path_w_sep(world_, 0, name, "::", "::", true);
47 ecs_assert(e != 0, ECS_INVALID_PARAMETER, NULL);
48
49 ecs_set_alias(world_, e, alias);
50 return flecs::entity(world_, e);
51}
52
53inline void world::use(flecs::entity e, const char *alias) const {
54 entity_t eid = e.id();
55 const char *name = alias;
56 if (!name) {
57 // If no name is defined, use the entity name without the scope
58 name = ecs_get_name(world_, eid);
59 }
60 ecs_set_alias(world_, eid, name);
61}
62
63inline flecs::entity world::set_scope(const flecs::entity_t s) const {
64 return flecs::entity(ecs_set_scope(world_, s));
65}
66
68 return flecs::entity(world_, ecs_get_scope(world_));
69}
70
71template <typename T>
73 return set_scope( _::type<T>::id(world_) );
74}
75
76inline entity world::lookup(const char *name, const char *sep, const char *root_sep, bool recursive) const {
77 auto e = ecs_lookup_path_w_sep(world_, 0, name, sep, root_sep, recursive);
78 return flecs::entity(*this, e);
79}
80
81#ifndef ensure
82template <typename T>
83inline T& world::ensure() const {
84 flecs::entity e(world_, _::type<T>::id(world_));
85 return e.ensure<T>();
86}
87#endif
88
89template <typename T>
90inline void world::modified() const {
91 flecs::entity e(world_, _::type<T>::id(world_));
92 e.modified<T>();
93}
94
95template <typename First, typename Second>
96inline void world::set(Second second, const First& value) const {
97 flecs::entity e(world_, _::type<First>::id(world_));
98 e.set<First>(second, value);
99}
100
101template <typename First, typename Second>
102inline void world::set(Second second, First&& value) const {
103 flecs::entity e(world_, _::type<First>::id(world_));
104 e.set<First>(second, value);
105}
106
107template <typename T>
108inline ref<T> world::get_ref() const {
109 flecs::entity e(world_, _::type<T>::id(world_));
110 return e.get_ref<T>();
111}
112
113template <typename T>
114inline const T* world::get() const {
115 flecs::entity e(world_, _::type<T>::id(world_));
116 return e.get<T>();
117}
118
119template <typename First, typename Second, typename P, typename A>
120const A* world::get() const {
121 flecs::entity e(world_, _::type<First>::id(world_));
122 return e.get<First, Second>();
123}
124
125template <typename First, typename Second>
126const First* world::get(Second second) const {
127 flecs::entity e(world_, _::type<First>::id(world_));
128 return e.get<First>(second);
129}
130
131template <typename T>
132T* world::get_mut() const {
133 flecs::entity e(world_, _::type<T>::id(world_));
134 return e.get_mut<T>();
135}
136
137template <typename First, typename Second, typename P, typename A>
138A* world::get_mut() const {
139 flecs::entity e(world_, _::type<First>::id(world_));
140 return e.get_mut<First, Second>();
141}
142
143template <typename First, typename Second>
144First* world::get_mut(Second second) const {
145 flecs::entity e(world_, _::type<First>::id(world_));
146 return e.get_mut<First>(second);
147}
148
149template <typename T>
150inline bool world::has() const {
151 flecs::entity e(world_, _::type<T>::id(world_));
152 return e.has<T>();
153}
154
155template <typename First, typename Second>
156inline bool world::has() const {
157 flecs::entity e(world_, _::type<First>::id(world_));
158 return e.has<First, Second>();
159}
160
161template <typename First>
162inline bool world::has(flecs::id_t second) const {
163 flecs::entity e(world_, _::type<First>::id(world_));
164 return e.has<First>(second);
165}
166
167inline bool world::has(flecs::id_t first, flecs::id_t second) const {
168 flecs::entity e(world_, first);
169 return e.has(first, second);
170}
171
172template <typename T>
173inline void world::add() const {
174 flecs::entity e(world_, _::type<T>::id(world_));
175 e.add<T>();
176}
177
178template <typename First, typename Second>
179inline void world::add() const {
180 flecs::entity e(world_, _::type<First>::id(world_));
181 e.add<First, Second>();
182}
183
184template <typename First>
185inline void world::add(flecs::entity_t second) const {
186 flecs::entity e(world_, _::type<First>::id(world_));
187 e.add<First>(second);
188}
189
190inline void world::add(flecs::entity_t first, flecs::entity_t second) const {
191 flecs::entity e(world_, first);
192 e.add(first, second);
193}
194
195template <typename T>
196inline void world::remove() const {
197 flecs::entity e(world_, _::type<T>::id(world_));
198 e.remove<T>();
199}
200
201template <typename First, typename Second>
202inline void world::remove() const {
203 flecs::entity e(world_, _::type<First>::id(world_));
204 e.remove<First, Second>();
205}
206
207template <typename First>
208inline void world::remove(flecs::entity_t second) const {
209 flecs::entity e(world_, _::type<First>::id(world_));
210 e.remove<First>(second);
211}
212
213inline void world::remove(flecs::entity_t first, flecs::entity_t second) const {
214 flecs::entity e(world_, first);
215 e.remove(first, second);
216}
217
218template <typename Func>
219inline void world::children(Func&& f) const {
220 this->entity(0).children(FLECS_FWD(f));
221}
222
223template <typename T>
225 return flecs::entity(world_, _::type<T>::id(world_));
226}
227
228template <typename First>
229inline flecs::entity world::target(int32_t index) const
230{
231 return flecs::entity(world_,
232 ecs_get_target(world_, _::type<First>::id(world_), _::type<First>::id(world_), index));
233}
234
235template <typename T>
237 flecs::entity_t relationship,
238 int32_t index) const
239{
240 return flecs::entity(world_,
241 ecs_get_target(world_, _::type<T>::id(world_), relationship, index));
242}
243
245 flecs::entity_t relationship,
246 int32_t index) const
247{
248 return flecs::entity(world_,
249 ecs_get_target(world_, relationship, relationship, index));
250}
251
252template <typename Func, if_t< is_callable<Func>::value > >
253inline void world::get(const Func& func) const {
254 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
256 this->world_, this->singleton<first_arg_t<Func>>(), func);
257}
258
259template <typename Func, if_t< is_callable<Func>::value > >
260inline void world::set(const Func& func) const {
261 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
263 this->world_, this->singleton<first_arg_t<Func>>(), func);
264}
265
266inline flecs::entity world::get_alive(flecs::entity_t e) const {
267 e = ecs_get_alive(world_, e);
268 return flecs::entity(world_, e);
269}
270
271inline flecs::entity world::make_alive(flecs::entity_t e) const {
272 ecs_make_alive(world_, e);
273 return flecs::entity(world_, e);
274}
275
276template <typename E>
277inline flecs::entity enum_data<E>::entity() const {
278 return flecs::entity(world_, _::type<E>::id(world_));
279}
280
281template <typename E>
282inline flecs::entity enum_data<E>::entity(underlying_type_t<E> value) const {
283 int index = index_by_value(value);
284 if (index >= 0) {
285 int32_t constant_i = impl_.constants[index].index;
286 flecs::entity_t entity = flecs_component_ids_get(world_, constant_i);
287 return flecs::entity(world_, entity);
288 }
289#ifdef FLECS_META
290 // Reflection data lookup failed. Try value lookup amongst flecs::Constant relationships
291 flecs::world world = flecs::world(world_);
292 return world.query_builder()
293 .with(flecs::ChildOf, world.id<E>())
294 .with(flecs::Constant, world.id<int32_t>())
295 .build()
296 .find([value](flecs::entity constant) {
297 const int32_t *constant_value = constant.get_second<int32_t>(flecs::Constant);
298 ecs_assert(constant_value, ECS_INTERNAL_ERROR, NULL);
299 return value == static_cast<underlying_type_t<E>>(*constant_value);
300 });
301#else
302 return flecs::entity::null(world_);
303#endif
304}
305
306template <typename E>
307inline flecs::entity enum_data<E>::entity(E value) const {
308 return entity(static_cast<underlying_type_t<E>>(value));
309}
310
314inline flecs::scoped_world world::scope(id_t parent) const {
315 return scoped_world(world_, parent);
316}
317
318template <typename T>
319inline flecs::scoped_world world::scope() const {
320 flecs::id_t parent = _::type<T>::id(world_);
321 return scoped_world(world_, parent);
322}
323
324inline flecs::scoped_world world::scope(const char* name) const {
325 return scope(entity(name));
326}
327
328} // namespace flecs
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:352
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 & add() const
Add a component to an entity.
Definition builder.hpp:25
const T * get() const
Get component value.
T * get_mut() const
Get mutable component value.
bool has(flecs::id_t e) const
Check if entity has the provided entity.
entity_t id() const
Get entity id.
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:247
void modified() const
Signal that component was modified.
Definition entity.hpp:186
T & ensure() const
Get mutable component value.
Definition entity.hpp:99
static flecs::entity null(const flecs::world_t *world)
Entity id 0.
Definition entity.hpp:331
Convenience type with enum reflection data.
Definition enum.hpp:406
Component reference.
Definition ref.hpp:23
Scoped world.
Definition world.hpp:1234
The world.
Definition world.hpp:137
flecs::entity get_scope() const
Get current scope.
Definition world.hpp:67
T * get_mut() const
Get mutable singleton component.
Definition world.hpp:132
void remove() const
Remove singleton component.
Definition world.hpp:196
flecs::entity lookup(const char *name, const char *sep="::", const char *root_sep="::", bool recursive=true) const
Lookup entity by name.
Definition world.hpp:76
flecs::entity get_alive(flecs::entity_t e) const
Get alive entity for id.
Definition world.hpp:266
flecs::entity make_alive(flecs::entity_t e) const
Definition world.hpp:271
flecs::entity target(int32_t index=0) const
Get target for a given pair from a singleton entity.
Definition world.hpp:229
const T * get() const
Get singleton component.
Definition world.hpp:114
flecs::entity set_scope() const
Same as set_scope but with type.
Definition world.hpp:72
void children(Func &&f) const
Iterate entities in root of world Accepts a callback with the following signature:
Definition world.hpp:219
void modified() const
Mark singleton component as modified.
Definition world.hpp:90
flecs::entity use(const char *alias=nullptr) const
Create alias for component.
Definition world.hpp:34
void add() const
Add singleton component.
Definition world.hpp:173
T & ensure() const
Ensure singleton component.
Definition world.hpp:83
void set(const T &value) const
Set singleton component.
Definition world.hpp:649
bool has() const
Test if world has singleton component.
Definition world.hpp:150
flecs::entity singleton() const
Get singleton entity for type.
Definition world.hpp:224
ref< T > get_ref() const
Get ref singleton component.
Definition world.hpp:108