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