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<Target>();
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
83template <typename T>
84inline T* world::get_mut() const {
85 flecs::entity e(m_world, _::cpp_type<T>::id(m_world));
86 return e.get_mut<T>();
87}
88
89template <typename T>
90inline void world::modified() const {
91 flecs::entity e(m_world, _::cpp_type<T>::id(m_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(m_world, _::cpp_type<First>::id(m_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(m_world, _::cpp_type<First>::id(m_world));
104 e.set<First>(second, value);
105}
106
107template <typename T>
108inline ref<T> world::get_ref() const {
109 flecs::entity e(m_world, _::cpp_type<T>::id(m_world));
110 return e.get_ref<T>();
111}
112
113template <typename T>
114inline const T* world::get() const {
115 flecs::entity e(m_world, _::cpp_type<T>::id(m_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(m_world, _::cpp_type<First>::id(m_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(m_world, _::cpp_type<First>::id(m_world));
128 return e.get<First>(second);
129}
130
131template <typename T>
132inline bool world::has() const {
133 flecs::entity e(m_world, _::cpp_type<T>::id(m_world));
134 return e.has<T>();
135}
136
137template <typename First, typename Second>
138inline bool world::has() const {
139 flecs::entity e(m_world, _::cpp_type<First>::id(m_world));
140 return e.has<First, Second>();
141}
142
143template <typename First>
144inline bool world::has(flecs::id_t second) const {
145 flecs::entity e(m_world, _::cpp_type<First>::id(m_world));
146 return e.has<First>(second);
147}
148
149inline bool world::has(flecs::id_t first, flecs::id_t second) const {
150 flecs::entity e(m_world, first);
151 return e.has(first, second);
152}
153
154template <typename T>
155inline void world::add() const {
156 flecs::entity e(m_world, _::cpp_type<T>::id(m_world));
157 e.add<T>();
158}
159
160template <typename First, typename Second>
161inline void world::add() const {
162 flecs::entity e(m_world, _::cpp_type<First>::id(m_world));
163 e.add<First, Second>();
164}
165
166template <typename First>
167inline void world::add(flecs::entity_t second) const {
168 flecs::entity e(m_world, _::cpp_type<First>::id(m_world));
169 e.add<First>(second);
170}
171
172inline void world::add(flecs::entity_t first, flecs::entity_t second) const {
173 flecs::entity e(m_world, first);
174 e.add(first, second);
175}
176
177template <typename T>
178inline void world::remove() const {
179 flecs::entity e(m_world, _::cpp_type<T>::id(m_world));
180 e.remove<T>();
181}
182
183template <typename First, typename Second>
184inline void world::remove() const {
185 flecs::entity e(m_world, _::cpp_type<First>::id(m_world));
186 e.remove<First, Second>();
187}
188
189template <typename First>
190inline void world::remove(flecs::entity_t second) const {
191 flecs::entity e(m_world, _::cpp_type<First>::id(m_world));
192 e.remove<First>(second);
193}
194
195inline void world::remove(flecs::entity_t first, flecs::entity_t second) const {
196 flecs::entity e(m_world, first);
197 e.remove(first, second);
198}
199
200template <typename Func>
201inline void world::children(Func&& f) const {
202 this->entity(0).children(FLECS_FWD(f));
203}
204
205template <typename T>
207 return flecs::entity(m_world, _::cpp_type<T>::id(m_world));
208}
209
210template <typename First>
211inline flecs::entity world::target(int32_t index) const
212{
213 return flecs::entity(m_world,
214 ecs_get_target(m_world, _::cpp_type<First>::id(m_world), _::cpp_type<First>::id(m_world), index));
215}
216
217template <typename T>
219 flecs::entity_t relationship,
220 int32_t index) const
221{
222 return flecs::entity(m_world,
223 ecs_get_target(m_world, _::cpp_type<T>::id(m_world), relationship, index));
224}
225
227 flecs::entity_t relationship,
228 int32_t index) const
229{
230 return flecs::entity(m_world,
231 ecs_get_target(m_world, relationship, relationship, index));
232}
233
234template <typename Func, if_t< is_callable<Func>::value > >
235inline void world::get(const Func& func) const {
236 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
238 this->m_world, this->singleton<first_arg_t<Func>>(), func);
239}
240
241template <typename Func, if_t< is_callable<Func>::value > >
242inline void world::set(const Func& func) const {
243 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
245 this->m_world, this->singleton<first_arg_t<Func>>(), func);
246}
247
248inline flecs::entity world::get_alive(flecs::entity_t e) const {
249 e = ecs_get_alive(m_world, e);
250 return flecs::entity(m_world, e);
251}
252/* Prevent clashing with Unreal define. Unreal applications will have to use
253 * ecs_ensure. */
254#ifndef ensure
255inline flecs::entity world::ensure(flecs::entity_t e) const {
256 ecs_ensure(m_world, e);
257 return flecs::entity(m_world, e);
258}
259#endif
260
261template <typename E>
262inline flecs::entity enum_data<E>::entity() const {
263 return flecs::entity(world_, impl_.id);
264}
265
266template <typename E>
267inline flecs::entity enum_data<E>::entity(int value) const {
268 return flecs::entity(world_, impl_.constants[value].id);
269}
270
271template <typename E>
272inline flecs::entity enum_data<E>::entity(E value) const {
273 return flecs::entity(world_, impl_.constants[static_cast<int>(value)].id);
274}
275
279inline flecs::scoped_world world::scope(id_t parent) const {
280 return scoped_world(m_world, parent);
281}
282
283template <typename T>
284inline flecs::scoped_world world::scope() const {
285 flecs::id_t parent = _::cpp_type<T>::id(m_world);
286 return scoped_world(m_world, parent);
287}
288
289inline flecs::scoped_world world::scope(const char* name) const {
290 return scope(entity(name));
291}
292
293} // namespace flecs
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:351
flecs::entity entity(Args &&... args) const
Create an 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_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.
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:258
Component reference.
Definition ref.hpp:23
Scoped world.
Definition world.hpp:1116
flecs::entity get_scope() const
Get current scope.
Definition world.hpp:69
T * get_mut() const
Get mut singleton component.
Definition world.hpp:84
void remove() const
Remove singleton component.
Definition world.hpp:178
flecs::entity get_alive(flecs::entity_t e) const
Get alive entity for id.
Definition world.hpp:248
flecs::entity target(int32_t index=0) const
Get target for a given pair from a singleton entity.
Definition world.hpp:211
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:74
void children(Func &&f) const
Iterate entities in root of world Accepts a callback with the following signature:
Definition world.hpp:201
flecs::entity ensure(flecs::entity_t e) const
Ensures that entity with provided generation is alive.
Definition world.hpp:255
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:90
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:155
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:132
flecs::entity singleton() const
Get singleton entity for type.
Definition world.hpp:206
ref< T > get_ref() const
Get ref singleton component.
Definition world.hpp:108