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");
19 ecs_cpp_get_mut_t res = ecs_cpp_set(world, entity,
id, &value,
sizeof(T));
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);
25 dst = FLECS_MOV(value);
28 if (res.call_modified) {
35inline void set(world_t *world, flecs::entity_t entity,
const T& value, flecs::id_t
id) {
36 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
37 "operation invalid for empty type");
39 ecs_cpp_get_mut_t res = ecs_cpp_set(world, entity,
id, &value,
sizeof(T));
41 T& dst = *
static_cast<remove_reference_t<T>*
>(res.ptr);
44 if (res.call_modified) {
50template <
typename T,
typename A>
51inline void set(world_t *world, entity_t entity, A&& value) {
52 id_t
id = _::type<T>::id(world);
53 flecs::set(world, entity, FLECS_FWD(value),
id);
57template <
typename T,
typename A>
58inline void set(world_t *world, entity_t entity,
const A& value) {
59 id_t
id = _::type<T>::id(world);
60 flecs::set(world, entity, value,
id);
65inline void assign(world_t *world, flecs::entity_t entity, T&& value, flecs::id_t
id) {
66 ecs_assert(_::type<remove_reference_t<T>>::size() != 0,
67 ECS_INVALID_PARAMETER,
"operation invalid for empty type");
69 ecs_cpp_get_mut_t res = ecs_cpp_assign(
70 world, entity,
id, &value,
sizeof(T));
72 T& dst = *
static_cast<remove_reference_t<T>*
>(res.ptr);
73 if constexpr (std::is_copy_assignable_v<T>) {
74 dst = FLECS_FWD(value);
76 dst = FLECS_MOV(value);
79 if (res.call_modified) {
86inline void assign(world_t *world, flecs::entity_t entity,
const T& value, flecs::id_t
id) {
87 ecs_assert(_::type<remove_reference_t<T>>::size() != 0,
88 ECS_INVALID_PARAMETER,
"operation invalid for empty type");
90 ecs_cpp_get_mut_t res = ecs_cpp_assign(
91 world, entity,
id, &value,
sizeof(T));
93 T& dst = *
static_cast<remove_reference_t<T>*
>(res.ptr);
96 if (res.call_modified) {
102template <
typename T,
typename A>
103inline void assign(world_t *world, entity_t entity, A&& value) {
104 id_t
id = _::type<T>::id(world);
105 flecs::assign(world, entity, FLECS_FWD(value),
id);
109template <
typename T,
typename A>
110inline void assign(world_t *world, entity_t entity,
const A& value) {
111 id_t
id = _::type<T>::id(world);
112 flecs::assign(world, entity, value,
id);
117template <
typename T,
typename ... Args, if_t<
118 std::is_constructible<actual_type_t<T>, Args...>::value ||
119 std::is_default_constructible<actual_type_t<T>>::value > = 0>
120inline void emplace(world_t *world, flecs::entity_t entity, flecs::id_t
id, Args&&... args) {
121 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
122 "operation invalid for empty type");
123 T& dst = *
static_cast<T*
>(
ecs_emplace_id(world, entity,
id,
sizeof(T),
nullptr));
125 FLECS_PLACEMENT_NEW(&dst, T{FLECS_FWD(args)...});
141 return ECS_GENERATION(e);
163 init_builtin_components();
170 explicit world(
int argc,
char *argv[])
172 init_builtin_components();
187 this->world_ = obj.world_;
188 flecs_poly_claim(this->world_);
193 this->world_ = obj.world_;
194 flecs_poly_claim(this->world_);
200 obj.world_ =
nullptr;
206 obj.world_ =
nullptr;
214 if (!flecs_poly_release(world_)) {
221 flecs_poly_claim(world_);
234 operator world_t*()
const {
return world_; }
244 flecs_poly_release(world_);
250 ecs_assert(flecs_poly_refcount(world_) == 1, ECS_INVALID_OPERATION,
251 "reset would invalidate other handles");
446 ECS_INVALID_PARAMETER,
447 "flecs::world instance contains invalid reference to world or stage");
500 flecs_poly_release(as);
513 world_ ?
const_cast<flecs::world_t*
>(
ecs_get_world(world_)) :
nullptr);
593 void dim(int32_t entity_count)
const {
647 template <
typename T>
665 flecs::entity lookup(
const char *name,
const char *sep =
"::",
const char *root_sep =
"::",
bool recursive =
true)
const;
669 template <typename T, if_t< !is_callable<T>::value > = 0>
670 void set(
const T& value)
const {
676 template <typename T, if_t< !is_callable<T>::value > = 0>
677 void set(T&& value)
const {
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(
const A& value)
const {
692 template <
typename First,
typename Second,
typename P = flecs::pair<First, Second>,
693 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
694 void set(A&& value)
const {
700 template <
typename First,
typename Second>
701 void set(Second second,
const First& value)
const;
705 template <
typename First,
typename Second>
706 void set(Second second, First&& value)
const;
710 template <typename Func, if_t< is_callable<Func>::value > = 0 >
711 void set(
const Func& func)
const;
713 template <
typename T,
typename ... Args>
714 void emplace(Args&&... args)
const {
716 flecs::emplace<T>(world_, component_id, component_id, FLECS_FWD(args)...);
722 template <
typename T>
728 template <
typename T>
733 template <
typename T>
741 const void*
try_get(flecs::id_t
id)
const;
745 const void*
try_get(flecs::entity_t r, flecs::entity_t t)
const;
749 template <
typename T>
754 template <
typename First,
typename Second,
typename P = flecs::pair<First, Second>,
755 typename A = actual_type_t<P>>
760 template <
typename First,
typename Second>
761 const First*
try_get(Second second)
const;
768 const void* get(flecs::id_t
id)
const;
772 const void* get(flecs::entity_t r, flecs::entity_t t)
const;
774 template <
typename T>
775 const T& get()
const;
779 template <
typename First,
typename Second,
typename P = flecs::pair<First, Second>,
780 typename A = actual_type_t<P>>
781 const A& get()
const;
785 template <
typename First,
typename Second>
786 const First& get(Second second)
const;
790 template <typename Func, if_t< is_callable<Func>::value > = 0 >
791 void get(
const Func& func)
const;
798 void* try_get_mut(flecs::id_t
id)
const;
802 void* try_get_mut(flecs::entity_t r, flecs::entity_t t)
const;
804 template <
typename T>
805 T* try_get_mut()
const;
809 template <
typename First,
typename Second,
typename P = flecs::pair<First, Second>,
810 typename A = actual_type_t<P>>
811 A* try_get_mut()
const;
815 template <
typename First,
typename Second>
816 First* try_get_mut(Second second)
const;
823 void* get_mut(flecs::id_t
id)
const;
827 void* get_mut(flecs::entity_t r, flecs::entity_t t)
const;
829 template <
typename T>
834 template <
typename First,
typename Second,
typename P = flecs::pair<First, Second>,
835 typename A = actual_type_t<P>>
840 template <
typename First,
typename Second>
841 First& get_mut(Second second)
const;
849 template <
typename T>
858 template <
typename First,
typename Second>
867 template <
typename First>
868 bool has(flecs::id_t second)
const;
876 bool has(flecs::id_t first, flecs::id_t second)
const;
884 template <typename E, if_t< is_enum<E>::value > = 0>
885 bool has(E value)
const;
889 template <
typename T>
897 template <
typename First,
typename Second>
905 template <
typename First>
906 void add(flecs::entity_t second)
const;
913 void add(flecs::entity_t first, flecs::entity_t second)
const;
920 template <typename E, if_t< is_enum<E>::value > = 0>
921 void add(E value)
const;
925 template <
typename T>
933 template <
typename First,
typename Second>
941 template <
typename First>
942 void remove(flecs::entity_t second)
const;
949 void remove(flecs::entity_t first, flecs::entity_t second)
const;
958 template <
typename Func>
963 template <
typename T>
974 template<
typename First>
1004 template <
typename T>
1025 int count(flecs::id_t component_id)
const {
1034 int count(flecs::entity_t first, flecs::entity_t second)
const {
1042 template <
typename T>
1052 template <
typename First>
1053 int count(flecs::entity_t second)
const {
1062 template <
typename First,
typename Second>
1071 template <
typename Func>
1072 void with(id_t with_id,
const Func& func)
const {
1080 template <
typename T,
typename Func>
1081 void with(
const Func& func)
const {
1087 template <
typename First,
typename Second,
typename Func>
1088 void with(
const Func& func)
const {
1094 template <
typename First,
typename Func>
1095 void with(id_t second,
const Func& func)
const {
1101 template <
typename Func>
1102 void with(id_t first, id_t second,
const Func& func)
const {
1103 with(ecs_pair(first, second), func);
1109 template <
typename Func>
1110 void scope(id_t parent,
const Func& func)
const {
1118 template <
typename T,
typename Func>
1121 scope(parent, func);
1129 template <
typename T>
1145 template <
typename T>
1151 template <
typename First,
typename Second>
1157 template <
typename First>
1173 template <
typename T>
1179 template <
typename First,
typename Second>
1185 template <
typename First>
1198 template <
typename Func>
1332 template <
typename T>
1353 template <
typename T>
1359 template <
typename R>
1365 template <
typename R,
typename T>
1382# ifdef FLECS_PIPELINE
1403# ifdef FLECS_METRICS
1404# include "mixins/metrics/mixin.inl"
1407# include "mixins/alerts/mixin.inl"
1411 void init_builtin_components();
1422 flecs::entity_t s) :
world(w)
1432 prev_scope_ = obj.prev_scope_;
1433 world_ = obj.world_;
1434 flecs_poly_claim(world_);
1437 flecs::entity_t prev_scope_;
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.
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.
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.
ecs_id_t ecs_entity_t
An entity identifier.
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
uint64_t ecs_id_t
Ids are the things that can be added to an entity.
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.
void(* ecs_ctx_free_t)(void *ctx)
Function to cleanup context data.
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.
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.
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.
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.
Type that contains component information (passed to ctors/dtors/...)
Type that contains information about the world.
float delta_time
Time passed to or computed by ecs_progress()
Class that wraps around a flecs::id_t.
bool is_stage() const
Test if is a stage.
void shrink() const
Free unused memory.
void delete_with() const
Delete all entities with specified component.
void remove_all(id_t the_id) const
Remove all instances of specified id.
void delete_with(entity_t second) const
Delete all entities with specified pair.
void merge() const
Merge world or stage.
void delete_with(id_t the_id) const
Delete all entities with specified id.
const flecs::world_info_t * get_info() const
Get the world info.
flecs::entity get_scope() const
Get current scope.
void remove() const
Remove singleton component.
flecs::entity lookup(const char *name, const char *sep="::", const char *root_sep="::", bool recursive=true) const
Lookup entity by name.
void set(A &&value) const
Set singleton pair.
ecs_ftime_t delta_time() const
Get delta_time.
void quit() const
Signal application should quit.
flecs::entity get_alive(flecs::entity_t e) const
Get alive entity for id.
const flecs::type_info_t * type_info()
Return type info.
void set_entity_range(entity_t min, entity_t max) const
Set entity range.
void readonly_end() const
End readonly mode.
void with(const Func &func) const
All entities created in function are created with pair.
flecs::entity make_alive(flecs::entity_t e) const
int count() const
Count entities matching a component.
void exclusive_access_begin(const char *thread_name=nullptr)
Begin exclusive access.
flecs::entity target(int32_t index=0) const
Get target for a given pair from a singleton entity.
void defer(const Func &func) const
Defer all operations called in function.
bool should_quit() const
Test if quit() has been called.
bool is_alive(flecs::entity_t e) const
Check if entity id exists in the world.
world_t * c_ptr() const
Obtain pointer to C world object.
const T * try_get() const
Get singleton component.
bool defer_begin() const
Defer operations until end of frame.
void reset()
Deletes and recreates the world.
flecs::entity_t * set_lookup_path(const flecs::entity_t *search_path) const
Set search path.
void defer_suspend() const
Suspend deferring operations.
void set(const A &value) const
Set singleton pair.
void make_owner()
Make current world object owner of the world.
bool is_valid(flecs::entity_t e) const
Check if entity id is valid.
flecs::world async_stage() const
Create asynchronous stage.
bool is_deferred() const
Test whether deferring is enabled.
void * get_binding_ctx() const
Get world binding context.
int32_t get_stage_id() const
Get current stage id.
world(const world &obj)
Not allowed to copy a world.
void scope(const Func &func) const
Same as scope(parent, func), but with T as parent.
void defer_resume() const
Resume deferring operations.
flecs::entity set_scope() const
Same as set_scope but with type.
void dim(int32_t entity_count) const
Preallocate memory for number of entities.
void set_version(flecs::entity_t e) const
Set version of entity to provided.
void set_stage_count(int32_t stages) const
Configure world to have N stages.
void with(id_t with_id, const Func &func) const
All entities created in function are created with id.
void * get_ctx() const
Get world context.
const flecs::type_info_t * type_info(flecs::entity_t r, flecs::entity_t t)
Return type info.
int count() const
Count entities matching a pair.
void remove_all() const
Remove all instances of specified pair.
bool defer_end() const
End block of operations to defer.
int count(flecs::entity_t first, flecs::entity_t second) const
Count entities matching a pair.
void remove_all(entity_t second) const
Remove all instances of specified pair.
world(world_t *w)
Create world from C world.
void children(Func &&f) const
Iterate entities in root of world Accepts a callback with the following signature:
flecs::world get_world() const
Get actual world.
void scope(id_t parent, const Func &func) const
All entities created in function are created in scope.
void with(const Func &func) const
All entities created in function are created with type.
void remove_all(entity_t first, entity_t second) const
Remove all instances of specified pair.
void modified() const
Mark singleton component as modified.
void enable_range_check(bool enabled=true) const
Enforce that operations cannot modify entities outside of range.
int count(flecs::entity_t second) const
Count entities matching a pair.
flecs::id_t id_if_registered()
Return component id if it has been registered.
ecs_ftime_t frame_begin(float delta_time=0) const
Begin frame.
flecs::entity use(const char *alias=nullptr) const
Create alias for component.
bool is_readonly() const
Test whether the current world object is readonly.
void add() const
Add singleton component.
T & ensure() const
Ensure singleton component.
bool readonly_begin(bool multi_threaded=false) const
Begin readonly mode.
void set(const T &value) const
Set singleton component.
void with(id_t first, id_t second, const Func &func) const
All entities created in function are created with pair.
flecs::world get_stage(int32_t stage_id) const
Get stage-specific world pointer.
void set(T &&value) const
Set singleton component.
bool has() const
Test if world has singleton component.
bool exists(flecs::entity_t e) const
Check if entity id exists in the world.
void frame_end() const
End frame.
void set_binding_ctx(void *ctx, ecs_ctx_free_t ctx_free=nullptr) const
Set world binding context.
void atfini(ecs_fini_action_t action, void *ctx=nullptr) const
Register action to be executed when world is destroyed.
const flecs::type_info_t * type_info(flecs::id_t component)
Return type info.
int32_t get_stage_count() const
Get number of configured stages.
void delete_with(entity_t first, entity_t second) const
Delete all entities with specified pair.
void delete_with() const
Delete all entities with specified pair.
flecs::entity singleton() const
Get singleton entity for type.
void exclusive_access_end(bool lock_world=false)
End exclusive access.
void with(id_t second, const Func &func) const
All entities created in function are created with pair.
void set_ctx(void *ctx, ecs_ctx_free_t ctx_free=nullptr) const
Set world context.
world(int argc, char *argv[])
Create world with command line arguments.
int count(flecs::id_t component_id) const
Count entities matching a component.
const flecs::type_info_t * type_info()
Return type info.
const flecs::type_info_t * type_info(flecs::entity_t t)
Return type info.
void remove_all() const
Remove all instances of specified component.
ref< T > get_ref() const
Get ref singleton component.
System module world mixin.
flecs::id_t strip_generation(flecs::entity_t e)
Return id without generation.
uint32_t get_generation(flecs::entity_t e)
Return entity generation.