Make value struct fields public, add examples (#44)
This commit is contained in:
parent
2c82a2fa0e
commit
297bffd060
7 changed files with 99 additions and 4 deletions
10
Cargo.toml
10
Cargo.toml
|
|
@ -86,6 +86,11 @@ name = "side_effects_rhai"
|
|||
path = "examples/rhai/side_effects.rs"
|
||||
required-features = ["rhai"]
|
||||
|
||||
[[example]]
|
||||
name = "function_return_value_rhai"
|
||||
path = "examples/rhai/function_return_value.rs"
|
||||
required-features = ["rhai"]
|
||||
|
||||
[[example]]
|
||||
name = "call_function_from_rust_lua"
|
||||
path = "examples/lua/call_function_from_rust.rs"
|
||||
|
|
@ -141,6 +146,11 @@ name = "side_effects_lua"
|
|||
path = "examples/lua/side_effects.rs"
|
||||
required-features = ["lua"]
|
||||
|
||||
[[example]]
|
||||
name = "function_return_value_lua"
|
||||
path = "examples/lua/function_return_value.rs"
|
||||
required-features = ["lua"]
|
||||
|
||||
[dev-dependencies]
|
||||
tracing-subscriber = "0.3.18"
|
||||
mlua = { version = "0.9.8", features = ["luajit", "vendored", "send"] }
|
||||
|
|
|
|||
3
assets/examples/lua/function_return_value.lua
Normal file
3
assets/examples/lua/function_return_value.lua
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
function get_value()
|
||||
return 42
|
||||
end
|
||||
3
assets/examples/rhai/function_return_value.rhai
Normal file
3
assets/examples/rhai/function_return_value.rhai
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fn get_value() {
|
||||
42
|
||||
}
|
||||
42
examples/lua/function_return_value.rs
Normal file
42
examples/lua/function_return_value.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
use bevy::{app::AppExit, prelude::*};
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::lua::prelude::*;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_systems(Startup, startup)
|
||||
.add_systems(Update, call_lua_on_update_from_rust)
|
||||
.add_scripting::<LuaRuntime>(|runtime| {
|
||||
runtime.add_function(String::from("quit"), |mut exit: EventWriter<AppExit>| {
|
||||
exit.write(AppExit::Success);
|
||||
});
|
||||
})
|
||||
.run();
|
||||
}
|
||||
|
||||
fn startup(mut commands: Commands, assets_server: Res<AssetServer>) {
|
||||
commands.spawn(Script::<LuaScript>::new(
|
||||
assets_server.load("examples/lua/function_return_value.lua"),
|
||||
));
|
||||
}
|
||||
|
||||
fn call_lua_on_update_from_rust(
|
||||
mut scripted_entities: Query<(Entity, &mut LuaScriptData)>,
|
||||
scripting_runtime: ResMut<LuaRuntime>,
|
||||
mut exit: EventWriter<AppExit>,
|
||||
) {
|
||||
for (entity, mut script_data) in &mut scripted_entities {
|
||||
let val = scripting_runtime
|
||||
.call_fn("get_value", &mut script_data, entity, ())
|
||||
.unwrap()
|
||||
.0;
|
||||
scripting_runtime.with_engine(|engine| {
|
||||
println!(
|
||||
"script returned: {}",
|
||||
engine.registry_value::<mlua::Integer>(&val).unwrap()
|
||||
);
|
||||
});
|
||||
exit.write(AppExit::Success);
|
||||
}
|
||||
}
|
||||
37
examples/rhai/function_return_value.rs
Normal file
37
examples/rhai/function_return_value.rs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
use bevy::{app::AppExit, prelude::*};
|
||||
use bevy_scriptum::prelude::*;
|
||||
use bevy_scriptum::runtimes::rhai::prelude::*;
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_systems(Startup, startup)
|
||||
.add_systems(Update, call_rhai_on_update_from_rust)
|
||||
.add_scripting::<RhaiRuntime>(|runtime| {
|
||||
runtime.add_function(String::from("quit"), |mut exit: EventWriter<AppExit>| {
|
||||
exit.write(AppExit::Success);
|
||||
});
|
||||
})
|
||||
.run();
|
||||
}
|
||||
|
||||
fn startup(mut commands: Commands, assets_server: Res<AssetServer>) {
|
||||
commands.spawn(Script::<RhaiScript>::new(
|
||||
assets_server.load("examples/rhai/function_return_value.rhai"),
|
||||
));
|
||||
}
|
||||
|
||||
fn call_rhai_on_update_from_rust(
|
||||
mut scripted_entities: Query<(Entity, &mut RhaiScriptData)>,
|
||||
scripting_runtime: ResMut<RhaiRuntime>,
|
||||
mut exit: EventWriter<AppExit>,
|
||||
) {
|
||||
for (entity, mut script_data) in &mut scripted_entities {
|
||||
let val = scripting_runtime
|
||||
.call_fn("get_value", &mut script_data, entity, ())
|
||||
.unwrap()
|
||||
.0;
|
||||
println!("script returned: {}", val);
|
||||
exit.write(AppExit::Success);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
use bevy::{
|
||||
asset::Asset,
|
||||
ecs::{component::Component, entity::Entity, schedule::ScheduleLabel, resource::Resource},
|
||||
ecs::{component::Component, entity::Entity, resource::Resource, schedule::ScheduleLabel},
|
||||
math::Vec3,
|
||||
reflect::TypePath,
|
||||
};
|
||||
|
|
@ -21,7 +21,7 @@ use crate::{
|
|||
type LuaEngine = Arc<Mutex<Lua>>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct LuaValue(Arc<RegistryKey>);
|
||||
pub struct LuaValue(pub Arc<RegistryKey>);
|
||||
|
||||
impl LuaValue {
|
||||
fn new<'a, T: IntoLua<'a>>(engine: &'a Lua, value: T) -> Self {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ use std::fmt::Debug;
|
|||
|
||||
use bevy::{
|
||||
asset::Asset,
|
||||
ecs::{component::Component, entity::Entity, schedule::ScheduleLabel, resource::Resource},
|
||||
ecs::{component::Component, entity::Entity, resource::Resource, schedule::ScheduleLabel},
|
||||
math::Vec3,
|
||||
reflect::TypePath,
|
||||
};
|
||||
|
|
@ -48,7 +48,7 @@ pub struct RhaiScriptData {
|
|||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RhaiValue(rhai::Dynamic);
|
||||
pub struct RhaiValue(pub rhai::Dynamic);
|
||||
|
||||
impl Runtime for RhaiRuntime {
|
||||
type Schedule = RhaiSchedule;
|
||||
|
|
|
|||
Loading…
Reference in a new issue