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