Skip to content
Flecs v4.1
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
48function_builder function(const char *name) const {
49 return function_builder(world_, name);
50}
51
52function_builder method(flecs::entity_t type, const char *name) const {
53 return function_builder(world_, name, type, true);
54}
55
56template <typename T>
57function_builder method(const char *name) const {
58 return method(_::type<T>::id(world_), name);
59}
60
61template <typename T>
62flecs::entity const_var(const char *name, const T& value,
63 flecs::entity_t parent = 0) const;
64
65template <typename T>
66flecs::entity mut_var(const char *name, const T& value,
67 flecs::entity_t parent = 0) const;
68
69template <typename T>
70int set_mut_var(const char *name, const T& value) const;
71
72/** Convert a value to a string. */
73flecs::string to_expr(flecs::entity_t tid, const void* value) {
74 char *expr = ecs_ptr_to_expr(world_, tid, value);
75 return flecs::string(expr);
76}
77
78/** Convert a value to a string. */
79template <typename T>
80flecs::string to_expr(const T* value) {
81 flecs::entity_t tid = _::type<T>::id(world_);
82 return to_expr(tid, value);
83}
84
85/** Get the value of an exported script variable.
86 * This operation will panic if no const var with the provided name was found,
87 * or if the type of the variable cannot be converted to the provided type.
88 *
89 * An exported variable can be created in a script like this:
90 *
91 * @code
92 * export const x: f64 = 10
93 * @endcode
94 *
95 * See the Flecs script manual for more details.
96 *
97 * @tparam T The type of the value to obtain.
98 * @param name The name of the exported variable.
99 * @param default_value Optional default value. Returned when const var lookup failed.
100 * @return The value of the variable.
101 */
102template <typename T>
103T get_const_var(const char *name, const T& default_value = {}) const;
104
105/** Get the value of an exported script variable.
106 * This operation will panic if no const var with the provided name was found,
107 * or if the type of the variable cannot be converted to the provided type.
108 *
109 * An exported variable can be created in a script like this:
110 *
111 * @code
112 * export const x: f64 = 10
113 * @endcode
114 *
115 * See the Flecs script manual for more details.
116 *
117 * @tparam T The type of the value to obtain.
118 * @param name The name of the exported variable.
119 * @param out Optional pointer to out variable. Can be used to automatically deduce T.
120 * @param default_value Optional default value. Returned when const var lookup failed.
121 */
122template <typename T>
123void get_const_var(const char *name, T& out, const T& default_value = {}) const;
124
125/** Get the value of an exported script mut variable.
126 * Unlike a const variable, the value of a mut variable is never folded into
127 * expressions that use it.
128 *
129 * An exported mut variable can be created in a script like this:
130 *
131 * @code
132 * export mut x: f64 = 10
133 * @endcode
134 *
135 * See the Flecs script manual for more details.
136 *
137 * @tparam T The type of the value to obtain.
138 * @param name The name of the exported variable.
139 * @param default_value Optional default value. Returned when mut var lookup failed.
140 * @return The value of the variable.
141 */
142template <typename T>
143T get_mut_var(const char *name, const T& default_value = {}) const;
144
145/** Get the value of an exported script mut variable.
146 * Unlike a const variable, the value of a mut variable is never folded into
147 * expressions that use it.
148 *
149 * An exported mut variable can be created in a script like this:
150 *
151 * @code
152 * export mut x: f64 = 10
153 * @endcode
154 *
155 * See the Flecs script manual for more details.
156 *
157 * @tparam T The type of the value to obtain.
158 * @param name The name of the exported variable.
159 * @param out Optional pointer to out variable. Can be used to automatically deduce T.
160 * @param default_value Optional default value. Returned when mut var lookup failed.
161 */
162template <typename T>
163void get_mut_var(const char *name, T& out, const T& default_value = {}) const;
164
165
166/** @} */
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_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:73
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:164
Used to capture error output from script evaluation.
Definition script.h:197
Entity.
Definition entity.hpp:30
Owned string wrapper.
Definition string.hpp:15