26inline flecs::entity world::const_var(
const char *name,
const T& value,
33 desc.
value =
const_cast<T*
>(&value);
38inline flecs::entity world::mut_var(
const char *name,
const T& value,
45 desc.
value =
const_cast<T*
>(&value);
50inline int world::set_mut_var(
const char *name,
const T& value)
const {
52 world_, 0, name,
"::",
"::",
false);
58 _::type<T>::id(
world_), ECS_SIZEOF(T), &value);
67 world, 0, name,
"::",
"::",
false);
69 ecs_warn(
"unresolved const variable '%s', returning default", name);
74 if (value.ptr ==
nullptr) {
75 ecs_warn(
"entity '%s' is not a const variable, returning default",
83 template <
bool Mutable,
typename T>
84 inline std::conditional_t<std::is_arithmetic_v<T>, T,
const T&>
85 get_script_var(
flecs::world_t *world,
const char *name,
const T& default_value) {
87 world, 0, name,
"::",
"::",
false);
88 const char *kind = Mutable ?
"mut" :
"const";
90 ecs_warn(
"unresolved %s variable '%s', returning default", kind, name);
95 if constexpr (Mutable) {
101 ecs_warn(
"entity '%s' is not a %s variable, returning default", name, kind);
102 return default_value;
106 if (
type == value.type) {
107 return *
static_cast<const T*
>(value.ptr);
110 if constexpr (std::is_arithmetic_v<T> && !std::is_same_v<T, bool>) {
112 if constexpr (std::is_same_v<T, char>) {
114 }
else if constexpr (std::is_floating_point_v<T>) {
116 }
else if constexpr (std::is_signed_v<T>) {
124 ecs_warn(
"cannot cast %s variable '%s' from type '%s' to '%s', "
125 "returning default", kind, name, var_type, requested_type);
126 ecs_os_free(requested_type);
127 ecs_os_free(var_type);
128 return default_value;
132 inline const char* get_const_string(
135 if (value.type == flecs::String) {
136 return *(
static_cast<const char**
>(value.ptr));
144 ecs_warn(
"cannot cast const variable '%s' from type '%s' to "
145 "'flecs::string', returning default", name, var_type);
146 ecs_os_free(var_type);
147 return default_value;
156 return _::get_script_var<false>(
world_, name, default_value);
161 out = _::get_script_var<false>(
world_, name, default_value);
166 return _::get_script_var<true>(
world_, name, default_value);
171 out = _::get_script_var<true>(
world_, name, default_value);
178 const char*
const& default_value)
const
182 return default_value;
185 return flecs::_::get_const_string(
world_, name, value, default_value);
192 const char*
const& default_value)
const
200 out = flecs::_::get_const_string(
world_, name, value, default_value);
207inline void init(flecs::world& world) {
208 world.
component<Script>(
"flecs::script::Script");
struct ecs_value_t ecs_value_t
Value of a dynamic type.
#define ecs_warn(...)
Warning macro.
FLECS_API ecs_value_t ecs_mut_var_get(const ecs_world_t *world, ecs_entity_t var)
Return the value for a mut variable.
FLECS_API int ecs_mut_var_set_w_type(ecs_world_t *world, const char *name, ecs_entity_t type, ecs_size_t size, const void *value)
Set the value of a mut variable.
FLECS_API ecs_entity_t ecs_function_init(ecs_world_t *world, const ecs_function_desc_t *desc)
Create new function.
FLECS_API ecs_entity_t ecs_mut_var_init(ecs_world_t *world, ecs_mut_var_desc_t *desc)
Create a mut variable that can be accessed by scripts.
struct ecs_mut_var_desc_t ecs_mut_var_desc_t
Used with ecs_mut_var_init().
FLECS_API ecs_entity_t ecs_script_init(ecs_world_t *world, const ecs_script_desc_t *desc)
Load managed script.
FLECS_API ecs_value_t ecs_const_var_get(const ecs_world_t *world, ecs_entity_t var)
Return the value for a const variable.
FLECS_API ecs_entity_t ecs_method_init(ecs_world_t *world, const ecs_function_desc_t *desc)
Create new method.
FLECS_API ecs_entity_t ecs_const_var_init(ecs_world_t *world, ecs_const_var_desc_t *desc)
Create a const variable that can be accessed by scripts.
ecs_id_t ecs_entity_t
An entity identifier.
T get_const_var(const char *name, const T &default_value={}) const
Get the value of an exported script variable.
T get_mut_var(const char *name, const T &default_value={}) const
Get the value of an exported script mut variable.
flecs::component< T > component(Args &&... args) const
Find or register a component.
ecs_entity_t entity_t
Entity type.
ecs_world_t world_t
World type.
#define ecs_get_path(world, child)
Get path from root.
ecs_entity_t ecs_get_scope(const ecs_world_t *world)
Get the current scope.
ecs_entity_t ecs_lookup_path_w_sep(const ecs_world_t *world, ecs_entity_t parent, const char *path, const char *sep, const char *prefix, bool recursive)
Look up an entity from a path.
Used with ecs_const_var_init().
ecs_entity_t parent
Variable parent (namespace).
const char * name
Variable name.
ecs_entity_t type
Variable type.
void * value
Pointer to value of variable.
void * value
Pointer to value of variable.
ecs_entity_t type
Variable type.
const char * name
Variable name.
ecs_entity_t parent
Variable parent (namespace).
void * ptr
Pointer to value.
flecs::entity run() const
Run the script and return the script entity.
world_t * world_
Pointer to the underlying C world.
flecs::entity entity(Args &&... args) const
Create an entity.