forked from makspll/bevy_mod_scripting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbevy_api.rs
More file actions
238 lines (193 loc) · 10.7 KB
/
bevy_api.rs
File metadata and controls
238 lines (193 loc) · 10.7 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use bevy::app::AppExit;
use bevy::math::DQuat;
use bevy::prelude::*;
use bevy_mod_scripting::prelude::*;
use bevy_mod_scripting_lua::tealr::mlu::mlua::UserData;
use bevy_script_api::lua::{
bevy::LuaBevyAPIProvider, ReflectLuaProxyable, RegisterForeignLuaType, ValueLuaType,
};
/// 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(Default, Clone, Resource, Reflect)]
#[reflect(Resource, LuaProxyable)]
pub struct MyResource {
pub thing: f64,
}
/// NOTE: this is a marker enabling an automatic implementation of LuaProxyable
/// By default, because this type implements Clone as well,
/// It will be passed BY VALUE
/// meaning that calling these methods will result in changes to the cloned value on lua side only
/// untill the resource is assigned back to the original component to make the changes on the original type.
/// To have "pass by reference" semantics use a [`bevy_mod_scripting::api::lua::LuaWrapper`] and implement LuaProxyable yourself (see wrappers.rs example)
impl ValueLuaType for MyResource {}
impl UserData for MyResource {
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_method_mut("custom_resource_method", |_, s, v: f64| {
s.thing = v;
Ok("hello?")
});
methods.add_meta_method(mlua::MetaMethod::ToString, |_, s, ()| {
Ok(format!(
"I'm a resource with a custom metatable!: {}",
s.thing
))
});
}
}
#[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,
u8: u8,
option: Option<Vec3>,
vec_of_option_bools: Vec<Option<bool>>,
option_vec_of_bools: Option<Vec<bool>>,
}
fn main() -> std::io::Result<()> {
let mut app = App::new();
app.add_plugins(DefaultPlugins)
.add_plugin(ScriptingPlugin)
.register_type::<MyComponent>()
.register_type::<MyResource>()
// note the implementation for Option is there, but we must register `LuaProxyable` for it
.register_foreign_lua_type::<Option<Vec3>>()
.register_foreign_lua_type::<Vec<Option<bool>>>()
.register_foreign_lua_type::<Option<bool>>()
.register_foreign_lua_type::<Option<Vec<bool>>>()
.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::<LuaScriptHost<()>, _>(CoreSet::PostUpdate)
.add_script_handler_to_base_set::<LuaScriptHost<()>,_,0,0>(CoreSet::PostUpdate)
.add_api_provider::<LuaScriptHost<()>>(Box::new(LuaBevyAPIProvider))
.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),
u8: 240,
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<LuaScriptHost<()>>| {
host.run_one_shot(
r#"
function once()
-- the api provides us with 3 globals
print(entity)
print(script)
print(world)
-- we first retrieve ID's for our component and resource by their short name (long name/full path also work)
local my_component_type = world:get_type_by_name("MyComponent")
local my_resource_type = world:get_type_by_name("MyResource")
-- then ask the world to give us a reference to `MyComponent` on the entity we just spawned
local comp = world:get_component(entity, my_component_type)
-- and our resource
local res = world:get_resource(my_resource_type)
-- we can now arbitrarily affect these
-- we can even call our custom methods!
print(string.format(res:custom_resource_method(42)))
-- we can set any of the fields
-- if the field is `LuaProxyable` like our resource
-- that trait's implementation controls this assignment
comp.u8 = 2
-- Option's get converted to nil | Value
if comp.option == nil then
print(string.format("option was %s", comp.option))
comp.option = Vec3.new(2,1,3)
print(string.format("option[1] is now %s", comp.option[1]))
comp.option[1] = 5
print(string.format("and now option[1] is %s", comp.option[1]))
end
-- Vec<T> references get converted to a special proxy `LuaVec<T>` which is
-- assignable via the Lua Tables
comp.vec_of_option_bools = {true,false,true}
-- everything on the bevy side which uses Lua constructs as "Owned" variants
-- indexes from 1, other types map one to one with the bevy implementations and index from zero
comp.vec_of_option_bools[1] = false
-- there are some additional methods available on LuaVec
comp.vec_of_option_bools:insert(1,nil)
comp.vec_of_option_bools:push(false)
-- Note, that Option's are reflected as Value types in Bevy, we are using
-- a custom magical SubReflection system allowing us to do this!
comp.option_vec_of_bools = {false,true,false}
comp.option_vec_of_bools[1] = true
comp.option_vec_of_bools:insert(1,false)
comp.option_vec_of_bools:push(true)
print(#comp.vec_of_option_bools)
print(comp.vec_of_option_bools:pop())
print(comp.option_vec_of_bools:pop())
for k,v in pairs(comp.vec_of_option_bools) do
print(string.format("%s:%s",k,v))
end
print(#comp.option_vec_of_bools)
for k,v in pairs(comp.option_vec_of_bools) do
print(string.format("%s:%s",k,v))
end
comp.vec_of_option_bools:clear()
print(#comp.vec_of_option_bools)
-- Every Bevy type implementing Reflect is available as a LuaProxyable wrapper
-- available types are under `api::lua::bevy::*` and std types in `api::lua::std::*`
-- not all functions are exposed but support for more complex functions will be rolled out
comp.vec2 = comp.vec2 + comp.vec2
comp.uvec2 = comp.uvec2 + comp.uvec2
comp.usize = comp.vec2:min_element()
comp.f32 = comp.f32 + comp.f32 + comp.vec2:min_element()
comp.vec2 = Vec2.new(2,1)
comp.vec3 = Vec3.new(0,1,0):any_orthonormal_vector() + comp.mat3.x_axis + comp.option
comp.vec4 = Vec4.splat(3)
comp.quat = Quat.from_xyzw(3,2,1,4)
comp.dquat = comp.dquat * 2
local a = Mat3.from_cols(Vec3.new(1,0,0),Vec3.new(0,1,0),Vec3.new(0,0,-1))
-- again an instance of sub reflection here, very cool!
comp.mat3[0][0] = 42
comp.mat3.x_axis = Vec3.new(69,69,69)
-- now let's retrieve these again to see if we actually changed their values
comp = world:get_component(Entity.from_raw(0),my_component_type)
res = world:get_resource(my_resource_type)
-- notet that our custom resource's value has not affected the original
-- this is because it is a by-value proxy, see wrappers.rs for an alternative
print("After script:")
print(comp)
print(res)
end
"#
.as_bytes(),
"script.lua",
entity,
world,
LuaEvent {
hook_name: "once".to_owned(),
args: (),
recipients: Recipients::All,
},
)
.expect("Something went wrong in the script!");
});
world.send_event(AppExit)
},
);
app.run();
Ok(())
}