Skip to content
Flecs v4.2
mixin.inl
Go to the documentation of this file.
1/**
2 * @file addons/cpp/mixins/script/mixin.inl
3 * @brief Script world mixin.
4 */
5
6/**
7 * @defgroup cpp_addons_script Script
8 * @ingroup cpp_addons
9 * Data definition format for loading entity data.
10 *
11 * @{
12 */
13
14/** Run a script.
15 * @see ecs_script_run()
16 */
17int script_run(const char *name, const char *str) const {
18 return ecs_script_run(world_, name, str, nullptr);
19}
20
21/** Run a script from a file.
22 * @see ecs_script_run_file()
23 */
24int script_run_file(const char *filename) const {
25 return ecs_script_run_file(world_, filename);
26}
27
28/** Build a script.
29 * @see ecs_script_init()
30 */
31script_builder script(const char *name = nullptr) const {
32 return script_builder(world_, name);
33}
34
35parsed_script script_parse(const char *name, const char *code,
36 const ecs_script_eval_desc_t *desc = nullptr,
37 ecs_script_eval_result_t *result = nullptr) const
38{
39 return parsed_script(ecs_script_parse(world_, name, code, desc, result));
40}
41
42int script_update(flecs::entity_t script, const char *code,
43 flecs::entity_t instance = 0) const
44{
45 return ecs_script_update(world_, script, instance, code);
46}
47
48/** Reload a script from new code, or from its file when code is nullptr.
49 * @see ecs_script_reload()
50 */
51int script_reload(flecs::entity_t script, const char *code = nullptr) const {
52 return ecs_script_reload(world_, script, code);
53}
54
55function_builder function(const char *name) const {
56 return function_builder(world_, name);
57}
58
59function_builder method(flecs::entity_t type, const char *name) const {
60 return function_builder(world_, name, type, true);
61}
62
63template <typename T>
64function_builder method(const char *name) const {
65 return method(_::type<T>::id(world_), name);
66}
67
68template <typename T>
69flecs::entity const_var(const char *name, const T& value,
70 flecs::entity_t parent = 0) const;
71
72template <typename T>
73flecs::entity mut_var(const char *name, const T& value,
74 flecs::entity_t parent = 0) const;
75
76template <typename T>
77int set_mut_var(const char *name, const T& value) const;
78
79/** Convert a value to a string. */
80flecs::string to_expr(flecs::entity_t tid, const void* value) {
81 char *expr = ecs_ptr_to_expr(world_, tid, value);
82 return flecs::string(expr);
83}
84
85/** Convert a value to a string. */
86template <typename T>
87flecs::string to_expr(const T* value) {
88 flecs::entity_t tid = _::type<T>::id(world_);
89 return to_expr(tid, value);
90}
91
92/** Get the value of an exported script variable.
93 * This operation will panic if no const var with the provided name was found,
94 * or if the type of the variable cannot be converted to the provided type.
95 *
96 * An exported variable can be created in a script like this:
97 *
98 * @code
99 * export const x: f64 = 10
100 * @endcode
101 *
102 * See the Flecs script manual for more details.
103 *
104 * @tparam T The type of the value to obtain.
105 * @param name The name of the exported variable.
106 * @param default_value Optional default value. Returned when const var lookup failed.
107 * @return The value of the variable.
108 */
109template <typename T>
110T get_const_var(const char *name, const T& default_value = {}) const;
111
112/** Get the value of an exported script variable.
113 * This operation will panic if no const var with the provided name was found,
114 * or if the type of the variable cannot be converted to the provided type.
115 *
116 * An exported variable can be created in a script like this:
117 *
118 * @code
119 * export const x: f64 = 10
120 * @endcode
121 *
122 * See the Flecs script manual for more details.
123 *
124 * @tparam T The type of the value to obtain.
125 * @param name The name of the exported variable.
126 * @param out Optional pointer to out variable. Can be used to automatically deduce T.
127 * @param default_value Optional default value. Returned when const var lookup failed.
128 */
129template <typename T>
130void get_const_var(const char *name, T& out, const T& default_value = {}) const;
131
132/** Get the value of an exported script mut variable.
133 * Unlike a const variable, the value of a mut variable is never folded into
134 * expressions that use it.
135 *
136 * An exported mut variable can be created in a script like this:
137 *
138 * @code
139 * export mut x: f64 = 10
140 * @endcode
141 *
142 * See the Flecs script manual for more details.
143 *
144 * @tparam T The type of the value to obtain.
145 * @param name The name of the exported variable.
146 * @param default_value Optional default value. Returned when mut var lookup failed.
147 * @return The value of the variable.
148 */
149template <typename T>
150T get_mut_var(const char *name, const T& default_value = {}) const;
151
152/** Get the value of an exported script mut variable.
153 * Unlike a const variable, the value of a mut variable is never folded into
154 * expressions that use it.
155 *
156 * An exported mut variable can be created in a script like this:
157 *
158 * @code
159 * export mut x: f64 = 10
160 * @endcode
161 *
162 * See the Flecs script manual for more details.
163 *
164 * @tparam T The type of the value to obtain.
165 * @param name The name of the exported variable.
166 * @param out Optional pointer to out variable. Can be used to automatically deduce T.
167 * @param default_value Optional default value. Returned when mut var lookup failed.
168 */
169template <typename T>
170void get_mut_var(const char *name, T& out, const T& default_value = {}) const;
171
172
173/** @} */
FLECS_API int ecs_script_reload(ecs_world_t *world, ecs_entity_t script, const char *code)
Reload script from new code or from the file it was loaded from.
FLECS_API char * ecs_ptr_to_expr(const ecs_world_t *world, ecs_entity_t type, const void *data)
Serialize value into expression string.
FLECS_API int ecs_script_update(ecs_world_t *world, ecs_entity_t script, ecs_entity_t instance, const char *code)
Update script with new code.
FLECS_API int ecs_script_run(ecs_world_t *world, const char *name, const char *code, ecs_script_eval_result_t *result)
Parse script.
FLECS_API ecs_script_t * ecs_script_parse(ecs_world_t *world, const char *name, const char *code, const ecs_script_eval_desc_t *desc, ecs_script_eval_result_t *result)
Parse script.
FLECS_API int ecs_script_run_file(ecs_world_t *world, const char *filename)
Parse script file.
int script_run(const char *name, const char *str) const
Run a script.
Definition mixin.inl:17
int script_reload(flecs::entity_t script, const char *code=nullptr) const
Reload a script from new code, or from its file when code is nullptr.
Definition mixin.inl:51
int script_run_file(const char *filename) const
Run a script from a file.
Definition mixin.inl:24
flecs::string to_expr(flecs::entity_t tid, const void *value)
Convert a value to a string.
Definition mixin.inl:80
T get_mut_var(const char *name, const T &default_value={}) const
Get the value of an exported script mut variable.
T get_const_var(const char *name, const T &default_value={}) const
Get the value of an exported script variable.
script_builder script(const char *name=nullptr) const
Build a script.
Definition mixin.inl:31
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
Used with ecs_script_parse() and ecs_script_eval().
Definition script.h:186
Used to capture error output from script evaluation.
Definition script.h:219
Entity.
Definition entity.hpp:30
Owned string wrapper.
Definition string.hpp:15