diff --git a/Cargo.toml b/Cargo.toml index f4bc6c2..dbea7df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -180,6 +180,11 @@ name = "entity_variable_ruby" path = "examples/ruby/entity_variable.rs" required-features = ["ruby"] +[[example]] +name = "function_params_ruby" +path = "examples/ruby/function_params.rs" +required-features = ["ruby"] + [dev-dependencies] tracing-subscriber = "0.3.18" mlua = { version = "0.9.8", features = ["luajit", "vendored", "send"] } diff --git a/assets/examples/ruby/function_params.rb b/assets/examples/ruby/function_params.rb new file mode 100644 index 0000000..89433cd --- /dev/null +++ b/assets/examples/ruby/function_params.rb @@ -0,0 +1,5 @@ +fun_with_string_param("hello") +fun_with_i64_param(5) +fun_with_multiple_params(5, "hello") +# fun_with_i64_and_array_param(5, [1, 2, "third element"]) +# TODO: add test for heteregenous array diff --git a/examples/ruby/function_params.rs b/examples/ruby/function_params.rs new file mode 100644 index 0000000..0999a5a --- /dev/null +++ b/examples/ruby/function_params.rs @@ -0,0 +1,60 @@ +use bevy::prelude::*; +use bevy_scriptum::prelude::*; +use bevy_scriptum::runtimes::ruby::magnus; +use bevy_scriptum::runtimes::ruby::prelude::*; + +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .add_scripting::(|runtime| { + runtime + .add_function(String::from("fun_without_params"), || { + println!("called without params"); + }) + .add_function( + String::from("fun_with_string_param"), + |In((x,)): In<(String,)>| { + println!("called with string: '{}'", x); + }, + ) + .add_function( + String::from("fun_with_i64_param"), + |In((x,)): In<(i64,)>| { + println!("called with i64: {}", x); + }, + ) + .add_function( + String::from("fun_with_multiple_params"), + |In((x, y)): In<(i64, String)>| { + println!("called with i64: {} and string: '{}'", x, y); + }, + ); + // .add_function( + // String::from("fun_with_i64_and_array_param"), + // |In((x, y)): In<(i64, magnus::value::RArray)>, runtime: Res| { + // runtime.with_engine(|engine| { + // println!( + // "called with i64: {} and dynamically typed array: [{:?}]", + // x, + // engine + // .registry_value::(&y) + // .unwrap() + // .pairs::() + // .map(|pair| pair.unwrap()) + // .map(|(_, v)| format!("{:?}", v)) + // .collect::>() + // .join(",") + // ); + // }); + // }, + // ); + }) + .add_systems(Startup, startup) + .run(); +} + +fn startup(mut commands: Commands, assets_server: Res) { + commands.spawn(Script::::new( + assets_server.load("examples/ruby/function_params.rb"), + )); +}