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
use flecs_ecs::core::*;
use flecs_ecs::sys;
use super::ScriptEntityView;
/// [`ScriptBuilder`] is a builder pattern for creating scripts.
pub struct ScriptBuilder<'a> {
script: sys::ecs_script_desc_t,
world: WorldRef<'a>,
}
impl<'a> ScriptBuilder<'a> {
/// 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 new(world: impl WorldProvider<'a>) -> Self {
ScriptBuilder {
script: sys::ecs_script_desc_t {
entity: 0,
filename: core::ptr::null(),
code: core::ptr::null(),
},
world: world.world(),
}
}
/// 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 new_named(world: impl WorldProvider<'a>, name: &str) -> Self {
let name = compact_str::format_compact!("{}\0", name);
let entity_desc = sys::ecs_entity_desc_t {
name: name.as_ptr() as *const _,
sep: SEPARATOR.as_ptr(),
root_sep: SEPARATOR.as_ptr(),
..Default::default()
};
ScriptBuilder {
script: sys::ecs_script_desc_t {
entity: unsafe { sys::ecs_entity_init(world.world_ptr_mut(), &entity_desc) },
filename: core::ptr::null(),
code: core::ptr::null(),
},
world: world.world(),
}
}
/// 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 new_from(world: impl WorldProvider<'a>, entity: impl Into<Entity>) -> Self {
ScriptBuilder {
script: sys::ecs_script_desc_t {
entity: entity.into().into(),
filename: core::ptr::null(),
code: core::ptr::null(),
},
world: world.world(),
}
}
/// Loads a managed script from a file into the ECS world.
///
/// This function initializes an ECS script from a file specified by `filename`.
///
/// # Arguments
///
/// * `world` - A pointer to the ECS world.
/// * `entity` - The entity handle associated with the script.
/// * `filename` - The path to the script file as a string slice.
///
/// # Returns
///
/// Returns the script entity handle of the loaded script.
///
/// # See also
///
/// * C API: `ecs_script_init`
pub fn build_from_file(&mut self, filename: &str) -> ScriptEntityView<'a> {
let filename = compact_str::format_compact!("{}\0", filename);
let world = self.world.world_ptr_mut();
self.script.filename = filename.as_ptr() as *const _;
let result = unsafe { sys::ecs_script_init(world, &self.script) };
ScriptEntityView::new_from(self.world, result)
}
/// Loads a managed script from a code string into the ECS world.
///
/// This function initializes an ECS script from a code string specified by `code`.
///
/// # Arguments
///
/// * `world` - A pointer to the ECS world.
/// * `entity` - The entity handle associated with the script.
/// * `code` - The script code as a string slice.
///
/// # Returns
///
/// Returns the script entity handle of the loaded script.
///
/// # See also
///
/// * C API: `ecs_script_init`
pub fn build_from_code(&mut self, code: &str) -> ScriptEntityView<'a> {
let code = compact_str::format_compact!("{}\0", code);
let world = self.world.world_ptr_mut();
self.script.code = code.as_ptr() as *const _;
let result = unsafe { sys::ecs_script_init(world, &self.script) };
ScriptEntityView::new_from(self.world, result)
}
}