1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use super::*;
use crate::sys;
use alloc::string::String;
use flecs_ecs::core::*;
/// Script mixin implementation
impl World {
/// Create a new script builder.
/// This will create a new entity that is associated with a script.
///
/// The entity will receive an [`EcsScript`][crate::sys::EcsScript] component.
pub fn script(&self) -> ScriptBuilder<'_> {
ScriptBuilder::new(self)
}
/// Create a new script builder with a name.
/// This will create a new named entity that is associated with a script.
///
/// The entity will receive an [`EcsScript`][crate::sys::EcsScript] component.
pub fn script_named(&self, name: &str) -> ScriptBuilder<'_> {
ScriptBuilder::new_named(self, name)
}
/// Create a new script builder that is associated with an entity.
/// This will not create a new entity, but will associate the script with an existing entity.
/// This is useful if you want to tie the lifetime of the script to an existing entity.
///
/// The entity will set a (new) [`EcsScript`][crate::sys::EcsScript] component.
pub fn script_from(&self, entity: impl Into<Entity>) -> ScriptBuilder<'_> {
ScriptBuilder::new_from(self, entity)
}
/// Parse script. This parses a script and instantiates the entities in the world.
/// This operation is the equivalent to doing: [`parse`][flecs_ecs::addons::script::Script::parse], [`eval`][flecs_ecs::addons::script::Script::eval], [`destroy`][flecs_ecs::addons::script::Script::destroy].
///
/// # Arguments
///
/// * name - The script name (typically the file).
///
/// * code - The script.
///
/// # Returns
///
/// True if success, false otherwise.
///
/// # See also
///
/// * C API: `ecs_script_run`
pub fn run_code(&self, name: &str, code: &str) -> bool {
Script::run_code(self, name, code)
}
/// Parse script file. This parses a script file and instantiates the entities in the world.
/// This operation is equivalent to loading the file contents and passing it to `run`.
///
/// # Arguments
///
/// * filename - The script file name.
///
/// # Returns
///
/// True if success, false if failed.
pub fn run_file(&self, filename: &str) -> bool {
Script::run_file(self, filename)
}
/// Serialize value into a String.
/// This operation serializes a value of the provided type to a string.
///
/// # Safety
///
/// The caller must ensure that `value` points to valid data of the type specified by `id_of_value`.
///
/// # See also
///
/// * C API: `ecs_ptr_to_expr`
pub unsafe fn to_expr_id<T: IntoEntity>(
&self,
id_of_value: T,
value: *const T::CastType,
) -> String {
unsafe { Script::to_expr(self, id_of_value, value) }
}
/// Serialize value into a String.
/// This operation serializes a value of the provided type to a string.
///
/// # See also
///
/// * C API: `ecs_ptr_to_expr`
pub fn to_expr<T: ComponentId>(&self, value: &T) -> String {
unsafe { Script::to_expr(self, T::id(), value as *const T) }
}
/*
template <typename T>
inline T world::get_const_var(
const char *name,
const T& default_value) const
{
ecs_value_t value = flecs::_::get_const_var(world_, name);
if (!value.ptr) {
return default_value;
}
flecs::id_t type = flecs::_::type<T>::id(world_);
if (type == value.type) {
return *(static_cast<T*>(value.ptr));
}
return flecs::_::get_const_value<T>(
world_, name, value, type, default_value);
}
template <typename T>
void world::get_const_var(
const char *name,
T& out,
const T& default_value) const
{
ecs_value_t value = flecs::_::get_const_var(world_, name);
if (!value.ptr) {
out = default_value;
return;
}
flecs::id_t type = flecs::_::type<T>::id(world_);
if (type == value.type) {
out = *(static_cast<T*>(value.ptr));
return;
}
out = flecs::_::get_const_value<T>(
world_, name, value, type, default_value);
}
*/
pub fn get_const_var(&self, name: &str) -> Option<sys::ecs_value_t> {
Script::get_const_var(self, name)
}
pub fn get_const_numeric<T: ConstNumeric>(&self, value: sys::ecs_value_t) -> T::ConstType {
Script::get_const_numeric::<T>(self, value)
}
pub fn get_const_str(&self, value: sys::ecs_value_t) -> String {
Script::get_const_str(self, value)
}
pub fn get_const_charptr(&self, value: sys::ecs_value_t) -> core::ffi::c_char {
Script::get_const_char(self, value)
}
/// Wraps the provided entity id in a [`ScriptEntityView`].
///
/// # Panics
///
/// The entity must have a [`flecs::Script`] component.
pub fn script_entity_from(&self, id: impl IntoEntity) -> ScriptEntityView<'_> {
ScriptEntityView::new_from(self, id)
}
}