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 component<Component>("flecs::core::Component");
13 component<Identifier>("flecs::core::Identifier");
14 component<Poly>("flecs::core::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 = _::cpp_type<T>::id(m_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(m_world, e);
40 }
41 ecs_set_alias(m_world, e, name);
42 return flecs::entity(m_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(m_world, 0, name, "::", "::", true);
47 ecs_assert(e != 0, ECS_INVALID_PARAMETER, NULL);
48
49 ecs_set_alias(m_world, e, alias);
50 return flecs::entity(m_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 ecs_get_name(m_world, eid);
59 }
60 ecs_set_alias(m_world, eid, alias);
61}
62
63inline flecs::entity world::set_scope(const flecs::entity_t s) const {
64 return flecs::entity(ecs_set_scope(m_world, s));
65}
66
68 return flecs::entity(m_world, ecs_get_scope(m_world));
69}
70
71template <typename T>
73 return set_scope( _::cpp_type<T>::id(m_world) );
74}
75
76inline entity world::lookup(const char *name) const {
77 auto e = ecs_lookup_path_w_sep(m_world, 0, name, "::", "::", true);
78 return flecs::entity(*this, e);
79}
80
81template <typename T>
82inline T* world::get_mut() const {
83 flecs::entity e(m_world, _::cpp_type<T>::id(m_world));
84 return e.get_mut<T>();
85}
86
87template <typename T>
88inline void world::modified() const {
89 flecs::entity e(m_world, _::cpp_type<T>::id(m_world));
90 return e.modified<T>();
91}
92
93template <typename First, typename Second>
94inline void world::set(Second second, const First& value) const {
95 flecs::entity e(m_world, _::cpp_type<First>::id(m_world));
96 e.set<First>(second, value);
97}
98
99template <typename First, typename Second>
100inline void world::set(Second second, First&& value) const {
101 flecs::entity e(m_world, _::cpp_type<First>::id(m_world));
102 e.set<First>(second, value);
103}
104
105template <typename T>
106inline ref<T> world::get_ref() const {
107 flecs::entity e(m_world, _::cpp_type<T>::id(m_world));
108 return e.get_ref<T>();
109}
110
111template <typename T>
112inline const T* world::get() const {
113 flecs::entity e(m_world, _::cpp_type<T>::id(m_world));
114 return e.get<T>();
115}
116
117template <typename First, typename Second, typename P, typename A>
118const A* world::get() const {
119 flecs::entity e(m_world, _::cpp_type<First>::id(m_world));
120 return e.get<First, Second>();
121}
122
123template <typename First, typename Second>
124const First* world::get(Second second) const {
125 flecs::entity e(m_world, _::cpp_type<First>::id(m_world));
126 return e.get<First>(second);
127}
128
129template <typename T>
130inline bool world::has() const {
131 flecs::entity e(m_world, _::cpp_type<T>::id(m_world));
132 return e.has<T>();
133}
134
135template <typename First, typename Second>
136inline bool world::has() const {
137 flecs::entity e(m_world, _::cpp_type<First>::id(m_world));
138 return e.has<First, Second>();
139}
140
141template <typename First>
142inline bool world::has(flecs::id_t second) const {
143 flecs::entity e(m_world, _::cpp_type<First>::id(m_world));
144 return e.has<First>(second);
145}
146
147inline bool world::has(flecs::id_t first, flecs::id_t second) const {
148 flecs::entity e(m_world, first);
149 return e.has(first, second);
150}
151
152template <typename T>
153inline void world::add() const {
154 flecs::entity e(m_world, _::cpp_type<T>::id(m_world));
155 e.add<T>();
156}
157
158template <typename First, typename Second>
159inline void world::add() const {
160 flecs::entity e(m_world, _::cpp_type<First>::id(m_world));
161 e.add<First, Second>();
162}
163
164template <typename First>
165inline void world::add(flecs::entity_t second) const {
166 flecs::entity e(m_world, _::cpp_type<First>::id(m_world));
167 e.add<First>(second);
168}
169
170inline void world::add(flecs::entity_t first, flecs::entity_t second) const {
171 flecs::entity e(m_world, first);
172 e.add(first, second);
173}
174
175template <typename T>
176inline void world::remove() const {
177 flecs::entity e(m_world, _::cpp_type<T>::id(m_world));
178 e.remove<T>();
179}
180
181template <typename First, typename Second>
182inline void world::remove() const {
183 flecs::entity e(m_world, _::cpp_type<First>::id(m_world));
184 e.remove<First, Second>();
185}
186
187template <typename First>
188inline void world::remove(flecs::entity_t second) const {
189 flecs::entity e(m_world, _::cpp_type<First>::id(m_world));
190 e.remove<First>(second);
191}
192
193inline void world::remove(flecs::entity_t first, flecs::entity_t second) const {
194 flecs::entity e(m_world, first);
195 e.remove(first, second);
196}
197
198template <typename T>
200 return flecs::entity(m_world, _::cpp_type<T>::id(m_world));
201}
202
203template <typename Func, if_t< is_callable<Func>::value > >
204inline void world::get(const Func& func) const {
205 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
207 this->m_world, this->singleton<first_arg_t<Func>>(), func);
208}
209
210template <typename Func, if_t< is_callable<Func>::value > >
211inline void world::set(const Func& func) const {
212 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
214 this->m_world, this->singleton<first_arg_t<Func>>(), func);
215}
216
217inline flecs::entity world::get_alive(flecs::entity_t e) const {
218 e = ecs_get_alive(m_world, e);
219 return flecs::entity(m_world, e);
220}
221/* Prevent clashing with Unreal define. Unreal applications will have to use
222 * ecs_ensure. */
223#ifndef ensure
224inline flecs::entity world::ensure(flecs::entity_t e) const {
225 ecs_ensure(m_world, e);
226 return flecs::entity(m_world, e);
227}
228#endif
229
230template <typename E>
231inline flecs::entity enum_data<E>::entity() const {
232 return flecs::entity(world_, impl_.id);
233}
234
235template <typename E>
236inline flecs::entity enum_data<E>::entity(int value) const {
237 return flecs::entity(world_, impl_.constants[value].id);
238}
239
240template <typename E>
241inline flecs::entity enum_data<E>::entity(E value) const {
242 return flecs::entity(world_, impl_.constants[static_cast<int>(value)].id);
243}
244
248inline flecs::scoped_world world::scope(id_t parent) const {
249 return scoped_world(m_world, parent);
250}
251
252template <typename T>
253inline flecs::scoped_world world::scope() const {
254 flecs::id_t parent = _::cpp_type<T>::id(m_world);
255 return scoped_world(m_world, parent);
256}
257
258} // namespace flecs
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:352
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:273
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.
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:236
Component reference.
Definition ref.hpp:23
Scoped world.
Definition world.hpp:1026
flecs::entity get_scope() const
Get current scope.
Definition world.hpp:67
T * get_mut() const
Get mut singleton component.
Definition world.hpp:82
void remove() const
Remove singleton component.
Definition world.hpp:176
flecs::entity get_alive(flecs::entity_t e) const
Get alive entity for id.
Definition world.hpp:217
const T * get() const
Get singleton component.
Definition world.hpp:112
flecs::entity lookup(const char *name) const
Lookup entity by name.
Definition world.hpp:76
flecs::entity set_scope() const
Same as set_scope but with type.
Definition world.hpp:72
flecs::entity ensure(flecs::entity_t e) const
Ensures that entity with provided generation is alive.
Definition world.hpp:224
void modified() const
Mark singleton component as modified.
Definition world.hpp:88
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:153
void set(const T &value) const
Set singleton component.
Definition world.hpp:544
bool has() const
Test if world has singleton component.
Definition world.hpp:130
flecs::entity singleton() const
Get singleton entity for type.
Definition world.hpp:199
ref< T > get_ref() const
Get ref singleton component.
Definition world.hpp:106