Flecs v3.1
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>
82T* 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>
88void world::modified() const {
89 flecs::entity e(m_world, _::cpp_type<T>::id(m_world));
90 return e.modified<T>();
91}
92
93template <typename T>
95 flecs::entity e(m_world, _::cpp_type<T>::id(m_world));
96 return e.get_ref<T>();
97}
98
99template <typename T>
100const T* world::get() const {
101 flecs::entity e(m_world, _::cpp_type<T>::id(m_world));
102 return e.get<T>();
103}
104
105template <typename T>
106bool world::has() const {
107 flecs::entity e(m_world, _::cpp_type<T>::id(m_world));
108 return e.has<T>();
109}
110
111template <typename T>
112void world::add() const {
113 flecs::entity e(m_world, _::cpp_type<T>::id(m_world));
114 e.add<T>();
115}
116
117template <typename T>
118void world::remove() const {
119 flecs::entity e(m_world, _::cpp_type<T>::id(m_world));
120 e.remove<T>();
121}
122
123template <typename T>
125 return flecs::entity(m_world, _::cpp_type<T>::id(m_world));
126}
127
128template <typename Func, if_t< is_callable<Func>::value > >
129void world::get(const Func& func) const {
130 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
132 this->m_world, this->singleton<first_arg_t<Func>>(), func);
133}
134
135template <typename Func, if_t< is_callable<Func>::value > >
136void world::set(const Func& func) const {
137 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
139 this->m_world, this->singleton<first_arg_t<Func>>(), func);
140}
141
142inline flecs::entity world::get_alive(flecs::entity_t e) const {
143 e = ecs_get_alive(m_world, e);
144 return flecs::entity(m_world, e);
145}
146/* Prevent clashing with Unreal define. Unreal applications will have to use
147 * ecs_ensure. */
148#ifndef ensure
149inline flecs::entity world::ensure(flecs::entity_t e) const {
150 ecs_ensure(m_world, e);
151 return flecs::entity(m_world, e);
152}
153#endif
154
155template <typename E>
156inline flecs::entity enum_data<E>::entity() const {
157 return flecs::entity(world_, impl_.id);
158}
159
160template <typename E>
161inline flecs::entity enum_data<E>::entity(int value) const {
162 return flecs::entity(world_, impl_.constants[value].id);
163}
164
165template <typename E>
166inline flecs::entity enum_data<E>::entity(E value) const {
167 return flecs::entity(world_, impl_.constants[static_cast<int>(value)].id);
168}
169
173inline flecs::scoped_world world::scope(id_t parent) const {
174 return scoped_world(m_world, parent);
175}
176
177template <typename T>
178inline flecs::scoped_world world::scope() const {
179 flecs::id_t parent = _::cpp_type<T>::id(m_world);
180 return scoped_world(m_world, parent);
181}
182
183} // 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.
Definition: entity_view.hpp:50
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:224
Component reference.
Definition: ref.hpp:23
Scoped world.
Definition: world.hpp:907
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:118
flecs::entity get_alive(flecs::entity_t e) const
Get alive entity for id.
Definition: world.hpp:142
const T * get() const
Get singleton component.
Definition: world.hpp:100
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:149
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:112
void set(const T &value) const
Set singleton component.
Definition: world.hpp:536
bool has() const
Test if world has singleton component.
Definition: world.hpp:106
flecs::entity singleton() const
Get singleton entity for type.
Definition: world.hpp:124
ref< T > get_ref() const
Get ref singleton component.
Definition: world.hpp:94