forked from makspll/bevy_mod_scripting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bevy_api.rs
170 lines (148 loc) · 6.51 KB
/
bevy_api.rs
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
164
165
166
167
168
169
170
use bevy::app::AppExit;
use bevy::math::DQuat;
use bevy::prelude::*;
use bevy_mod_scripting::{api::rhai::bevy::RhaiBevyAPIProvider, prelude::*};
use bevy_mod_scripting_rhai::rhai::Engine;
use bevy_script_api::rhai::{std::RegisterVecType, RegisterForeignRhaiType};
/// Let's define a resource, we want it to be "assignable" via lua so we derive `ReflectLuaProxyable`
/// This allows us to reach this value when it's a field under any other Reflectable type
#[derive(Resource, Default, Clone, Reflect)]
#[reflect(Resource)]
pub struct MyResource {
pub thing: f64,
}
#[derive(Component, Default, Reflect)]
#[reflect(Component)]
pub struct MyComponent {
dquat: DQuat,
quat: Quat,
vec2: Vec2,
vec3: Vec3,
uvec2: UVec2,
usize: usize,
f32: f32,
mat3: Mat3,
vec4: Vec4,
bool: bool,
string: String,
u8: u8,
bool_option: Option<bool>,
option: Option<Vec3>,
vec_of_option_bools: Vec<Option<bool>>,
option_vec_of_bools: Option<Vec<bool>>,
}
pub struct MyAPIProvider;
// unlike mlua, rhai does not have the concept of generic types, all functionality is based around
// registering monomorphized functions, therefore we must register functions of generic types for every type we want
// to use them with, it's less convenient and is what the compiler would do anyway but hey it works!
impl APIProvider for MyAPIProvider {
type APITarget = Engine;
type ScriptContext = RhaiContext;
type DocTarget = RhaiDocFragment;
fn attach_api(&mut self, api: &mut Self::APITarget) -> Result<(), ScriptError> {
api.set_max_expr_depths(999, 999);
api.register_vec_functions::<Option<bool>>();
api.register_vec_functions::<bool>();
Ok(())
}
}
fn main() -> std::io::Result<()> {
let mut app = App::new();
app.add_plugins(DefaultPlugins)
.add_plugin(ScriptingPlugin)
.register_type::<MyComponent>()
.register_type::<MyResource>()
.register_foreign_rhai_type::<Option<bool>>()
.register_foreign_rhai_type::<Vec<Option<bool>>>()
.register_foreign_rhai_type::<Option<Vec<bool>>>()
// note the implementation for Option is there, but we must register `LuaProxyable` for it
.init_resource::<MyResource>()
// this system set handles addition and removal of script contexts, we can safely use `CoreSet::PostUpdate`
.add_script_host_to_base_set::<RhaiScriptHost<()>, _>(CoreSet::PostUpdate)
.add_api_provider::<RhaiScriptHost<()>>(Box::new(RhaiBevyAPIProvider))
.add_api_provider::<RhaiScriptHost<()>>(Box::new(MyAPIProvider))
.add_system(|world: &mut World| {
let entity = world
.spawn(())
.insert(MyComponent {
vec2: Vec2::new(1.0, 2.0),
vec3: Vec3::new(1.0, 2.0, 3.0),
vec4: Vec4::new(1.0, 2.0, 3.0, 4.0),
uvec2: UVec2::new(1, 2),
usize: 5,
f32: 6.7,
mat3: Mat3::from_cols(
Vec3::new(1.0, 2.0, 3.0),
Vec3::new(4.0, 5.0, 6.0),
Vec3::new(7.0, 8.0, 9.0),
),
quat: Quat::from_xyzw(1.0, 2.0, 3.0, 4.0),
dquat: DQuat::from_xyzw(1.0, 2.0, 3.0, 4.0),
bool: true,
string: "hello".to_owned(),
u8: 240,
bool_option: Some(true),
option: None,
vec_of_option_bools: vec![Some(true), None, Some(false)],
option_vec_of_bools: Some(vec![true, true, true]),
})
.id();
// run script
world.resource_scope(|world, mut host: Mut<RhaiScriptHost<()>>| {
host.run_one_shot(
r#"
fn once() {
print(world);
print(world.get_children(entity));
let my_component_type = world.get_type_by_name("MyComponent");
let my_component = world.get_component(entity,my_component_type);
debug(my_component_type);
debug(my_component);
print(my_component.u8);
my_component.u8 = 255;
my_component.bool = false;
my_component.string = "bye";
my_component.bool_option = ();
print(my_component.u8);
print(my_component.bool);
print(my_component.string);
print(my_component.bool_option);
my_component.bool_option = true;
print(my_component.bool_option);
print("----");
for e in my_component.vec_of_option_bools {
print(`elem: ${e}`)
}
print("----");
my_component.vec_of_option_bools = [true,false,true];
my_component.vec_of_option_bools[0] = false;
my_component.vec_of_option_bools.insert(1,());
my_component.vec_of_option_bools.push(false);
for e in my_component.vec_of_option_bools {
print(`elem: ${e}`)
}
let my_component_after = world.get_component(entity,my_component_type);
print("after script:");
print(my_component_after.u8);
print(my_component_after.bool);
print(my_component_after.string);
print(my_component_after.bool_option);
}
"#
.as_bytes(),
"script.lua",
entity,
world,
RhaiEvent {
hook_name: "once".to_owned(),
args: (),
recipients: Recipients::All,
},
)
.expect("Something went wrong in the script!");
});
world.send_event(AppExit)
});
app.run();
Ok(())
}