Flecs: include/flecs/addons/cpp/world.hpp Source File
Description: A fast entity component system (ECS) for C & C++

| Flecs v4.0 A fast entity component system (ECS) for C & C++

Loading...
Searching...
No Matches
world.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8namespace flecs
9{
10
11/* Static helper functions to assign a component value */
12
13// set(T&&)
14template <typename T>
15 inline 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 dst = FLECS_FWD(value);
23
24 if (res.call_modified) {
25 ecs_modified_id(world, entity, id);
26 }
27}
28
29// set(const T&)
30 template <typename T>
31inline void set(world_t *world, flecs::entity_t entity, const T& value, flecs::id_t id) {
32 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
33 "operation invalid for empty type");
34
35 ecs_cpp_get_mut_t res = ecs_cpp_set(world, entity, id, &value, sizeof(T));
36
37 T& dst = *static_cast<remove_reference_t<T>*>(res.ptr);
38 dst = value;
39
40 if (res.call_modified) {
41 ecs_modified_id(world, entity, id);
42 }
43}
44
45// set(T&&)
46template <typename T, typename A>
47inline void set(world_t *world, entity_t entity, A&& value) {
48 id_t id = _::type<T>::id(world);
49 flecs::set(world, entity, FLECS_FWD(value), id);
50}
51
52// set(const T&)
53template <typename T, typename A>
54inline void set(world_t *world, entity_t entity, const A& value) {
55 id_t id = _::type<T>::id(world);
56 flecs::set(world, entity, value, id);
57}
58
59// assign(T&&)
60template <typename T>
61inline void assign(world_t *world, flecs::entity_t entity, T&& value, flecs::id_t id) {
62 ecs_assert(_::type<remove_reference_t<T>>::size() != 0,
63 ECS_INVALID_PARAMETER, "operation invalid for empty type");
64
65 ecs_cpp_get_mut_t res = ecs_cpp_assign(
66 world, entity, id, &value, sizeof(T));
67
68 T& dst = *static_cast<remove_reference_t<T>*>(res.ptr);
69 dst = FLECS_FWD(value);
70
71 if (res.call_modified) {
72 ecs_modified_id(world, entity, id);
73 }
74}
75
76// assign(const T&)
77template <typename T>
78inline void assign(world_t *world, flecs::entity_t entity, const T& value, flecs::id_t id) {
79 ecs_assert(_::type<remove_reference_t<T>>::size() != 0,
80 ECS_INVALID_PARAMETER, "operation invalid for empty type");
81
82 ecs_cpp_get_mut_t res = ecs_cpp_assign(
83 world, entity, id, &value, sizeof(T));
84
85 T& dst = *static_cast<remove_reference_t<T>*>(res.ptr);
86 dst = value;
87
88 if (res.call_modified) {
89 ecs_modified_id(world, entity, id);
90 }
91}
92
93// set(T&&)
94template <typename T, typename A>
95inline void assign(world_t *world, entity_t entity, A&& value) {
96 id_t id = _::type<T>::id(world);
97 flecs::assign(world, entity, FLECS_FWD(value), id);
98}
99
100// set(const T&)
101template <typename T, typename A>
102inline void assign(world_t *world, entity_t entity, const A& value) {
103 id_t id = _::type<T>::id(world);
104 flecs::assign(world, entity, value, id);
105}
106
107
108// emplace for T(Args...)
109template <typename T, typename ... Args, if_t<
110 std::is_constructible<actual_type_t<T>, Args...>::value ||
111 std::is_default_constructible<actual_type_t<T>>::value > = 0>
112inline void emplace(world_t *world, flecs::entity_t entity, flecs::id_t id, Args&&... args) {
113 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
114 "operation invalid for empty type");
115 T& dst = *static_cast<T*>(ecs_emplace_id(world, entity, id, sizeof(T), nullptr));
116
117 FLECS_PLACEMENT_NEW(&dst, T{FLECS_FWD(args)...});
118
119 ecs_modified_id(world, entity, id);
120}
121
126 inline flecs::id_t strip_generation(flecs::entity_t e) {
127 return ecs_strip_generation(e);
128}
129
132 inline uint32_t get_generation(flecs::entity_t e) {
133 return ECS_GENERATION(e);
134}
135
136struct scoped_world;
137
145
150 struct world {
153 explicit world()
154 : world_( ecs_init() ) {
155 init_builtin_components();
156 }
157
162 explicit world(int argc, char *argv[])
163 : world_( ecs_init_w_args(argc, argv) ) {
164 init_builtin_components();
165 }
166
169 explicit world(world_t *w)
170 : world_( w ) {
171 if (w) {
172 flecs_poly_claim(w);
173 }
174 }
175
178 world(const world& obj) {
179 this->world_ = obj.world_;
180 flecs_poly_claim(this->world_);
181 }
182
183 world& operator=(const world& obj) noexcept {
184 release();
185 this->world_ = obj.world_;
186 flecs_poly_claim(this->world_);
187 return *this;
188 }
189
190 world(world&& obj) noexcept {
191 world_ = obj.world_;
192 obj.world_ = nullptr;
193 }
194
195 world& operator=(world&& obj) noexcept {
196 release();
197 world_ = obj.world_;
198 obj.world_ = nullptr;
199 return *this;
200 }
201
202 /* Releases the underlying world object. If this is the last handle, the world
203 will be finalized. */
204 void release() {
205 if (world_) {
206 if (!flecs_poly_release(world_)) {
207 if (ecs_stage_get_id(world_) == -1) {
208 ecs_stage_free(world_);
209 } else {
210 // before we call ecs_fini(), we increment the reference count back to 1
211 // otherwise, copies of this object created during ecs_fini (e.g. a component on_remove hook)
212 // would call again this destructor and ecs_fini().
213 flecs_poly_claim(world_);
214 ecs_fini(world_);
215 }
216 }
217 world_ = nullptr;
218 }
219 }
220
221 ~world() {
222 release();
223 }
224
225 /* Implicit conversion to world_t* */
226 operator world_t*() const { return world_; }
227
235 void make_owner() {
236 flecs_poly_release(world_);
237 }
238
240 void reset() {
241 /* Make sure there's only one reference to the world */
242 ecs_assert(flecs_poly_refcount(world_) == 1, ECS_INVALID_OPERATION,
243 "reset would invalidate other handles");
244 ecs_fini(world_);
245 world_ = ecs_init();
246 }
247
250 world_t* c_ptr() const {
251 return world_;
252 }
253
257 void quit() const {
258 ecs_quit(world_);
259 }
260
263 void atfini(ecs_fini_action_t action, void *ctx = nullptr) const {
264 ecs_atfini(world_, action, ctx);
265 }
266
269 bool should_quit() const {
270 return ecs_should_quit(world_);
271 }
272
294 ecs_ftime_t frame_begin(float delta_time = 0) const {
295 return ecs_frame_begin(world_, delta_time);
296 }
297
307 void frame_end() const {
308 ecs_frame_end(world_);
309 }
310
321 bool readonly_begin(bool multi_threaded = false) const {
322 return ecs_readonly_begin(world_, multi_threaded);
323 }
324
331 void readonly_end() const {
332 ecs_readonly_end(world_);
333 }
334
350 bool defer_begin() const {
351 return ecs_defer_begin(world_);
352 }
353
368 bool defer_end() const {
369 return ecs_defer_end(world_);
370 }
371
383 bool is_deferred() const {
384 return ecs_is_deferred(world_);
385 }
386
402 void set_stage_count(int32_t stages) const {
403 ecs_set_stage_count(world_, stages);
404 }
405
414 int32_t get_stage_count() const {
415 return ecs_get_stage_count(world_);
416 }
417
424 int32_t get_stage_id() const {
425 return ecs_stage_get_id(world_);
426 }
427
434 bool is_stage() const {
435 ecs_assert(
436 flecs_poly_is(world_, ecs_world_t) ||
437 flecs_poly_is(world_, ecs_stage_t),
438 ECS_INVALID_PARAMETER,
439 "flecs::world instance contains invalid reference to world or stage");
440 return flecs_poly_is(world_, ecs_stage_t);
441 }
442
453 void merge() const {
454 ecs_merge(world_);
455 }
456
471 flecs::world get_stage(int32_t stage_id) const {
472 return flecs::world(ecs_get_stage(world_, stage_id));
473 }
474
490 flecs::world async_stage() const {
491 ecs_world_t *as = ecs_stage_new(world_);
492 flecs_poly_release(as); // world object will claim
493 return flecs::world(as);
494 }
495
502 flecs::world get_world() const {
503 /* Safe cast, mutability is checked */
504 return flecs::world(
505 world_ ? const_cast<flecs::world_t*>(ecs_get_world(world_)) : nullptr);
506 }
507
518 bool is_readonly() const {
519 return ecs_stage_is_readonly(world_);
520 }
521
533 void set_ctx(void* ctx, ecs_ctx_free_t ctx_free = nullptr) const {
534 ecs_set_ctx(world_, ctx, ctx_free);
535 }
536
546 void* get_ctx() const {
547 return ecs_get_ctx(world_);
548 }
549
561 void set_binding_ctx(void* ctx, ecs_ctx_free_t ctx_free = nullptr) const {
562 ecs_set_binding_ctx(world_, ctx, ctx_free);
563 }
564
574 void* get_binding_ctx() const {
575 return ecs_get_binding_ctx(world_);
576 }
577
585 void dim(int32_t entity_count) const {
586 ecs_dim(world_, entity_count);
587 }
588
597 void set_entity_range(entity_t min, entity_t max) const {
598 ecs_set_entity_range(world_, min, max);
599 }
600
611 void enable_range_check(bool enabled = true) const {
612 ecs_enable_range_check(world_, enabled);
613 }
614
623 flecs::entity set_scope(const flecs::entity_t scope) const;
624
632 flecs::entity get_scope() const;
633
639 template <typename T>
640 flecs::entity set_scope() const;
641
647 flecs::entity_t* set_lookup_path(const flecs::entity_t *search_path) const {
648 return ecs_set_lookup_path(world_, search_path);
649 }
650
657 flecs::entity lookup(const char *name, const char *sep = "::", const char *root_sep = "::", bool recursive = true) const;
658
661 template <typename T, if_t< !is_callable<T>::value > = 0>
662 void set(const T& value) const {
663 flecs::set<T>(world_, _::type<T>::id(world_), value);
664 }
665
668 template <typename T, if_t< !is_callable<T>::value > = 0>
669 void set(T&& value) const {
670 flecs::set<T>(world_, _::type<T>::id(world_),
671 FLECS_FWD(value));
672 }
673
676 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
677 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
678 void set(const A& value) const {
679 flecs::set<P>(world_, _::type<First>::id(world_), value);
680 }
681
684 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
685 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
686 void set(A&& value) const {
687 flecs::set<P>(world_, _::type<First>::id(world_), FLECS_FWD(value));
688 }
689
692 template <typename First, typename Second>
693 void set(Second second, const First& value) const;
694
697 template <typename First, typename Second>
698 void set(Second second, First&& value) const;
699
702 template <typename Func, if_t< is_callable<Func>::value > = 0 >
703 void set(const Func& func) const;
704
705 template <typename T, typename ... Args>
706 void emplace(Args&&... args) const {
707 flecs::id_t component_id = _::type<T>::id(world_);
708 flecs::emplace<T>(world_, component_id, component_id, FLECS_FWD(args)...);
709 }
710
713 #ifndef ensure
714 template <typename T>
715 T& ensure() const;
716 #endif
717
720 template <typename T>
721 void modified() const;
722
725 template <typename T>
726 ref<T> get_ref() const;
727
728
729 /* try_get */
730
733 const void* try_get(flecs::id_t id) const;
734
737 const void* try_get(flecs::entity_t r, flecs::entity_t t) const;
738
741 template <typename T>
742 const T* try_get() const;
743
746 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
747 typename A = actual_type_t<P>>
748 const A* try_get() const;
749
752 template <typename First, typename Second>
753 const First* try_get(Second second) const;
754
755
756 /* get */
757
760 const void* get(flecs::id_t id) const;
761
764 const void* get(flecs::entity_t r, flecs::entity_t t) const;
765
766 template <typename T>
767 const T& get() const;
768
771 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
772 typename A = actual_type_t<P>>
773 const A& get() const;
774
777 template <typename First, typename Second>
778 const First& get(Second second) const;
779
782 template <typename Func, if_t< is_callable<Func>::value > = 0 >
783 void get(const Func& func) const;
784
785
786 /* try_get_mut */
787
790 void* try_get_mut(flecs::id_t id) const;
791
794 void* try_get_mut(flecs::entity_t r, flecs::entity_t t) const;
795
796 template <typename T>
797 T* try_get_mut() const;
798
801 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
802 typename A = actual_type_t<P>>
803 A* try_get_mut() const;
804
807 template <typename First, typename Second>
808 First* try_get_mut(Second second) const;
809
810
811 /* get_mut */
812
815 void* get_mut(flecs::id_t id) const;
816
819 void* get_mut(flecs::entity_t r, flecs::entity_t t) const;
820
821 template <typename T>
822 T& get_mut() const;
823
826 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
827 typename A = actual_type_t<P>>
828 A& get_mut() const;
829
832 template <typename First, typename Second>
833 First& get_mut(Second second) const;
834
835
838 template <typename T>
839 bool has() const;
840
846 template <typename First, typename Second>
847 bool has() const;
848
854 template <typename First>
855 bool has(flecs::id_t second) const;
856
862 bool has(flecs::id_t first, flecs::id_t second) const;
863
866 template <typename T>
867 void add() const;
868
874 template <typename First, typename Second>
875 void add() const;
876
882 template <typename First>
883 void add(flecs::entity_t second) const;
884
890 void add(flecs::entity_t first, flecs::entity_t second) const;
891
894 template <typename T>
895 void remove() const;
896
902 template <typename First, typename Second>
903 void remove() const;
904
910 template <typename First>
911 void remove(flecs::entity_t second) const;
912
918 void remove(flecs::entity_t first, flecs::entity_t second) const;
919
927 template <typename Func>
928 void children(Func&& f) const;
929
932 template <typename T>
933 flecs::entity singleton() const;
934
943 template<typename First>
944 flecs::entity target(int32_t index = 0) const;
945
954 template<typename T>
955 flecs::entity target(flecs::entity_t first, int32_t index = 0) const;
956
965 flecs::entity target(flecs::entity_t first, int32_t index = 0) const;
966
973 template <typename T>
974 flecs::entity use(const char *alias = nullptr) const;
975
981 flecs::entity use(const char *name, const char *alias = nullptr) const;
982
988 void use(flecs::entity entity, const char *alias = nullptr) const;
989
994 int count(flecs::id_t component_id) const {
995 return ecs_count_id(world_, component_id);
996 }
997
1003 int count(flecs::entity_t first, flecs::entity_t second) const {
1004 return ecs_count_id(world_, ecs_pair(first, second));
1005 }
1006
1011 template <typename T>
1012 int count() const {
1013 return count(_::type<T>::id(world_));
1014 }
1015
1021 template <typename First>
1022 int count(flecs::entity_t second) const {
1023 return count(_::type<First>::id(world_), second);
1024 }
1025
1031 template <typename First, typename Second>
1032 int count() const {
1033 return count(
1034 _::type<First>::id(world_),
1035 _::type<Second>::id(world_));
1036 }
1037
1040 template <typename Func>
1041 void with(id_t with_id, const Func& func) const {
1042 ecs_id_t prev = ecs_set_with(world_, with_id);
1043 func();
1044 ecs_set_with(world_, prev);
1045 }
1046
1049 template <typename T, typename Func>
1050 void with(const Func& func) const {
1051 with(this->id<T>(), func);
1052 }
1053
1056 template <typename First, typename Second, typename Func>
1057 void with(const Func& func) const {
1058 with(ecs_pair(this->id<First>(), this->id<Second>()), func);
1059 }
1060
1063 template <typename First, typename Func>
1064 void with(id_t second, const Func& func) const {
1065 with(ecs_pair(this->id<First>(), second), func);
1066 }
1067
1070 template <typename Func>
1071 void with(id_t first, id_t second, const Func& func) const {
1072 with(ecs_pair(first, second), func);
1073 }
1074
1078 template <typename Func>
1079 void scope(id_t parent, const Func& func) const {
1080 ecs_entity_t prev = ecs_set_scope(world_, parent);
1081 func();
1082 ecs_set_scope(world_, prev);
1083 }
1084
1087 template <typename T, typename Func>
1088 void scope(const Func& func) const {
1089 flecs::id_t parent = _::type<T>::id(world_);
1090 scope(parent, func);
1091 }
1092
1096 flecs::scoped_world scope(id_t parent) const;
1097
1098 template <typename T>
1099 flecs::scoped_world scope() const;
1100
1101 flecs::scoped_world scope(const char* name) const;
1102
1104 void delete_with(id_t the_id) const {
1105 ecs_delete_with(world_, the_id);
1106 }
1107
1109 void delete_with(entity_t first, entity_t second) const {
1110 delete_with(ecs_pair(first, second));
1111 }
1112
1114 template <typename T>
1115 void delete_with() const {
1116 delete_with(_::type<T>::id(world_));
1117 }
1118
1120 template <typename First, typename Second>
1121 void delete_with() const {
1122 delete_with(_::type<First>::id(world_), _::type<Second>::id(world_));
1123 }
1124
1126 template <typename First>
1127 void delete_with(entity_t second) const {
1128 delete_with(_::type<First>::id(world_), second);
1129 }
1130
1132 void remove_all(id_t the_id) const {
1133 ecs_remove_all(world_, the_id);
1134 }
1135
1137 void remove_all(entity_t first, entity_t second) const {
1138 remove_all(ecs_pair(first, second));
1139 }
1140
1142 template <typename T>
1143 void remove_all() const {
1144 remove_all(_::type<T>::id(world_));
1145 }
1146
1148 template <typename First, typename Second>
1149 void remove_all() const {
1150 remove_all(_::type<First>::id(world_), _::type<Second>::id(world_));
1151 }
1152
1154 template <typename First>
1155 void remove_all(entity_t second) const {
1156 remove_all(_::type<First>::id(world_), second);
1157 }
1158
1167 template <typename Func>
1168 void defer(const Func& func) const {
1169 ecs_defer_begin(world_);
1170 func();
1171 ecs_defer_end(world_);
1172 }
1173
1183 void defer_suspend() const {
1184 ecs_defer_suspend(world_);
1185 }
1186
1196 void defer_resume() const {
1197 ecs_defer_resume(world_);
1198 }
1199
1206 bool exists(flecs::entity_t e) const {
1207 return ecs_exists(world_, e);
1208 }
1209
1216 bool is_alive(flecs::entity_t e) const {
1217 return ecs_is_alive(world_, e);
1218 }
1219
1227 bool is_valid(flecs::entity_t e) const {
1228 return ecs_is_valid(world_, e);
1229 }
1230
1236 flecs::entity get_alive(flecs::entity_t e) const;
1237
1241 flecs::entity make_alive(flecs::entity_t e) const;
1242
1247 void set_version(flecs::entity_t e) const {
1248 ecs_set_version(world_, e);
1249 }
1250
1251 /* Run callback after completing frame */
1252 void run_post_frame(ecs_fini_action_t action, void *ctx) const {
1253 ecs_run_post_frame(world_, action, ctx);
1254 }
1255
1260 const flecs::world_info_t* get_info() const{
1261 return ecs_get_world_info(world_);
1262 }
1263
1265 ecs_ftime_t delta_time() const {
1266 return get_info()->delta_time;
1267 }
1268
1273 void shrink() const {
1274 ecs_shrink(world_);
1275 }
1276
1282 void exclusive_access_begin(const char *thread_name = nullptr) {
1283 ecs_exclusive_access_begin(world_, thread_name);
1284 }
1285
1291 void exclusive_access_end(bool lock_world = false) {
1292 ecs_exclusive_access_end(world_, lock_world);
1293 }
1294
1301 template <typename T>
1302 flecs::id_t id_if_registered() {
1303 if (_::type<T>::registered(world_)) {
1304 return _::type<T>::id(world_);
1305 }
1306 else {
1307 return 0;
1308 }
1309 }
1310
1312 const flecs::type_info_t* type_info(flecs::id_t component) {
1313 return ecs_get_type_info(world_, component);
1314 }
1315
1317 const flecs::type_info_t* type_info(flecs::entity_t r, flecs::entity_t t) {
1318 return ecs_get_type_info(world_, ecs_pair(r, t));
1319 }
1320
1322 template <typename T>
1323 const flecs::type_info_t* type_info() {
1324 return ecs_get_type_info(world_, _::type<T>::id(world_));
1325 }
1326
1328 template <typename R>
1329 const flecs::type_info_t* type_info(flecs::entity_t t) {
1330 return type_info(_::type<R>::id(world_), t);
1331 }
1332
1334 template <typename R, typename T>
1335 const flecs::type_info_t* type_info() {
1336 return type_info<R>(_::type<T>::id(world_));
1337 }
1338
1339# include "mixins/id/mixin.inl"
1340# include "mixins/component/mixin.inl"
1341# include "mixins/entity/mixin.inl"
1342# include "mixins/event/mixin.inl"
1343# include "mixins/term/mixin.inl"
1344# include "mixins/observer/mixin.inl"
1345# include "mixins/query/mixin.inl"
1346# include "mixins/enum/mixin.inl"
1347
1348# ifdef FLECS_MODULE
1349# include "mixins/module/mixin.inl"
1350# endif
1351# ifdef FLECS_PIPELINE
1352# include "mixins/pipeline/mixin.inl"
1353# endif
1354# ifdef FLECS_SYSTEM
1355# include "mixins/system/mixin.inl"
1356# endif
1357# ifdef FLECS_TIMER
1358# include "mixins/timer/mixin.inl"
1359# endif
1360# ifdef FLECS_SCRIPT
1361# include "mixins/script/mixin.inl"
1362# endif
1363# ifdef FLECS_META
1364# include "mixins/meta/world.inl"
1365# endif
1366# ifdef FLECS_JSON
1367# include "mixins/json/world.inl"
1368# endif
1369# ifdef FLECS_APP
1370# include "mixins/app/mixin.inl"
1371# endif
1372# ifdef FLECS_METRICS
1373# include "mixins/metrics/mixin.inl"
1374# endif
1375# ifdef FLECS_ALERTS
1376# include "mixins/alerts/mixin.inl"
1377# endif
1378
1379public:
1380 void init_builtin_components();
1381
1382 world_t *world_;
1383};
1384
1388 struct scoped_world : world {
1389 scoped_world(
1390 flecs::world_t *w,
1391 flecs::entity_t s) : world(w)
1392 {
1393 prev_scope_ = ecs_set_scope(w, s);
1394 }
1395
1396 ~scoped_world() {
1397 ecs_set_scope(world_, prev_scope_);
1398 }
1399
1400 scoped_world(const scoped_world& obj) : world(nullptr) {
1401 prev_scope_ = obj.prev_scope_;
1402 world_ = obj.world_;
1403 flecs_poly_claim(world_);
1404 }
1405
1406 flecs::entity_t prev_scope_;
1407};
1408
1410
1411} // namespace flecs
mixin.inl
App world addon mixin.
mixin.inl
Component mixin.
mixin.inl
Entity world mixin.
mixin.inl
Enum world mixin.
mixin.inl
Event world mixin.
ecs_remove_all
void ecs_remove_all(ecs_world_t *world, ecs_id_t component)
Remove all instances of the specified component.
ecs_set_with
ecs_entity_t ecs_set_with(ecs_world_t *world, ecs_id_t component)
Create new entities with specified component.
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:368
ecs_stage_new
ecs_world_t * ecs_stage_new(ecs_world_t *world)
Create unmanaged stage.
ecs_defer_end
bool ecs_defer_end(ecs_world_t *world)
End block of operations to defer.
ecs_readonly_begin
bool ecs_readonly_begin(ecs_world_t *world, bool multi_threaded)
Begin readonly mode.
ecs_defer_resume
void ecs_defer_resume(ecs_world_t *world)
Resume deferring.
ecs_defer_begin
bool ecs_defer_begin(ecs_world_t *world)
Defer operations until end of frame.
ecs_defer_suspend
void ecs_defer_suspend(ecs_world_t *world)
Suspend deferring but do not flush queue.
ecs_is_deferred
bool ecs_is_deferred(const ecs_world_t *world)
Test if deferring is enabled for current stage.
ecs_stage_free
void ecs_stage_free(ecs_world_t *stage)
Free unmanaged stage.
ecs_merge
void ecs_merge(ecs_world_t *world)
Merge world or stage.
ecs_stage_get_id
int32_t ecs_stage_get_id(const ecs_world_t *world)
Get stage id.
ecs_stage_is_readonly
bool ecs_stage_is_readonly(const ecs_world_t *world)
Test whether the current world is readonly.
ecs_get_stage_count
int32_t ecs_get_stage_count(const ecs_world_t *world)
Get number of configured stages.
ecs_get_stage
ecs_world_t * ecs_get_stage(const ecs_world_t *world, int32_t stage_id)
Get stage-specific world pointer.
ecs_set_stage_count
void ecs_set_stage_count(ecs_world_t *world, int32_t stages)
Configure world to have N stages.
ecs_readonly_end
void ecs_readonly_end(ecs_world_t *world)
End readonly mode.
ecs_get_type_info
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.
ecs_stage_t
struct ecs_stage_t ecs_stage_t
A stage enables modification while iterating and from multiple threads.
Definition flecs.h:412
ecs_entity_t
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:365
ecs_world_t
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
Definition flecs.h:409
ecs_id_t
uint64_t ecs_id_t
Ids are the things that can be added to an entity.
Definition flecs.h:358
flecs::world::component
flecs::component< T > component(Args &&... args) const
Find or register component.
Definition impl.hpp:11
flecs::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.
Definition impl.hpp:190
ecs_delete_with
void ecs_delete_with(ecs_world_t *world, ecs_id_t component)
Delete all entities with the specified component.
ecs_count_id
int32_t ecs_count_id(const ecs_world_t *world, ecs_id_t entity)
Count entities that have the specified id.
ecs_fini_action_t
void(* ecs_fini_action_t)(ecs_world_t *world, void *ctx)
Action callback on world exit.
Definition flecs.h:624
ecs_ctx_free_t
void(* ecs_ctx_free_t)(void *ctx)
Function to cleanup context data.
Definition flecs.h:629
ecs_emplace_id
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.
ecs_modified_id
void ecs_modified_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Signal that a component has been modified.
ecs_strip_generation
ecs_id_t ecs_strip_generation(ecs_entity_t e)
Remove generation from entity id.
ecs_is_valid
bool ecs_is_valid(const ecs_world_t *world, ecs_entity_t e)
Test whether an entity is valid.
ecs_exists
bool ecs_exists(const ecs_world_t *world, ecs_entity_t entity)
Test whether an entity exists.
ecs_is_alive
bool ecs_is_alive(const ecs_world_t *world, ecs_entity_t e)
Test whether an entity is alive.
ecs_set_version
void ecs_set_version(ecs_world_t *world, ecs_entity_t entity)
Override the generation of an entity.
ecs_ftime_t
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition flecs.h:59
ecs_set_lookup_path
ecs_entity_t * ecs_set_lookup_path(ecs_world_t *world, const ecs_entity_t *lookup_path)
Set search path for lookup operations.
ecs_set_scope
ecs_entity_t ecs_set_scope(ecs_world_t *world, ecs_entity_t scope)
Set the current scope.
ecs_atfini
void ecs_atfini(ecs_world_t *world, ecs_fini_action_t action, void *ctx)
Register action to be executed when world is destroyed.
ecs_fini
int ecs_fini(ecs_world_t *world)
Delete a world.
ecs_init
ecs_world_t * ecs_init(void)
Create a new world.
ecs_init_w_args
ecs_world_t * ecs_init_w_args(int argc, char *argv[])
Create a new world with arguments.
ecs_frame_begin
float ecs_frame_begin(ecs_world_t *world, float delta_time)
Begin frame.
ecs_run_post_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.
ecs_should_quit
bool ecs_should_quit(const ecs_world_t *world)
Return whether a quit has been requested.
ecs_quit
void ecs_quit(ecs_world_t *world)
Signal exit This operation signals that the application should quit.
ecs_frame_end
void ecs_frame_end(ecs_world_t *world)
End frame.
ecs_get_binding_ctx
void * ecs_get_binding_ctx(const ecs_world_t *world)
Get the world binding context.
ecs_shrink
void ecs_shrink(ecs_world_t *world)
Free unused memory.
ecs_dim
void ecs_dim(ecs_world_t *world, int32_t entity_count)
Dimension the world for a specified number of entities.
ecs_set_entity_range
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.
ecs_get_world_info
const ecs_world_info_t * ecs_get_world_info(const ecs_world_t *world)
Get world info.
ecs_get_world
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
ecs_set_ctx
void ecs_set_ctx(ecs_world_t *world, void *ctx, ecs_ctx_free_t ctx_free)
Set a world context.
ecs_exclusive_access_begin
void ecs_exclusive_access_begin(ecs_world_t *world, const char *thread_name)
Begin exclusive thread access.
ecs_set_binding_ctx
void ecs_set_binding_ctx(ecs_world_t *world, void *ctx, ecs_ctx_free_t ctx_free)
Set a world binding context.
flecs_poly_is
#define flecs_poly_is(object, type)
Test if pointer is of specified type.
Definition flecs.h:2738
ecs_enable_range_check
bool ecs_enable_range_check(ecs_world_t *world, bool enable)
Enable/disable range limits.
ecs_exclusive_access_end
void ecs_exclusive_access_end(ecs_world_t *world, bool lock_world)
End exclusive thread access.
ecs_get_ctx
void * ecs_get_ctx(const ecs_world_t *world)
Get the world context.
mixin.inl
Id world mixin.
world.inl
JSON world mixin.
world.inl
Meta world mixin.
mixin.inl
Module world mixin.
mixin.inl
Observer world mixin.
mixin.inl
Pipeline world mixin.
mixin.inl
Query world mixin.
mixin.inl
Script world mixin.
ecs_world_info_t::delta_time
float delta_time
Time passed to or computed by ecs_progress()
Definition flecs.h:1465
flecs::_::type
Definition flecs.hpp:39
flecs::entity
Entity.
Definition entity.hpp:30
flecs::scoped_world
Scoped world.
Definition world.hpp:1388
flecs::world
The world.
Definition world.hpp:150
flecs::world::is_stage
bool is_stage() const
Test if is a stage.
Definition world.hpp:434
flecs::world::shrink
void shrink() const
Free unused memory.
Definition world.hpp:1273
flecs::world::delete_with
void delete_with() const
Delete all entities with specified component.
Definition world.hpp:1115
flecs::world::remove_all
void remove_all(id_t the_id) const
Remove all instances of specified id.
Definition world.hpp:1132
flecs::world::delete_with
void delete_with(entity_t second) const
Delete all entities with specified pair.
Definition world.hpp:1127
flecs::world::merge
void merge() const
Merge world or stage.
Definition world.hpp:453
flecs::world::delete_with
void delete_with(id_t the_id) const
Delete all entities with specified id.
Definition world.hpp:1104
flecs::world::get_info
const flecs::world_info_t * get_info() const
Get the world info.
Definition world.hpp:1260
flecs::world::get_scope
flecs::entity get_scope() const
Get current scope.
Definition world.hpp:84
flecs::world::remove
void remove() const
Remove singleton component.
Definition world.hpp:289
flecs::world::lookup
flecs::entity lookup(const char *name, const char *sep="::", const char *root_sep="::", bool recursive=true) const
Lookup entity by name.
Definition world.hpp:93
flecs::world::set
void set(A &&value) const
Set singleton pair.
Definition world.hpp:686
flecs::world::delta_time
ecs_ftime_t delta_time() const
Get delta_time.
Definition world.hpp:1265
flecs::world::quit
void quit() const
Signal application should quit.
Definition world.hpp:257
flecs::world::get_alive
flecs::entity get_alive(flecs::entity_t e) const
Get alive entity for id.
Definition world.hpp:359
flecs::world::type_info
const flecs::type_info_t * type_info()
Return type info.
Definition world.hpp:1323
flecs::world::set_entity_range
void set_entity_range(entity_t min, entity_t max) const
Set entity range.
Definition world.hpp:597
flecs::world::readonly_end
void readonly_end() const
End readonly mode.
Definition world.hpp:331
flecs::world::with
void with(const Func &func) const
All entities created in function are created with pair.
Definition world.hpp:1057
flecs::world::make_alive
flecs::entity make_alive(flecs::entity_t e) const
Definition world.hpp:364
flecs::world::count
int count() const
Count entities matching a component.
Definition world.hpp:1012
flecs::world::id
flecs::id id() const
Get id from a type.
Definition impl.hpp:70
flecs::world::exclusive_access_begin
void exclusive_access_begin(const char *thread_name=nullptr)
Begin exclusive access.
Definition world.hpp:1282
flecs::world::target
flecs::entity target(int32_t index=0) const
Get target for a given pair from a singleton entity.
Definition world.hpp:322
flecs::world::get
const A & get() const
Get singleton pair.
Definition world.hpp:175
flecs::world::defer
void defer(const Func &func) const
Defer all operations called in function.
Definition world.hpp:1168
flecs::world::should_quit
bool should_quit() const
Test if quit() has been called.
Definition world.hpp:269
flecs::world::is_alive
bool is_alive(flecs::entity_t e) const
Check if entity id exists in the world.
Definition world.hpp:1216
flecs::world::c_ptr
world_t * c_ptr() const
Obtain pointer to C world object.
Definition world.hpp:250
flecs::world::try_get
const T * try_get() const
Get singleton component.
Definition world.hpp:141
flecs::world::defer_begin
bool defer_begin() const
Defer operations until end of frame.
Definition world.hpp:350
flecs::world::reset
void reset()
Deletes and recreates the world.
Definition world.hpp:240
flecs::world::set_lookup_path
flecs::entity_t * set_lookup_path(const flecs::entity_t *search_path) const
Set search path.
Definition world.hpp:647
flecs::world::defer_suspend
void defer_suspend() const
Suspend deferring operations.
Definition world.hpp:1183
flecs::world::set
void set(const A &value) const
Set singleton pair.
Definition world.hpp:678
flecs::world::make_owner
void make_owner()
Make current world object owner of the world.
Definition world.hpp:235
flecs::world::is_valid
bool is_valid(flecs::entity_t e) const
Check if entity id is valid.
Definition world.hpp:1227
flecs::world::async_stage
flecs::world async_stage() const
Create asynchronous stage.
Definition world.hpp:490
flecs::world::is_deferred
bool is_deferred() const
Test whether deferring is enabled.
Definition world.hpp:383
flecs::world::get_binding_ctx
void * get_binding_ctx() const
Get world binding context.
Definition world.hpp:574
flecs::world::get_stage_id
int32_t get_stage_id() const
Get current stage id.
Definition world.hpp:424
flecs::world::world
world(const world &obj)
Not allowed to copy a world.
Definition world.hpp:178
flecs::world::scope
void scope(const Func &func) const
Same as scope(parent, func), but with T as parent.
Definition world.hpp:1088
flecs::world::defer_resume
void defer_resume() const
Resume deferring operations.
Definition world.hpp:1196
flecs::world::set_scope
flecs::entity set_scope() const
Same as set_scope but with type.
Definition world.hpp:89
flecs::world::dim
void dim(int32_t entity_count) const
Preallocate memory for number of entities.
Definition world.hpp:585
flecs::world::set_version
void set_version(flecs::entity_t e) const
Set version of entity to provided.
Definition world.hpp:1247
flecs::world::try_get_mut
A * try_get_mut() const
Get mutable singleton pair.
Definition world.hpp:203
flecs::world::set_stage_count
void set_stage_count(int32_t stages) const
Configure world to have N stages.
Definition world.hpp:402
flecs::world::with
void with(id_t with_id, const Func &func) const
All entities created in function are created with id.
Definition world.hpp:1041
flecs::world::get_ctx
void * get_ctx() const
Get world context.
Definition world.hpp:546
flecs::world::type_info
const flecs::type_info_t * type_info(flecs::entity_t r, flecs::entity_t t)
Return type info.
Definition world.hpp:1317
flecs::world::count
int count() const
Count entities matching a pair.
Definition world.hpp:1032
flecs::world::remove_all
void remove_all() const
Remove all instances of specified pair.
Definition world.hpp:1149
flecs::world::defer_end
bool defer_end() const
End block of operations to defer.
Definition world.hpp:368
flecs::world::count
int count(flecs::entity_t first, flecs::entity_t second) const
Count entities matching a pair.
Definition world.hpp:1003
flecs::world::remove_all
void remove_all(entity_t second) const
Remove all instances of specified pair.
Definition world.hpp:1155
flecs::world::world
world(world_t *w)
Create world from C world.
Definition world.hpp:169
flecs::world::children
void children(Func &&f) const
Iterate entities in root of world Accepts a callback with the following signature:
Definition world.hpp:312
flecs::world::get_world
flecs::world get_world() const
Get actual world.
Definition world.hpp:502
flecs::world::scope
void scope(id_t parent, const Func &func) const
All entities created in function are created in scope.
Definition world.hpp:1079
flecs::world::with
void with(const Func &func) const
All entities created in function are created with type.
Definition world.hpp:1050
flecs::world::remove_all
void remove_all(entity_t first, entity_t second) const
Remove all instances of specified pair.
Definition world.hpp:1137
flecs::world::modified
void modified() const
Mark singleton component as modified.
Definition world.hpp:107
flecs::world::enable_range_check
void enable_range_check(bool enabled=true) const
Enforce that operations cannot modify entities outside of range.
Definition world.hpp:611
flecs::world::count
int count(flecs::entity_t second) const
Count entities matching a pair.
Definition world.hpp:1022
flecs::world::id_if_registered
flecs::id_t id_if_registered()
Return component id if it has been registered.
Definition world.hpp:1302
flecs::world::frame_begin
ecs_ftime_t frame_begin(float delta_time=0) const
Begin frame.
Definition world.hpp:294
flecs::world::use
flecs::entity use(const char *alias=nullptr) const
Create alias for component.
Definition world.hpp:51
flecs::world::is_readonly
bool is_readonly() const
Test whether the current world object is readonly.
Definition world.hpp:518
flecs::world::add
void add() const
Add singleton component.
Definition world.hpp:266
flecs::world::ensure
T & ensure() const
Ensure singleton component.
Definition world.hpp:100
flecs::world::readonly_begin
bool readonly_begin(bool multi_threaded=false) const
Begin readonly mode.
Definition world.hpp:321
flecs::world::get_mut
A & get_mut() const
Get mutable singleton pair.
Definition world.hpp:231
flecs::world::set
void set(const T &value) const
Set singleton component.
Definition world.hpp:662
flecs::world::with
void with(id_t first, id_t second, const Func &func) const
All entities created in function are created with pair.
Definition world.hpp:1071
flecs::world::get_stage
flecs::world get_stage(int32_t stage_id) const
Get stage-specific world pointer.
Definition world.hpp:471
flecs::world::set
void set(T &&value) const
Set singleton component.
Definition world.hpp:669
flecs::world::has
bool has() const
Test if world has singleton component.
Definition world.hpp:243
flecs::world::exists
bool exists(flecs::entity_t e) const
Check if entity id exists in the world.
Definition world.hpp:1206
flecs::world::world
world()
Create world.
Definition world.hpp:153
flecs::world::frame_end
void frame_end() const
End frame.
Definition world.hpp:307
flecs::world::set_binding_ctx
void set_binding_ctx(void *ctx, ecs_ctx_free_t ctx_free=nullptr) const
Set world binding context.
Definition world.hpp:561
flecs::world::atfini
void atfini(ecs_fini_action_t action, void *ctx=nullptr) const
Register action to be executed when world is destroyed.
Definition world.hpp:263
flecs::world::type_info
const flecs::type_info_t * type_info(flecs::id_t component)
Return type info.
Definition world.hpp:1312
flecs::world::get_stage_count
int32_t get_stage_count() const
Get number of configured stages.
Definition world.hpp:414
flecs::world::delete_with
void delete_with(entity_t first, entity_t second) const
Delete all entities with specified pair.
Definition world.hpp:1109
flecs::world::delete_with
void delete_with() const
Delete all entities with specified pair.
Definition world.hpp:1121
flecs::world::singleton
flecs::entity singleton() const
Get singleton entity for type.
Definition world.hpp:317
flecs::world::exclusive_access_end
void exclusive_access_end(bool lock_world=false)
End exclusive access.
Definition world.hpp:1291
flecs::world::with
void with(id_t second, const Func &func) const
All entities created in function are created with pair.
Definition world.hpp:1064
flecs::world::set_ctx
void set_ctx(void *ctx, ecs_ctx_free_t ctx_free=nullptr) const
Set world context.
Definition world.hpp:533
flecs::world::world
world(int argc, char *argv[])
Create world with command line arguments.
Definition world.hpp:162
flecs::world::count
int count(flecs::id_t component_id) const
Count entities matching a component.
Definition world.hpp:994
flecs::world::type_info
const flecs::type_info_t * type_info()
Return type info.
Definition world.hpp:1335
flecs::world::type_info
const flecs::type_info_t * type_info(flecs::entity_t t)
Return type info.
Definition world.hpp:1329
flecs::world::remove_all
void remove_all() const
Remove all instances of specified component.
Definition world.hpp:1143
flecs::world::get_ref
ref< T > get_ref() const
Get ref singleton component.
Definition world.hpp:125
mixin.inl
System module world mixin.
mixin.inl
Term world mixin.
mixin.inl
Timer module mixin.
flecs::strip_generation
flecs::id_t strip_generation(flecs::entity_t e)
Return id without generation.
Definition world.hpp:126
flecs::get_generation
uint32_t get_generation(flecs::entity_t e)
Return entity generation.
Definition world.hpp:132