diff --git a/assets/tests/rhai/pass_vec3_to_script.rhai b/assets/tests/rhai/pass_vec3_to_script.rhai index 4d04eb1..2c8a176 100644 --- a/assets/tests/rhai/pass_vec3_to_script.rhai +++ b/assets/tests/rhai/pass_vec3_to_script.rhai @@ -1,5 +1,5 @@ fn test_func(vec3) { - if type_of(vec3) != "tests::rhai_tests::BevyVec3" { throw() } + if type_of(vec3) != "Vec3" { throw() } // TODO: assert x,y,z mark_success(); } diff --git a/src/runtimes/lua.rs b/src/runtimes/lua.rs index 9d0962b..256c249 100644 --- a/src/runtimes/lua.rs +++ b/src/runtimes/lua.rs @@ -42,6 +42,12 @@ pub struct LuaRuntime { #[derive(Debug, Clone, Copy)] pub struct BevyEntity(pub Entity); +impl BevyEntity { + pub fn index(&self) -> u32 { + self.0.index() + } +} + impl UserData for BevyEntity {} impl FromLua<'_> for BevyEntity { @@ -63,6 +69,18 @@ impl BevyVec3 { pub fn new(x: f32, y: f32, z: f32) -> Self { BevyVec3(Vec3 { x, y, z }) } + + pub fn x(&self) -> f32 { + self.0.x + } + + pub fn y(&self) -> f32 { + self.0.y + } + + pub fn z(&self) -> f32 { + self.0.z + } } impl UserData for BevyVec3 {} diff --git a/src/runtimes/rhai.rs b/src/runtimes/rhai.rs index 5228075..31faeda 100644 --- a/src/runtimes/rhai.rs +++ b/src/runtimes/rhai.rs @@ -51,6 +51,36 @@ pub struct RhaiScriptData { #[derive(Debug, Clone)] pub struct RhaiValue(pub rhai::Dynamic); +#[derive(Clone)] +pub struct BevyEntity(pub Entity); + +impl BevyEntity { + pub fn index(&self) -> u32 { + self.0.index() + } +} + +#[derive(Clone)] +pub struct BevyVec3(pub Vec3); + +impl BevyVec3 { + pub fn new(x: f32, y: f32, z: f32) -> Self { + Self(Vec3::new(x, y, z)) + } + + pub fn x(&self) -> f32 { + self.0.x + } + + pub fn y(&self) -> f32 { + self.0.y + } + + pub fn z(&self) -> f32 { + self.0.z + } +} + impl Runtime for RhaiRuntime { type Schedule = RhaiSchedule; type ScriptAsset = RhaiScript; @@ -66,7 +96,7 @@ impl Runtime for RhaiRuntime { entity: Entity, ) -> Result { let mut scope = Scope::new(); - scope.push(ENTITY_VAR_NAME, entity); + scope.push(ENTITY_VAR_NAME, BevyEntity(entity)); let engine = &self.engine; @@ -78,7 +108,7 @@ impl Runtime for RhaiRuntime { .run_ast_with_scope(&mut scope, &ast) .map_err(|e| ScriptingError::RuntimeError(Box::new(e)))?; - scope.remove::(ENTITY_VAR_NAME).unwrap(); + scope.remove::(ENTITY_VAR_NAME).unwrap(); Ok(Self::ScriptData { ast, scope }) } @@ -114,7 +144,7 @@ impl Runtime for RhaiRuntime { ) -> Result { let ast = script_data.ast.clone(); let scope = &mut script_data.scope; - scope.push(ENTITY_VAR_NAME, entity); + scope.push(ENTITY_VAR_NAME, BevyEntity(entity)); let options = CallFnOptions::new().eval_ast(false); let args = args .parse(&self.engine) @@ -124,7 +154,7 @@ impl Runtime for RhaiRuntime { let result = self .engine .call_fn_with_options::(options, scope, &ast, name, args); - scope.remove::(ENTITY_VAR_NAME).unwrap(); + scope.remove::(ENTITY_VAR_NAME).unwrap(); match result { Ok(val) => Ok(RhaiValue(val)), Err(e) => Err(ScriptingError::RuntimeError(Box::new(e))), @@ -188,8 +218,8 @@ impl Default for RhaiRuntime { let mut engine = Engine::new(); engine - .register_type_with_name::("Entity") - .register_get("index", |entity: &mut Entity| entity.index()); + .register_type_with_name::("Entity") + .register_get("index", |entity: &mut BevyEntity| entity.index()); #[allow(deprecated)] engine .register_type_with_name::>("Promise") @@ -202,9 +232,9 @@ impl Default for RhaiRuntime { ); engine - .register_type_with_name::("Vec3") + .register_type_with_name::("Vec3") .register_fn("new_vec3", |x: f64, y: f64, z: f64| { - Vec3::new(x as f32, y as f32, z as f32) + BevyVec3(Vec3::new(x as f32, y as f32, z as f32)) }) .register_get("x", |vec: &mut Vec3| vec.x as f64) .register_get("y", |vec: &mut Vec3| vec.y as f64) @@ -242,7 +272,7 @@ impl FromRuntimeValueWithEngine<'_, RhaiRuntime> for T { } pub mod prelude { - pub use super::{RhaiRuntime, RhaiScript, RhaiScriptData}; + pub use super::{BevyEntity, BevyVec3, RhaiRuntime, RhaiScript, RhaiScriptData}; } macro_rules! impl_tuple { diff --git a/src/runtimes/ruby.rs b/src/runtimes/ruby.rs index d7a3e97..ccba49b 100644 --- a/src/runtimes/ruby.rs +++ b/src/runtimes/ruby.rs @@ -176,7 +176,7 @@ fn then(r_self: magnus::Value) -> magnus::Value { pub struct BevyEntity(pub Entity); impl BevyEntity { - fn index(&self) -> u32 { + pub fn index(&self) -> u32 { self.0.index() } } diff --git a/tests/tests.rs b/tests/tests.rs index 411c4a6..e39de94 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -458,7 +458,7 @@ macro_rules! scripting_tests { runtime.add_function( String::from("rust_func"), |In((entity,)): In<($entity_type,)>, mut res: ResMut| { - res.index = Some(entity.0.index()); + res.index = Some(entity.index()); }, ); }); @@ -490,9 +490,9 @@ macro_rules! scripting_tests { runtime.add_function( String::from("rust_func"), |In((v,)): In<($vec_type,)>, mut res: ResMut| { - assert_eq!(v.0.x, 1.5); - assert_eq!(v.0.y, 2.5); - assert_eq!(v.0.z, -3.5); + assert_eq!(v.x(), 1.5); + assert_eq!(v.y(), 2.5); + assert_eq!(v.z(), -3.5); res.success = true }, ); @@ -551,18 +551,6 @@ mod rhai_tests { use bevy::prelude::*; use bevy_scriptum::runtimes::rhai::prelude::*; - #[derive(Clone)] - struct BevyEntity(Entity); - - #[derive(Clone)] - struct BevyVec3(Vec3); - - impl BevyVec3 { - fn new(x: f32, y: f32, z: f32) -> Self { - Self(Vec3 { x, y, z }) - } - } - impl AssertStateKeyValue for RhaiRuntime { type ScriptData = RhaiScriptData;