Flecs v4.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
11/* Static helper functions to assign a component value */
12
13// set(T&&)
14template <typename T>
15inline void set(world_t *world, flecs::entity_t entity, T&& value, flecs::id_t id) {
16 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
17 "operation invalid for empty type");
18
19 ecs_cpp_get_mut_t res = ecs_cpp_set(world, entity, id, &value, sizeof(T));
20
21 T& dst = *static_cast<remove_reference_t<T>*>(res.ptr);
22 if constexpr (std::is_copy_assignable_v<T>) {
23 dst = FLECS_FWD(value);
24 } else {
25 dst = FLECS_MOV(value);
26 }
27
28 if (res.stage) {
29 flecs_defer_end(res.world, res.stage);
30 }
31
32 if (res.call_modified) {
33 ecs_modified_id(world, entity, id);
34 }
35}
36
37// set(const T&)
38template <typename T>
39inline void set(world_t *world, flecs::entity_t entity, const T& value, flecs::id_t id) {
40 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
41 "operation invalid for empty type");
42
43 ecs_cpp_get_mut_t res = ecs_cpp_set(world, entity, id, &value, sizeof(T));
44
45 T& dst = *static_cast<remove_reference_t<T>*>(res.ptr);
46 dst = value;
47
48 if (res.stage) {
49 flecs_defer_end(res.world, res.stage);
50 }
51
52 if (res.call_modified) {
53 ecs_modified_id(world, entity, id);
54 }
55}
56
57// set(T&&)
58template <typename T, typename A>
59inline void set(world_t *world, entity_t entity, A&& value) {
60 id_t id = _::type<T>::id(world);
61 flecs::set(world, entity, FLECS_FWD(value), id);
62}
63
64// set(const T&)
65template <typename T, typename A>
66inline void set(world_t *world, entity_t entity, const A& value) {
67 id_t id = _::type<T>::id(world);
68 flecs::set(world, entity, value, id);
69}
70
71// assign(T&&)
72template <typename T>
73inline void assign(world_t *world, flecs::entity_t entity, T&& value, flecs::id_t id) {
74 ecs_assert(_::type<remove_reference_t<T>>::size() != 0,
75 ECS_INVALID_PARAMETER, "operation invalid for empty type");
76
77 ecs_cpp_get_mut_t res = ecs_cpp_assign(
78 world, entity, id, &value, sizeof(T));
79
80 T& dst = *static_cast<remove_reference_t<T>*>(res.ptr);
81 if constexpr (std::is_copy_assignable_v<T>) {
82 dst = FLECS_FWD(value);
83 } else {
84 dst = FLECS_MOV(value);
85 }
86
87 if (res.stage) {
88 flecs_defer_end(res.world, res.stage);
89 }
90
91 if (res.call_modified) {
92 ecs_modified_id(world, entity, id);
93 }
94}
95
96// assign(const T&)
97template <typename T>
98inline void assign(world_t *world, flecs::entity_t entity, const T& value, flecs::id_t id) {
99 ecs_assert(_::type<remove_reference_t<T>>::size() != 0,
100 ECS_INVALID_PARAMETER, "operation invalid for empty type");
101
102 ecs_cpp_get_mut_t res = ecs_cpp_assign(
103 world, entity, id, &value, sizeof(T));
104
105 T& dst = *static_cast<remove_reference_t<T>*>(res.ptr);
106 dst = value;
107
108 if (res.stage) {
109 flecs_defer_end(res.world, res.stage);
110 }
111
112 if (res.call_modified) {
113 ecs_modified_id(world, entity, id);
114 }
115}
116
117// set(T&&)
118template <typename T, typename A>
119inline void assign(world_t *world, entity_t entity, A&& value) {
120 id_t id = _::type<T>::id(world);
121 flecs::assign(world, entity, FLECS_FWD(value), id);
122}
123
124// set(const T&)
125template <typename T, typename A>
126inline void assign(world_t *world, entity_t entity, const A& value) {
127 id_t id = _::type<T>::id(world);
128 flecs::assign(world, entity, value, id);
129}
130
131
132// emplace for T(Args...)
133template <typename T, typename ... Args, if_t<
134 std::is_constructible<actual_type_t<T>, Args...>::value ||
135 std::is_default_constructible<actual_type_t<T>>::value > = 0>
136inline void emplace(world_t *world, flecs::entity_t entity, flecs::id_t id, Args&&... args) {
137 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
138 "operation invalid for empty type");
139 T& dst = *static_cast<T*>(ecs_emplace_id(world, entity, id, sizeof(T), nullptr));
140
141 FLECS_PLACEMENT_NEW(&dst, T{FLECS_FWD(args)...});
142
143 ecs_modified_id(world, entity, id);
144}
145
150inline flecs::id_t strip_generation(flecs::entity_t e) {
151 return ecs_strip_generation(e);
152}
153
156inline uint32_t get_generation(flecs::entity_t e) {
157 return ECS_GENERATION(e);
158}
159
160struct scoped_world;
161
174struct world {
177 explicit world()
178 : world_( ecs_init() ) {
179 init_builtin_components();
180 }
181
186 explicit world(int argc, char *argv[])
187 : world_( ecs_init_w_args(argc, argv) ) {
188 init_builtin_components();
189 }
190
193 explicit world(world_t *w)
194 : world_( w ) {
195 if (w) {
196 flecs_poly_claim(w);
197 }
198 }
199
202 world(const world& obj) {
203 this->world_ = obj.world_;
204 flecs_poly_claim(this->world_);
205 }
206
207 world& operator=(const world& obj) noexcept {
208 release();
209 this->world_ = obj.world_;
210 flecs_poly_claim(this->world_);
211 return *this;
212 }
213
214 world(world&& obj) noexcept {
215 world_ = obj.world_;
216 obj.world_ = nullptr;
217 }
218
219 world& operator=(world&& obj) noexcept {
220 release();
221 world_ = obj.world_;
222 obj.world_ = nullptr;
223 return *this;
224 }
225
226 /* Releases the underlying world object. If this is the last handle, the world
227 will be finalized. */
228 void release() {
229 if (world_) {
230 if (!flecs_poly_release(world_)) {
231 if (ecs_stage_get_id(world_) == -1) {
232 ecs_stage_free(world_);
233 } else {
234 // before we call ecs_fini(), we increment the reference count back to 1
235 // otherwise, copies of this object created during ecs_fini (e.g. a component on_remove hook)
236 // would call again this destructor and ecs_fini().
237 flecs_poly_claim(world_);
238 ecs_fini(world_);
239 }
240 }
241 world_ = nullptr;
242 }
243 }
244
245 ~world() {
246 release();
247 }
248
249 /* Implicit conversion to world_t* */
250 operator world_t*() const { return world_; }
251
259 void make_owner() {
260 flecs_poly_release(world_);
261 }
262
264 void reset() {
265 /* Make sure there's only one reference to the world */
266 ecs_assert(flecs_poly_refcount(world_) == 1, ECS_INVALID_OPERATION,
267 "reset would invalidate other handles");
268 ecs_fini(world_);
269 world_ = ecs_init();
270 }
271
274 world_t* c_ptr() const {
275 return world_;
276 }
277
281 void quit() const {
282 ecs_quit(world_);
283 }
284
287 void atfini(ecs_fini_action_t action, void *ctx = nullptr) const {
288 ecs_atfini(world_, action, ctx);
289 }
290
293 bool should_quit() const {
294 return ecs_should_quit(world_);
295 }
296
319 return ecs_frame_begin(world_, delta_time);
320 }
321
331 void frame_end() const {
332 ecs_frame_end(world_);
333 }
334
345 bool readonly_begin(bool multi_threaded = false) const {
346 return ecs_readonly_begin(world_, multi_threaded);
347 }
348
355 void readonly_end() const {
356 ecs_readonly_end(world_);
357 }
358
375 bool defer_begin() const {
376 return ecs_defer_begin(world_);
377 }
378
394 bool defer_end() const {
395 return ecs_defer_end(world_);
396 }
397
410 bool is_deferred() const {
411 return ecs_is_deferred(world_);
412 }
413
426 bool is_defer_suspended() const {
427 return ecs_is_defer_suspended(world_);
428 }
429
445 void set_stage_count(int32_t stages) const {
446 ecs_set_stage_count(world_, stages);
447 }
448
457 int32_t get_stage_count() const {
458 return ecs_get_stage_count(world_);
459 }
460
467 int32_t get_stage_id() const {
468 return ecs_stage_get_id(world_);
469 }
470
477 bool is_stage() const {
479 flecs_poly_is(world_, ecs_world_t) ||
480 flecs_poly_is(world_, ecs_stage_t),
481 ECS_INVALID_PARAMETER,
482 "flecs::world instance contains invalid reference to world or stage");
483 return flecs_poly_is(world_, ecs_stage_t);
484 }
485
496 void merge() const {
497 ecs_merge(world_);
498 }
499
514 flecs::world get_stage(int32_t stage_id) const {
515 return flecs::world(ecs_get_stage(world_, stage_id));
516 }
517
534 ecs_world_t *as = ecs_stage_new(world_);
535 flecs_poly_release(as); // world object will claim
536 return flecs::world(as);
537 }
538
546 /* Safe cast, mutability is checked */
547 return flecs::world(
548 world_ ? const_cast<flecs::world_t*>(ecs_get_world(world_)) : nullptr);
549 }
550
561 bool is_readonly() const {
562 return ecs_stage_is_readonly(world_);
563 }
564
576 void set_ctx(void* ctx, ecs_ctx_free_t ctx_free = nullptr) const {
577 ecs_set_ctx(world_, ctx, ctx_free);
578 }
579
589 void* get_ctx() const {
590 return ecs_get_ctx(world_);
591 }
592
604 void set_binding_ctx(void* ctx, ecs_ctx_free_t ctx_free = nullptr) const {
605 ecs_set_binding_ctx(world_, ctx, ctx_free);
606 }
607
617 void* get_binding_ctx() const {
618 return ecs_get_binding_ctx(world_);
619 }
620
628 void dim(int32_t entity_count) const {
629 ecs_dim(world_, entity_count);
630 }
631
640 void set_entity_range(entity_t min, entity_t max) const {
641 ecs_set_entity_range(world_, min, max);
642 }
643
654 void enable_range_check(bool enabled = true) const {
655 ecs_enable_range_check(world_, enabled);
656 }
657
666 flecs::entity set_scope(const flecs::entity_t scope) const;
667
675 flecs::entity get_scope() const;
676
682 template <typename T>
683 flecs::entity set_scope() const;
684
690 flecs::entity_t* set_lookup_path(const flecs::entity_t *search_path) const {
691 return ecs_set_lookup_path(world_, search_path);
692 }
693
700 flecs::entity lookup(const char *name, const char *sep = "::", const char *root_sep = "::", bool recursive = true) const;
701
704 template <typename T, if_t< !is_callable<T>::value > = 0>
705 void set(const T& value) const {
706 flecs::set<T>(world_, _::type<T>::id(world_), value);
707 }
708
711 template <typename T, if_t< !is_callable<T>::value > = 0>
712 void set(T&& value) const {
713 flecs::set<T>(world_, _::type<T>::id(world_),
714 FLECS_FWD(value));
715 }
716
719 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
720 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
721 void set(const A& value) const {
722 flecs::set<P>(world_, _::type<First>::id(world_), value);
723 }
724
727 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
728 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
729 void set(A&& value) const {
730 flecs::set<P>(world_, _::type<First>::id(world_), FLECS_FWD(value));
731 }
732
735 template <typename First, typename Second>
736 void set(Second second, const First& value) const;
737
740 template <typename First, typename Second>
741 void set(Second second, First&& value) const;
742
745 template <typename Func, if_t< is_callable<Func>::value > = 0 >
746 void set(const Func& func) const;
747
748 template <typename T, typename ... Args>
749 void emplace(Args&&... args) const {
750 flecs::id_t component_id = _::type<T>::id(world_);
751 flecs::emplace<T>(world_, component_id, component_id, FLECS_FWD(args)...);
752 }
753
756 #ifndef ensure
757 template <typename T>
758 T& ensure() const;
759 #endif
760
763 template <typename T>
764 void modified() const;
765
768 template <typename T>
769 ref<T> get_ref() const;
770
771
772 /* try_get */
773
776 const void* try_get(flecs::id_t id) const;
777
780 const void* try_get(flecs::entity_t r, flecs::entity_t t) const;
781
784 template <typename T>
785 const T* try_get() const;
786
789 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
790 typename A = actual_type_t<P>>
791 const A* try_get() const;
792
795 template <typename First, typename Second>
796 const First* try_get(Second second) const;
797
798
799 /* get */
800
803 const void* get(flecs::id_t id) const;
804
807 const void* get(flecs::entity_t r, flecs::entity_t t) const;
808
809 template <typename T>
810 const T& get() const;
811
814 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
815 typename A = actual_type_t<P>>
816 const A& get() const;
817
820 template <typename First, typename Second>
821 const First& get(Second second) const;
822
825 template <typename Func, if_t< is_callable<Func>::value > = 0 >
826 void get(const Func& func) const;
827
828
829 /* try_get_mut */
830
833 void* try_get_mut(flecs::id_t id) const;
834
837 void* try_get_mut(flecs::entity_t r, flecs::entity_t t) const;
838
839 template <typename T>
840 T* try_get_mut() const;
841
844 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
845 typename A = actual_type_t<P>>
846 A* try_get_mut() const;
847
850 template <typename First, typename Second>
851 First* try_get_mut(Second second) const;
852
853
854 /* get_mut */
855
858 void* get_mut(flecs::id_t id) const;
859
862 void* get_mut(flecs::entity_t r, flecs::entity_t t) const;
863
864 template <typename T>
865 T& get_mut() const;
866
869 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
870 typename A = actual_type_t<P>>
871 A& get_mut() const;
872
875 template <typename First, typename Second>
876 First& get_mut(Second second) const;
877
878
884 template <typename T>
885 bool has() const;
886
893 template <typename First, typename Second>
894 bool has() const;
895
902 template <typename First>
903 bool has(flecs::id_t second) const;
904
911 bool has(flecs::id_t first, flecs::id_t second) const;
912
919 template <typename E, if_t< is_enum<E>::value > = 0>
920 bool has(E value) const;
921
924 template <typename T>
925 void add() const;
926
932 template <typename First, typename Second>
933 void add() const;
934
940 template <typename First>
941 void add(flecs::entity_t second) const;
942
948 void add(flecs::entity_t first, flecs::entity_t second) const;
949
955 template <typename E, if_t< is_enum<E>::value > = 0>
956 void add(E value) const;
957
960 template <typename T>
961 void remove() const;
962
968 template <typename First, typename Second>
969 void remove() const;
970
976 template <typename First>
977 void remove(flecs::entity_t second) const;
978
984 void remove(flecs::entity_t first, flecs::entity_t second) const;
985
993 template <typename Func>
994 void children(Func&& f) const;
995
998 template <typename T>
999 flecs::entity singleton() const;
1000
1009 template<typename First>
1010 flecs::entity target(int32_t index = 0) const;
1011
1020 template<typename T>
1021 flecs::entity target(flecs::entity_t first, int32_t index = 0) const;
1022
1031 flecs::entity target(flecs::entity_t first, int32_t index = 0) const;
1032
1039 template <typename T>
1040 flecs::entity use(const char *alias = nullptr) const;
1041
1047 flecs::entity use(const char *name, const char *alias = nullptr) const;
1048
1054 void use(flecs::entity entity, const char *alias = nullptr) const;
1055
1060 int count(flecs::id_t component_id) const {
1061 return ecs_count_id(world_, component_id);
1062 }
1063
1069 int count(flecs::entity_t first, flecs::entity_t second) const {
1070 return ecs_count_id(world_, ecs_pair(first, second));
1071 }
1072
1077 template <typename T>
1078 int count() const {
1079 return count(_::type<T>::id(world_));
1080 }
1081
1087 template <typename First>
1088 int count(flecs::entity_t second) const {
1089 return count(_::type<First>::id(world_), second);
1090 }
1091
1097 template <typename First, typename Second>
1098 int count() const {
1099 return count(
1100 _::type<First>::id(world_),
1101 _::type<Second>::id(world_));
1102 }
1103
1106 template <typename Func>
1107 void with(id_t with_id, const Func& func) const {
1108 ecs_id_t prev = ecs_set_with(world_, with_id);
1109 func();
1110 ecs_set_with(world_, prev);
1111 }
1112
1115 template <typename T, typename Func>
1116 void with(const Func& func) const {
1117 with(this->id<T>(), func);
1118 }
1119
1122 template <typename First, typename Second, typename Func>
1123 void with(const Func& func) const {
1124 with(ecs_pair(this->id<First>(), this->id<Second>()), func);
1125 }
1126
1129 template <typename First, typename Func>
1130 void with(id_t second, const Func& func) const {
1131 with(ecs_pair(this->id<First>(), second), func);
1132 }
1133
1136 template <typename Func>
1137 void with(id_t first, id_t second, const Func& func) const {
1138 with(ecs_pair(first, second), func);
1139 }
1140
1144 template <typename Func>
1145 void scope(id_t parent, const Func& func) const {
1146 ecs_entity_t prev = ecs_set_scope(world_, parent);
1147 func();
1148 ecs_set_scope(world_, prev);
1149 }
1150
1153 template <typename T, typename Func>
1154 void scope(const Func& func) const {
1155 flecs::id_t parent = _::type<T>::id(world_);
1156 scope(parent, func);
1157 }
1158
1162 flecs::scoped_world scope(id_t parent) const;
1163
1164 template <typename T>
1165 flecs::scoped_world scope() const;
1166
1167 flecs::scoped_world scope(const char* name) const;
1168
1170 void delete_with(id_t the_id) const {
1171 ecs_delete_with(world_, the_id);
1172 }
1173
1175 void delete_with(entity_t first, entity_t second) const {
1176 delete_with(ecs_pair(first, second));
1177 }
1178
1180 template <typename T>
1181 void delete_with() const {
1182 delete_with(_::type<T>::id(world_));
1183 }
1184
1186 template <typename First, typename Second>
1187 void delete_with() const {
1189 }
1190
1192 template <typename First>
1193 void delete_with(entity_t second) const {
1194 delete_with(_::type<First>::id(world_), second);
1195 }
1196
1198 void remove_all(id_t the_id) const {
1199 ecs_remove_all(world_, the_id);
1200 }
1201
1203 void remove_all(entity_t first, entity_t second) const {
1204 remove_all(ecs_pair(first, second));
1205 }
1206
1208 template <typename T>
1209 void remove_all() const {
1210 remove_all(_::type<T>::id(world_));
1211 }
1212
1214 template <typename First, typename Second>
1215 void remove_all() const {
1217 }
1218
1220 template <typename First>
1221 void remove_all(entity_t second) const {
1222 remove_all(_::type<First>::id(world_), second);
1223 }
1224
1233 template <typename Func>
1234 void defer(const Func& func) const {
1235 ecs_defer_begin(world_);
1236 func();
1237 ecs_defer_end(world_);
1238 }
1239
1249 void defer_suspend() const {
1250 ecs_defer_suspend(world_);
1251 }
1252
1262 void defer_resume() const {
1263 ecs_defer_resume(world_);
1264 }
1265
1272 bool exists(flecs::entity_t e) const {
1273 return ecs_exists(world_, e);
1274 }
1275
1282 bool is_alive(flecs::entity_t e) const {
1283 return ecs_is_alive(world_, e);
1284 }
1285
1293 bool is_valid(flecs::entity_t e) const {
1294 return ecs_is_valid(world_, e);
1295 }
1296
1302 flecs::entity get_alive(flecs::entity_t e) const;
1303
1307 flecs::entity make_alive(flecs::entity_t e) const;
1308
1313 void set_version(flecs::entity_t e) const {
1314 ecs_set_version(world_, e);
1315 }
1316
1321 uint32_t get_version(flecs::entity_t e) const {
1322 return ecs_get_version(e);
1323 }
1324
1325 /* Run callback after completing frame */
1326 void run_post_frame(ecs_fini_action_t action, void *ctx) const {
1327 ecs_run_post_frame(world_, action, ctx);
1328 }
1329
1335 return ecs_get_world_info(world_);
1336 }
1337
1340 return get_info()->delta_time;
1341 }
1342
1347 void shrink() const {
1348 ecs_shrink(world_);
1349 }
1350
1356 void exclusive_access_begin(const char *thread_name = nullptr) {
1357 ecs_exclusive_access_begin(world_, thread_name);
1358 }
1359
1365 void exclusive_access_end(bool lock_world = false) {
1366 ecs_exclusive_access_end(world_, lock_world);
1367 }
1368
1375 template <typename T>
1376 flecs::id_t id_if_registered() {
1377 if (_::type<T>::registered(world_)) {
1378 return _::type<T>::id(world_);
1379 }
1380 else {
1381 return 0;
1382 }
1383 }
1384
1387 return ecs_get_type_info(world_, component);
1388 }
1389
1391 const flecs::type_info_t* type_info(flecs::entity_t r, flecs::entity_t t) {
1392 return ecs_get_type_info(world_, ecs_pair(r, t));
1393 }
1394
1396 template <typename T>
1398 return ecs_get_type_info(world_, _::type<T>::id(world_));
1399 }
1400
1402 template <typename R>
1403 const flecs::type_info_t* type_info(flecs::entity_t t) {
1404 return type_info(_::type<R>::id(world_), t);
1405 }
1406
1408 template <typename R, typename T>
1410 return type_info<R>(_::type<T>::id(world_));
1411 }
1412
1413# include "mixins/id/mixin.inl"
1415# include "mixins/entity/mixin.inl"
1416# include "mixins/event/mixin.inl"
1417# include "mixins/term/mixin.inl"
1418# include "mixins/observer/mixin.inl"
1419# include "mixins/query/mixin.inl"
1420# include "mixins/enum/mixin.inl"
1421
1422# ifdef FLECS_MODULE
1423# include "mixins/module/mixin.inl"
1424# endif
1425# ifdef FLECS_PIPELINE
1426# include "mixins/pipeline/mixin.inl"
1427# endif
1428# ifdef FLECS_SYSTEM
1429# include "mixins/system/mixin.inl"
1430# endif
1431# ifdef FLECS_TIMER
1432# include "mixins/timer/mixin.inl"
1433# endif
1434# ifdef FLECS_SCRIPT
1435# include "mixins/script/mixin.inl"
1436# endif
1437# ifdef FLECS_META
1438# include "mixins/meta/world.inl"
1439# endif
1440# ifdef FLECS_JSON
1441# include "mixins/json/world.inl"
1442# endif
1443# ifdef FLECS_APP
1444# include "mixins/app/mixin.inl"
1445# endif
1446# ifdef FLECS_METRICS
1447# include "mixins/metrics/mixin.inl"
1448# endif
1449# ifdef FLECS_ALERTS
1450# include "mixins/alerts/mixin.inl"
1451# endif
1452
1453public:
1454 void init_builtin_components();
1455
1456 world_t *world_;
1457};
1458
1464 flecs::world_t *w,
1465 flecs::entity_t s) : world(w)
1466 {
1467 prev_scope_ = ecs_set_scope(w, s);
1468 }
1469
1470 ~scoped_world() {
1471 ecs_set_scope(world_, prev_scope_);
1472 }
1473
1474 scoped_world(const scoped_world& obj) : world(nullptr) {
1475 prev_scope_ = obj.prev_scope_;
1476 world_ = obj.world_;
1477 flecs_poly_claim(world_);
1478 }
1479
1480 flecs::entity_t prev_scope_;
1481};
1482
1485} // namespace flecs
App world addon mixin.
Component mixin.
Entity world mixin.
Enum world mixin.
Event world mixin.
void ecs_remove_all(ecs_world_t *world, ecs_id_t component)
Remove all instances of the specified component.
ecs_entity_t ecs_set_with(ecs_world_t *world, ecs_id_t component)
Create new entities with specified component.
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:368
ecs_world_t * ecs_stage_new(ecs_world_t *world)
Create unmanaged stage.
bool ecs_defer_end(ecs_world_t *world)
End block of operations to defer.
bool ecs_readonly_begin(ecs_world_t *world, bool multi_threaded)
Begin readonly mode.
void ecs_defer_resume(ecs_world_t *world)
Resume deferring.
bool ecs_defer_begin(ecs_world_t *world)
Defer operations until end of frame.
void ecs_defer_suspend(ecs_world_t *world)
Suspend deferring but do not flush queue.
bool ecs_is_deferred(const ecs_world_t *world)
Test if deferring is enabled for current stage.
void ecs_stage_free(ecs_world_t *stage)
Free unmanaged stage.
void ecs_merge(ecs_world_t *stage)
Merge stage.
int32_t ecs_stage_get_id(const ecs_world_t *world)
Get stage id.
bool ecs_stage_is_readonly(const ecs_world_t *world)
Test whether the current world is readonly.
int32_t ecs_get_stage_count(const ecs_world_t *world)
Get number of configured stages.
ecs_world_t * ecs_get_stage(const ecs_world_t *world, int32_t stage_id)
Get stage-specific world pointer.
void ecs_set_stage_count(ecs_world_t *world, int32_t stages)
Configure world to have N stages.
void ecs_readonly_end(ecs_world_t *world)
End readonly mode.
bool ecs_is_defer_suspended(const ecs_world_t *world)
Test if deferring is suspended for current stage.
const ecs_type_info_t * ecs_get_type_info(const ecs_world_t *world, ecs_id_t component)
Get the type info for an component.
struct ecs_stage_t ecs_stage_t
A stage enables modification while iterating and from multiple threads.
Definition flecs.h:414
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:367
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:411
uint64_t ecs_id_t
Ids are the things that can be added to an entity.
Definition flecs.h:360
flecs::entity entity(Args &&... args) const
Create an entity.
void ecs_delete_with(ecs_world_t *world, ecs_id_t component)
Delete all entities with the specified component.
int32_t ecs_count_id(const ecs_world_t *world, ecs_id_t entity)
Count entities that have the specified id.
void(* ecs_fini_action_t)(ecs_world_t *world, void *ctx)
Action callback on world exit.
Definition flecs.h:626
void(* ecs_ctx_free_t)(void *ctx)
Function to cleanup context data.
Definition flecs.h:631
void * ecs_emplace_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component, size_t size, bool *is_new)
Emplace a component.
void ecs_modified_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Signal that a component has been modified.
ecs_id_t ecs_strip_generation(ecs_entity_t e)
Remove generation from entity id.
bool ecs_is_valid(const ecs_world_t *world, ecs_entity_t e)
Test whether an entity is valid.
uint32_t ecs_get_version(ecs_entity_t entity)
Get generation of an entity.
bool ecs_exists(const ecs_world_t *world, ecs_entity_t entity)
Test whether an entity exists.
bool ecs_is_alive(const ecs_world_t *world, ecs_entity_t e)
Test whether an entity is alive.
void ecs_set_version(ecs_world_t *world, ecs_entity_t entity)
Override the generation of an entity.
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition flecs.h:59
ecs_entity_t * ecs_set_lookup_path(ecs_world_t *world, const ecs_entity_t *lookup_path)
Set search path for lookup operations.
ecs_entity_t ecs_set_scope(ecs_world_t *world, ecs_entity_t scope)
Set the current scope.
void ecs_atfini(ecs_world_t *world, ecs_fini_action_t action, void *ctx)
Register action to be executed when world is destroyed.
int ecs_fini(ecs_world_t *world)
Delete a world.
ecs_world_t * ecs_init(void)
Create a new world.
ecs_world_t * ecs_init_w_args(int argc, char *argv[])
Create a new world with arguments.
float ecs_frame_begin(ecs_world_t *world, float delta_time)
Begin frame.
void ecs_run_post_frame(ecs_world_t *world, ecs_fini_action_t action, void *ctx)
Register action to be executed once after frame.
bool ecs_should_quit(const ecs_world_t *world)
Return whether a quit has been requested.
void ecs_quit(ecs_world_t *world)
Signal exit This operation signals that the application should quit.
void ecs_frame_end(ecs_world_t *world)
End frame.
void * ecs_get_binding_ctx(const ecs_world_t *world)
Get the world binding context.
void ecs_shrink(ecs_world_t *world)
Free unused memory.
void ecs_dim(ecs_world_t *world, int32_t entity_count)
Dimension the world for a specified number of entities.
void ecs_set_entity_range(ecs_world_t *world, ecs_entity_t id_start, ecs_entity_t id_end)
Set a range for issuing new entity ids.
const ecs_world_info_t * ecs_get_world_info(const ecs_world_t *world)
Get world info.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
void ecs_set_ctx(ecs_world_t *world, void *ctx, ecs_ctx_free_t ctx_free)
Set a world context.
void ecs_exclusive_access_begin(ecs_world_t *world, const char *thread_name)
Begin exclusive thread access.
void ecs_set_binding_ctx(ecs_world_t *world, void *ctx, ecs_ctx_free_t ctx_free)
Set a world binding context.
#define flecs_poly_is(object, type)
Test if pointer is of specified type.
Definition flecs.h:2786
bool ecs_enable_range_check(ecs_world_t *world, bool enable)
Enable/disable range limits.
void ecs_exclusive_access_end(ecs_world_t *world, bool lock_world)
End exclusive thread access.
void * ecs_get_ctx(const ecs_world_t *world)
Get the world context.
Id world mixin.
JSON world mixin.
Meta world mixin.
Module world mixin.
Observer world mixin.
Pipeline world mixin.
Query world mixin.
Script world mixin.
Type that contains component information (passed to ctors/dtors/...)
Definition flecs.h:1001
Type that contains information about the world.
Definition flecs.h:1455
float delta_time
Time passed to or computed by ecs_progress()
Definition flecs.h:1461
Component class.
Entity.
Definition entity.hpp:30
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
Scoped world.
Definition world.hpp:1462
The world.
Definition world.hpp:174
bool is_stage() const
Test if is a stage.
Definition world.hpp:477
void shrink() const
Free unused memory.
Definition world.hpp:1347
uint32_t get_version(flecs::entity_t e) const
Get version of provided entity.
Definition world.hpp:1321
void delete_with() const
Delete all entities with specified component.
Definition world.hpp:1181
void remove_all(id_t the_id) const
Remove all instances of specified id.
Definition world.hpp:1198
void delete_with(entity_t second) const
Delete all entities with specified pair.
Definition world.hpp:1193
void merge() const
Merge world or stage.
Definition world.hpp:496
void delete_with(id_t the_id) const
Delete all entities with specified id.
Definition world.hpp:1170
const flecs::world_info_t * get_info() const
Get the world info.
Definition world.hpp:1334
flecs::entity get_scope() const
Get current scope.
Definition world.hpp:85
void remove() const
Remove singleton component.
Definition world.hpp:302
flecs::entity lookup(const char *name, const char *sep="::", const char *root_sep="::", bool recursive=true) const
Lookup entity by name.
Definition world.hpp:94
void set(A &&value) const
Set singleton pair.
Definition world.hpp:729
ecs_ftime_t delta_time() const
Get delta_time.
Definition world.hpp:1339
void quit() const
Signal application should quit.
Definition world.hpp:281
flecs::entity get_alive(flecs::entity_t e) const
Get alive entity for id.
Definition world.hpp:372
const flecs::type_info_t * type_info()
Return type info.
Definition world.hpp:1397
void set_entity_range(entity_t min, entity_t max) const
Set entity range.
Definition world.hpp:640
void readonly_end() const
End readonly mode.
Definition world.hpp:355
void with(const Func &func) const
All entities created in function are created with pair.
Definition world.hpp:1123
flecs::entity make_alive(flecs::entity_t e) const
Definition world.hpp:377
int count() const
Count entities matching a component.
Definition world.hpp:1078
void exclusive_access_begin(const char *thread_name=nullptr)
Begin exclusive access.
Definition world.hpp:1356
flecs::entity target(int32_t index=0) const
Get target for a given pair from a singleton entity.
Definition world.hpp:335
void defer(const Func &func) const
Defer all operations called in function.
Definition world.hpp:1234
bool should_quit() const
Test if quit() has been called.
Definition world.hpp:293
bool is_alive(flecs::entity_t e) const
Check if entity id exists in the world.
Definition world.hpp:1282
world_t * c_ptr() const
Obtain pointer to C world object.
Definition world.hpp:274
const T * try_get() const
Get singleton component.
Definition world.hpp:142
bool defer_begin() const
Defer operations until end of frame.
Definition world.hpp:375
void reset()
Deletes and recreates the world.
Definition world.hpp:264
flecs::entity_t * set_lookup_path(const flecs::entity_t *search_path) const
Set search path.
Definition world.hpp:690
void defer_suspend() const
Suspend deferring operations.
Definition world.hpp:1249
void set(const A &value) const
Set singleton pair.
Definition world.hpp:721
void make_owner()
Make current world object owner of the world.
Definition world.hpp:259
bool is_valid(flecs::entity_t e) const
Check if entity id is valid.
Definition world.hpp:1293
flecs::world async_stage() const
Create asynchronous stage.
Definition world.hpp:533
bool is_deferred() const
Test whether deferring is enabled.
Definition world.hpp:410
void * get_binding_ctx() const
Get world binding context.
Definition world.hpp:617
int32_t get_stage_id() const
Get current stage id.
Definition world.hpp:467
world(const world &obj)
Not allowed to copy a world.
Definition world.hpp:202
void scope(const Func &func) const
Same as scope(parent, func), but with T as parent.
Definition world.hpp:1154
void defer_resume() const
Resume deferring operations.
Definition world.hpp:1262
flecs::entity set_scope() const
Same as set_scope but with type.
Definition world.hpp:90
void dim(int32_t entity_count) const
Preallocate memory for number of entities.
Definition world.hpp:628
void set_version(flecs::entity_t e) const
Set version of entity to provided.
Definition world.hpp:1313
void set_stage_count(int32_t stages) const
Configure world to have N stages.
Definition world.hpp:445
void with(id_t with_id, const Func &func) const
All entities created in function are created with id.
Definition world.hpp:1107
void * get_ctx() const
Get world context.
Definition world.hpp:589
const flecs::type_info_t * type_info(flecs::entity_t r, flecs::entity_t t)
Return type info.
Definition world.hpp:1391
int count() const
Count entities matching a pair.
Definition world.hpp:1098
void remove_all() const
Remove all instances of specified pair.
Definition world.hpp:1215
bool defer_end() const
End block of operations to defer.
Definition world.hpp:394
int count(flecs::entity_t first, flecs::entity_t second) const
Count entities matching a pair.
Definition world.hpp:1069
void remove_all(entity_t second) const
Remove all instances of specified pair.
Definition world.hpp:1221
world(world_t *w)
Create world from C world.
Definition world.hpp:193
void children(Func &&f) const
Iterate entities in root of world Accepts a callback with the following signature:
Definition world.hpp:325
flecs::world get_world() const
Get actual world.
Definition world.hpp:545
void scope(id_t parent, const Func &func) const
All entities created in function are created in scope.
Definition world.hpp:1145
void with(const Func &func) const
All entities created in function are created with type.
Definition world.hpp:1116
void remove_all(entity_t first, entity_t second) const
Remove all instances of specified pair.
Definition world.hpp:1203
void modified() const
Mark singleton component as modified.
Definition world.hpp:108
void enable_range_check(bool enabled=true) const
Enforce that operations cannot modify entities outside of range.
Definition world.hpp:654
int count(flecs::entity_t second) const
Count entities matching a pair.
Definition world.hpp:1088
flecs::id_t id_if_registered()
Return component id if it has been registered.
Definition world.hpp:1376
ecs_ftime_t frame_begin(float delta_time=0) const
Begin frame.
Definition world.hpp:318
flecs::entity use(const char *alias=nullptr) const
Create alias for component.
Definition world.hpp:52
bool is_readonly() const
Test whether the current world object is readonly.
Definition world.hpp:561
void add() const
Add singleton component.
Definition world.hpp:273
T & ensure() const
Ensure singleton component.
Definition world.hpp:101
bool readonly_begin(bool multi_threaded=false) const
Begin readonly mode.
Definition world.hpp:345
void set(const T &value) const
Set singleton component.
Definition world.hpp:705
void with(id_t first, id_t second, const Func &func) const
All entities created in function are created with pair.
Definition world.hpp:1137
flecs::world get_stage(int32_t stage_id) const
Get stage-specific world pointer.
Definition world.hpp:514
void set(T &&value) const
Set singleton component.
Definition world.hpp:712
bool has() const
Test if world has singleton component.
Definition world.hpp:244
bool exists(flecs::entity_t e) const
Check if entity id exists in the world.
Definition world.hpp:1272
world()
Create world.
Definition world.hpp:177
void frame_end() const
End frame.
Definition world.hpp:331
void set_binding_ctx(void *ctx, ecs_ctx_free_t ctx_free=nullptr) const
Set world binding context.
Definition world.hpp:604
void atfini(ecs_fini_action_t action, void *ctx=nullptr) const
Register action to be executed when world is destroyed.
Definition world.hpp:287
const flecs::type_info_t * type_info(flecs::id_t component)
Return type info.
Definition world.hpp:1386
int32_t get_stage_count() const
Get number of configured stages.
Definition world.hpp:457
void delete_with(entity_t first, entity_t second) const
Delete all entities with specified pair.
Definition world.hpp:1175
void delete_with() const
Delete all entities with specified pair.
Definition world.hpp:1187
flecs::entity singleton() const
Get singleton entity for type.
Definition world.hpp:330
void exclusive_access_end(bool lock_world=false)
End exclusive access.
Definition world.hpp:1365
void with(id_t second, const Func &func) const
All entities created in function are created with pair.
Definition world.hpp:1130
void set_ctx(void *ctx, ecs_ctx_free_t ctx_free=nullptr) const
Set world context.
Definition world.hpp:576
bool is_defer_suspended() const
Test whether deferring is suspended.
Definition world.hpp:426
world(int argc, char *argv[])
Create world with command line arguments.
Definition world.hpp:186
int count(flecs::id_t component_id) const
Count entities matching a component.
Definition world.hpp:1060
const flecs::type_info_t * type_info()
Return type info.
Definition world.hpp:1409
const flecs::type_info_t * type_info(flecs::entity_t t)
Return type info.
Definition world.hpp:1403
void remove_all() const
Remove all instances of specified component.
Definition world.hpp:1209
ref< T > get_ref() const
Get ref singleton component.
Definition world.hpp:126
System module world mixin.
Term world mixin.
Timer module mixin.
flecs::id_t strip_generation(flecs::entity_t e)
Return id without generation.
Definition world.hpp:150
uint32_t get_generation(flecs::entity_t e)
Return entity generation.
Definition world.hpp:156