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 init_builtin_components();
271 }
272
275 world_t* c_ptr() const {
276 return world_;
277 }
278
282 void quit() const {
283 ecs_quit(world_);
284 }
285
288 void atfini(ecs_fini_action_t action, void *ctx = nullptr) const {
289 ecs_atfini(world_, action, ctx);
290 }
291
294 bool should_quit() const {
295 return ecs_should_quit(world_);
296 }
297
320 return ecs_frame_begin(world_, delta_time);
321 }
322
332 void frame_end() const {
333 ecs_frame_end(world_);
334 }
335
346 bool readonly_begin(bool multi_threaded = false) const {
347 return ecs_readonly_begin(world_, multi_threaded);
348 }
349
356 void readonly_end() const {
357 ecs_readonly_end(world_);
358 }
359
376 bool defer_begin() const {
377 return ecs_defer_begin(world_);
378 }
379
395 bool defer_end() const {
396 return ecs_defer_end(world_);
397 }
398
411 bool is_deferred() const {
412 return ecs_is_deferred(world_);
413 }
414
427 bool is_defer_suspended() const {
428 return ecs_is_defer_suspended(world_);
429 }
430
446 void set_stage_count(int32_t stages) const {
447 ecs_set_stage_count(world_, stages);
448 }
449
458 int32_t get_stage_count() const {
459 return ecs_get_stage_count(world_);
460 }
461
468 int32_t get_stage_id() const {
469 return ecs_stage_get_id(world_);
470 }
471
478 bool is_stage() const {
480 flecs_poly_is(world_, ecs_world_t) ||
481 flecs_poly_is(world_, ecs_stage_t),
482 ECS_INVALID_PARAMETER,
483 "flecs::world instance contains invalid reference to world or stage");
484 return flecs_poly_is(world_, ecs_stage_t);
485 }
486
497 void merge() const {
498 ecs_merge(world_);
499 }
500
515 flecs::world get_stage(int32_t stage_id) const {
516 return flecs::world(ecs_get_stage(world_, stage_id));
517 }
518
535 ecs_world_t *as = ecs_stage_new(world_);
536 flecs_poly_release(as); // world object will claim
537 return flecs::world(as);
538 }
539
547 /* Safe cast, mutability is checked */
548 return flecs::world(
549 world_ ? const_cast<flecs::world_t*>(ecs_get_world(world_)) : nullptr);
550 }
551
562 bool is_readonly() const {
563 return ecs_stage_is_readonly(world_);
564 }
565
577 void set_ctx(void* ctx, ecs_ctx_free_t ctx_free = nullptr) const {
578 ecs_set_ctx(world_, ctx, ctx_free);
579 }
580
590 void* get_ctx() const {
591 return ecs_get_ctx(world_);
592 }
593
605 void set_binding_ctx(void* ctx, ecs_ctx_free_t ctx_free = nullptr) const {
606 ecs_set_binding_ctx(world_, ctx, ctx_free);
607 }
608
618 void* get_binding_ctx() const {
619 return ecs_get_binding_ctx(world_);
620 }
621
629 void dim(int32_t entity_count) const {
630 ecs_dim(world_, entity_count);
631 }
632
641 void set_entity_range(entity_t min, entity_t max) const {
642 ecs_set_entity_range(world_, min, max);
643 }
644
655 void enable_range_check(bool enabled = true) const {
656 ecs_enable_range_check(world_, enabled);
657 }
658
667 flecs::entity set_scope(const flecs::entity_t scope) const;
668
676 flecs::entity get_scope() const;
677
683 template <typename T>
684 flecs::entity set_scope() const;
685
691 flecs::entity_t* set_lookup_path(const flecs::entity_t *search_path) const {
692 return ecs_set_lookup_path(world_, search_path);
693 }
694
701 flecs::entity lookup(const char *name, const char *sep = "::", const char *root_sep = "::", bool recursive = true) const;
702
705 template <typename T, if_t< !is_callable<T>::value > = 0>
706 void set(const T& value) const {
707 flecs::set<T>(world_, _::type<T>::id(world_), value);
708 }
709
712 template <typename T, if_t< !is_callable<T>::value > = 0>
713 void set(T&& value) const {
714 flecs::set<T>(world_, _::type<T>::id(world_),
715 FLECS_FWD(value));
716 }
717
720 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
721 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
722 void set(const A& value) const {
723 flecs::set<P>(world_, _::type<First>::id(world_), value);
724 }
725
728 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
729 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
730 void set(A&& value) const {
731 flecs::set<P>(world_, _::type<First>::id(world_), FLECS_FWD(value));
732 }
733
736 template <typename First, typename Second>
737 void set(Second second, const First& value) const;
738
741 template <typename First, typename Second>
742 void set(Second second, First&& value) const;
743
746 template <typename Func, if_t< is_callable<Func>::value > = 0 >
747 void set(const Func& func) const;
748
749 template <typename T, typename ... Args>
750 void emplace(Args&&... args) const {
751 flecs::id_t component_id = _::type<T>::id(world_);
752 flecs::emplace<T>(world_, component_id, component_id, FLECS_FWD(args)...);
753 }
754
757 #ifndef ensure
758 template <typename T>
759 T& ensure() const;
760 #endif
761
764 template <typename T>
765 void modified() const;
766
769 template <typename T>
770 ref<T> get_ref() const;
771
772
773 /* try_get */
774
777 const void* try_get(flecs::id_t id) const;
778
781 const void* try_get(flecs::entity_t r, flecs::entity_t t) const;
782
785 template <typename T>
786 const T* try_get() const;
787
790 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
791 typename A = actual_type_t<P>>
792 const A* try_get() const;
793
796 template <typename First, typename Second>
797 const First* try_get(Second second) const;
798
799
800 /* get */
801
804 const void* get(flecs::id_t id) const;
805
808 const void* get(flecs::entity_t r, flecs::entity_t t) const;
809
810 template <typename T>
811 const T& get() const;
812
815 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
816 typename A = actual_type_t<P>>
817 const A& get() const;
818
821 template <typename First, typename Second>
822 const First& get(Second second) const;
823
826 template <typename Func, if_t< is_callable<Func>::value > = 0 >
827 void get(const Func& func) const;
828
829
830 /* try_get_mut */
831
834 void* try_get_mut(flecs::id_t id) const;
835
838 void* try_get_mut(flecs::entity_t r, flecs::entity_t t) const;
839
840 template <typename T>
841 T* try_get_mut() const;
842
845 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
846 typename A = actual_type_t<P>>
847 A* try_get_mut() const;
848
851 template <typename First, typename Second>
852 First* try_get_mut(Second second) const;
853
854
855 /* get_mut */
856
859 void* get_mut(flecs::id_t id) const;
860
863 void* get_mut(flecs::entity_t r, flecs::entity_t t) const;
864
865 template <typename T>
866 T& get_mut() const;
867
870 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
871 typename A = actual_type_t<P>>
872 A& get_mut() const;
873
876 template <typename First, typename Second>
877 First& get_mut(Second second) const;
878
879
885 template <typename T>
886 bool has() const;
887
894 template <typename First, typename Second>
895 bool has() const;
896
903 template <typename First>
904 bool has(flecs::id_t second) const;
905
912 bool has(flecs::id_t first, flecs::id_t second) const;
913
920 template <typename E, if_t< is_enum<E>::value > = 0>
921 bool has(E value) const;
922
925 template <typename T>
926 void add() const;
927
933 template <typename First, typename Second>
934 void add() const;
935
941 template <typename First>
942 void add(flecs::entity_t second) const;
943
949 void add(flecs::entity_t first, flecs::entity_t second) const;
950
956 template <typename E, if_t< is_enum<E>::value > = 0>
957 void add(E value) const;
958
961 template <typename T>
962 void remove() const;
963
969 template <typename First, typename Second>
970 void remove() const;
971
977 template <typename First>
978 void remove(flecs::entity_t second) const;
979
985 void remove(flecs::entity_t first, flecs::entity_t second) const;
986
994 template <typename Func>
995 void children(Func&& f) const;
996
999 template <typename T>
1000 flecs::entity singleton() const;
1001
1010 template<typename First>
1011 flecs::entity target(int32_t index = 0) const;
1012
1021 template<typename T>
1022 flecs::entity target(flecs::entity_t first, int32_t index = 0) const;
1023
1032 flecs::entity target(flecs::entity_t first, int32_t index = 0) const;
1033
1040 template <typename T>
1041 flecs::entity use(const char *alias = nullptr) const;
1042
1048 flecs::entity use(const char *name, const char *alias = nullptr) const;
1049
1055 void use(flecs::entity entity, const char *alias = nullptr) const;
1056
1061 int count(flecs::id_t component_id) const {
1062 return ecs_count_id(world_, component_id);
1063 }
1064
1070 int count(flecs::entity_t first, flecs::entity_t second) const {
1071 return ecs_count_id(world_, ecs_pair(first, second));
1072 }
1073
1078 template <typename T>
1079 int count() const {
1080 return count(_::type<T>::id(world_));
1081 }
1082
1088 template <typename First>
1089 int count(flecs::entity_t second) const {
1090 return count(_::type<First>::id(world_), second);
1091 }
1092
1098 template <typename First, typename Second>
1099 int count() const {
1100 return count(
1101 _::type<First>::id(world_),
1102 _::type<Second>::id(world_));
1103 }
1104
1107 template <typename Func>
1108 void with(id_t with_id, const Func& func) const {
1109 ecs_id_t prev = ecs_set_with(world_, with_id);
1110 func();
1111 ecs_set_with(world_, prev);
1112 }
1113
1116 template <typename T, typename Func>
1117 void with(const Func& func) const {
1118 with(this->id<T>(), func);
1119 }
1120
1123 template <typename First, typename Second, typename Func>
1124 void with(const Func& func) const {
1125 with(ecs_pair(this->id<First>(), this->id<Second>()), func);
1126 }
1127
1130 template <typename First, typename Func>
1131 void with(id_t second, const Func& func) const {
1132 with(ecs_pair(this->id<First>(), second), func);
1133 }
1134
1137 template <typename Func>
1138 void with(id_t first, id_t second, const Func& func) const {
1139 with(ecs_pair(first, second), func);
1140 }
1141
1145 template <typename Func>
1146 void scope(id_t parent, const Func& func) const {
1147 ecs_entity_t prev = ecs_set_scope(world_, parent);
1148 func();
1149 ecs_set_scope(world_, prev);
1150 }
1151
1154 template <typename T, typename Func>
1155 void scope(const Func& func) const {
1156 flecs::id_t parent = _::type<T>::id(world_);
1157 scope(parent, func);
1158 }
1159
1163 flecs::scoped_world scope(id_t parent) const;
1164
1165 template <typename T>
1166 flecs::scoped_world scope() const;
1167
1168 flecs::scoped_world scope(const char* name) const;
1169
1171 void delete_with(id_t the_id) const {
1172 ecs_delete_with(world_, the_id);
1173 }
1174
1176 void delete_with(entity_t first, entity_t second) const {
1177 delete_with(ecs_pair(first, second));
1178 }
1179
1181 template <typename T>
1182 void delete_with() const {
1183 delete_with(_::type<T>::id(world_));
1184 }
1185
1187 template <typename First, typename Second>
1188 void delete_with() const {
1190 }
1191
1193 template <typename First>
1194 void delete_with(entity_t second) const {
1195 delete_with(_::type<First>::id(world_), second);
1196 }
1197
1199 void remove_all(id_t the_id) const {
1200 ecs_remove_all(world_, the_id);
1201 }
1202
1204 void remove_all(entity_t first, entity_t second) const {
1205 remove_all(ecs_pair(first, second));
1206 }
1207
1209 template <typename T>
1210 void remove_all() const {
1211 remove_all(_::type<T>::id(world_));
1212 }
1213
1215 template <typename First, typename Second>
1216 void remove_all() const {
1218 }
1219
1221 template <typename First>
1222 void remove_all(entity_t second) const {
1223 remove_all(_::type<First>::id(world_), second);
1224 }
1225
1234 template <typename Func>
1235 void defer(const Func& func) const {
1236 ecs_defer_begin(world_);
1237 func();
1238 ecs_defer_end(world_);
1239 }
1240
1250 void defer_suspend() const {
1251 ecs_defer_suspend(world_);
1252 }
1253
1263 void defer_resume() const {
1264 ecs_defer_resume(world_);
1265 }
1266
1273 bool exists(flecs::entity_t e) const {
1274 return ecs_exists(world_, e);
1275 }
1276
1283 bool is_alive(flecs::entity_t e) const {
1284 return ecs_is_alive(world_, e);
1285 }
1286
1294 bool is_valid(flecs::entity_t e) const {
1295 return ecs_is_valid(world_, e);
1296 }
1297
1303 flecs::entity get_alive(flecs::entity_t e) const;
1304
1308 flecs::entity make_alive(flecs::entity_t e) const;
1309
1314 void set_version(flecs::entity_t e) const {
1315 ecs_set_version(world_, e);
1316 }
1317
1322 uint32_t get_version(flecs::entity_t e) const {
1323 return ecs_get_version(e);
1324 }
1325
1326 /* Run callback after completing frame */
1327 void run_post_frame(ecs_fini_action_t action, void *ctx) const {
1328 ecs_run_post_frame(world_, action, ctx);
1329 }
1330
1336 return ecs_get_world_info(world_);
1337 }
1338
1341 return get_info()->delta_time;
1342 }
1343
1348 void shrink() const {
1349 ecs_shrink(world_);
1350 }
1351
1357 void exclusive_access_begin(const char *thread_name = nullptr) {
1358 ecs_exclusive_access_begin(world_, thread_name);
1359 }
1360
1366 void exclusive_access_end(bool lock_world = false) {
1367 ecs_exclusive_access_end(world_, lock_world);
1368 }
1369
1376 template <typename T>
1377 flecs::id_t id_if_registered() {
1378 if (_::type<T>::registered(world_)) {
1379 return _::type<T>::id(world_);
1380 }
1381 else {
1382 return 0;
1383 }
1384 }
1385
1388 return ecs_get_type_info(world_, component);
1389 }
1390
1392 const flecs::type_info_t* type_info(flecs::entity_t r, flecs::entity_t t) {
1393 return ecs_get_type_info(world_, ecs_pair(r, t));
1394 }
1395
1397 template <typename T>
1399 return ecs_get_type_info(world_, _::type<T>::id(world_));
1400 }
1401
1403 template <typename R>
1404 const flecs::type_info_t* type_info(flecs::entity_t t) {
1405 return type_info(_::type<R>::id(world_), t);
1406 }
1407
1409 template <typename R, typename T>
1411 return type_info<R>(_::type<T>::id(world_));
1412 }
1413
1414# include "mixins/id/mixin.inl"
1416# include "mixins/entity/mixin.inl"
1417# include "mixins/event/mixin.inl"
1418# include "mixins/term/mixin.inl"
1419# include "mixins/observer/mixin.inl"
1420# include "mixins/query/mixin.inl"
1421# include "mixins/enum/mixin.inl"
1422
1423# ifdef FLECS_MODULE
1424# include "mixins/module/mixin.inl"
1425# endif
1426# ifdef FLECS_PIPELINE
1427# include "mixins/pipeline/mixin.inl"
1428# endif
1429# ifdef FLECS_SYSTEM
1430# include "mixins/system/mixin.inl"
1431# endif
1432# ifdef FLECS_TIMER
1433# include "mixins/timer/mixin.inl"
1434# endif
1435# ifdef FLECS_SCRIPT
1436# include "mixins/script/mixin.inl"
1437# endif
1438# ifdef FLECS_META
1439# include "mixins/meta/world.inl"
1440# endif
1441# ifdef FLECS_JSON
1442# include "mixins/json/world.inl"
1443# endif
1444# ifdef FLECS_APP
1445# include "mixins/app/mixin.inl"
1446# endif
1447# ifdef FLECS_METRICS
1448# include "mixins/metrics/mixin.inl"
1449# endif
1450# ifdef FLECS_ALERTS
1451# include "mixins/alerts/mixin.inl"
1452# endif
1453
1454public:
1455 void init_builtin_components();
1456
1457 world_t *world_;
1458};
1459
1465 flecs::world_t *w,
1466 flecs::entity_t s) : world(w)
1467 {
1468 prev_scope_ = ecs_set_scope(w, s);
1469 }
1470
1471 ~scoped_world() {
1472 ecs_set_scope(world_, prev_scope_);
1473 }
1474
1475 scoped_world(const scoped_world& obj) : world(nullptr) {
1476 prev_scope_ = obj.prev_scope_;
1477 world_ = obj.world_;
1478 flecs_poly_claim(world_);
1479 }
1480
1481 flecs::entity_t prev_scope_;
1482};
1483
1486} // 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:428
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:381
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:425
uint64_t ecs_id_t
Ids are the things that can be added to an entity.
Definition flecs.h:374
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:640
void(* ecs_ctx_free_t)(void *ctx)
Function to cleanup context data.
Definition flecs.h:645
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:2803
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:1015
Type that contains information about the world.
Definition flecs.h:1474
float delta_time
Time passed to or computed by ecs_progress()
Definition flecs.h:1480
Component class.
Entity.
Definition entity.hpp:30
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
Scoped world.
Definition world.hpp:1463
The world.
Definition world.hpp:174
bool is_stage() const
Test if is a stage.
Definition world.hpp:478
void shrink() const
Free unused memory.
Definition world.hpp:1348
uint32_t get_version(flecs::entity_t e) const
Get version of provided entity.
Definition world.hpp:1322
void delete_with() const
Delete all entities with specified component.
Definition world.hpp:1182
void remove_all(id_t the_id) const
Remove all instances of specified id.
Definition world.hpp:1199
void delete_with(entity_t second) const
Delete all entities with specified pair.
Definition world.hpp:1194
void merge() const
Merge world or stage.
Definition world.hpp:497
void delete_with(id_t the_id) const
Delete all entities with specified id.
Definition world.hpp:1171
const flecs::world_info_t * get_info() const
Get the world info.
Definition world.hpp:1335
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:730
ecs_ftime_t delta_time() const
Get delta_time.
Definition world.hpp:1340
void quit() const
Signal application should quit.
Definition world.hpp:282
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:1398
void set_entity_range(entity_t min, entity_t max) const
Set entity range.
Definition world.hpp:641
void readonly_end() const
End readonly mode.
Definition world.hpp:356
void with(const Func &func) const
All entities created in function are created with pair.
Definition world.hpp:1124
flecs::entity make_alive(flecs::entity_t e) const
Definition world.hpp:377
int count() const
Count entities matching a component.
Definition world.hpp:1079
void exclusive_access_begin(const char *thread_name=nullptr)
Begin exclusive access.
Definition world.hpp:1357
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:1235
bool should_quit() const
Test if quit() has been called.
Definition world.hpp:294
bool is_alive(flecs::entity_t e) const
Check if entity id exists in the world.
Definition world.hpp:1283
world_t * c_ptr() const
Obtain pointer to C world object.
Definition world.hpp:275
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:376
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:691
void defer_suspend() const
Suspend deferring operations.
Definition world.hpp:1250
void set(const A &value) const
Set singleton pair.
Definition world.hpp:722
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:1294
flecs::world async_stage() const
Create asynchronous stage.
Definition world.hpp:534
bool is_deferred() const
Test whether deferring is enabled.
Definition world.hpp:411
void * get_binding_ctx() const
Get world binding context.
Definition world.hpp:618
int32_t get_stage_id() const
Get current stage id.
Definition world.hpp:468
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:1155
void defer_resume() const
Resume deferring operations.
Definition world.hpp:1263
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:629
void set_version(flecs::entity_t e) const
Set version of entity to provided.
Definition world.hpp:1314
void set_stage_count(int32_t stages) const
Configure world to have N stages.
Definition world.hpp:446
void with(id_t with_id, const Func &func) const
All entities created in function are created with id.
Definition world.hpp:1108
void * get_ctx() const
Get world context.
Definition world.hpp:590
const flecs::type_info_t * type_info(flecs::entity_t r, flecs::entity_t t)
Return type info.
Definition world.hpp:1392
int count() const
Count entities matching a pair.
Definition world.hpp:1099
void remove_all() const
Remove all instances of specified pair.
Definition world.hpp:1216
bool defer_end() const
End block of operations to defer.
Definition world.hpp:395
int count(flecs::entity_t first, flecs::entity_t second) const
Count entities matching a pair.
Definition world.hpp:1070
void remove_all(entity_t second) const
Remove all instances of specified pair.
Definition world.hpp:1222
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:546
void scope(id_t parent, const Func &func) const
All entities created in function are created in scope.
Definition world.hpp:1146
void with(const Func &func) const
All entities created in function are created with type.
Definition world.hpp:1117
void remove_all(entity_t first, entity_t second) const
Remove all instances of specified pair.
Definition world.hpp:1204
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:655
int count(flecs::entity_t second) const
Count entities matching a pair.
Definition world.hpp:1089
flecs::id_t id_if_registered()
Return component id if it has been registered.
Definition world.hpp:1377
ecs_ftime_t frame_begin(float delta_time=0) const
Begin frame.
Definition world.hpp:319
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:562
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:346
void set(const T &value) const
Set singleton component.
Definition world.hpp:706
void with(id_t first, id_t second, const Func &func) const
All entities created in function are created with pair.
Definition world.hpp:1138
flecs::world get_stage(int32_t stage_id) const
Get stage-specific world pointer.
Definition world.hpp:515
void set(T &&value) const
Set singleton component.
Definition world.hpp:713
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:1273
world()
Create world.
Definition world.hpp:177
void frame_end() const
End frame.
Definition world.hpp:332
void set_binding_ctx(void *ctx, ecs_ctx_free_t ctx_free=nullptr) const
Set world binding context.
Definition world.hpp:605
void atfini(ecs_fini_action_t action, void *ctx=nullptr) const
Register action to be executed when world is destroyed.
Definition world.hpp:288
const flecs::type_info_t * type_info(flecs::id_t component)
Return type info.
Definition world.hpp:1387
int32_t get_stage_count() const
Get number of configured stages.
Definition world.hpp:458
void delete_with(entity_t first, entity_t second) const
Delete all entities with specified pair.
Definition world.hpp:1176
void delete_with() const
Delete all entities with specified pair.
Definition world.hpp:1188
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:1366
void with(id_t second, const Func &func) const
All entities created in function are created with pair.
Definition world.hpp:1131
void set_ctx(void *ctx, ecs_ctx_free_t ctx_free=nullptr) const
Set world context.
Definition world.hpp:577
bool is_defer_suspended() const
Test whether deferring is suspended.
Definition world.hpp:427
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:1061
const flecs::type_info_t * type_info()
Return type info.
Definition world.hpp:1410
const flecs::type_info_t * type_info(flecs::entity_t t)
Return type info.
Definition world.hpp:1404
void remove_all() const
Remove all instances of specified component.
Definition world.hpp:1210
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