From 2a2b2343d73033339176e016d356283cf1ef6ca4 Mon Sep 17 00:00:00 2001 From: Jaroslaw Konik Date: Fri, 23 May 2025 21:10:35 +0200 Subject: [PATCH] add example --- Cargo.toml | 6 +++ assets/examples/ruby/current_entity.rb | 2 +- assets/examples/ruby/custom_type.rb | 4 ++ examples/ruby/custom_type.rs | 54 ++++++++++++++++++++++++++ src/lib.rs | 2 +- src/runtimes/lua.rs | 2 - src/runtimes/rhai.rs | 2 - src/runtimes/ruby.rs | 6 +++ 8 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 assets/examples/ruby/custom_type.rb create mode 100644 examples/ruby/custom_type.rs diff --git a/Cargo.toml b/Cargo.toml index 8c7712c..d683189 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -165,6 +165,12 @@ name = "current_entity_ruby" path = "examples/ruby/current_entity.rs" required-features = ["ruby"] +[[example]] +name = "custom_type_ruby" +path = "examples/ruby/custom_type.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/current_entity.rb b/assets/examples/ruby/current_entity.rb index 4c2c77b..172b3e9 100644 --- a/assets/examples/ruby/current_entity.rb +++ b/assets/examples/ruby/current_entity.rb @@ -1,3 +1,3 @@ get_name(Bevy::Entity.current).and_then do |name| - print(name) + puts(name) end diff --git a/assets/examples/ruby/custom_type.rb b/assets/examples/ruby/custom_type.rb new file mode 100644 index 0000000..0ab76a2 --- /dev/null +++ b/assets/examples/ruby/custom_type.rb @@ -0,0 +1,4 @@ +# Create a new instance of MyType +my_type = MyType.new() +# Call registered method +puts(my_type.my_method) diff --git a/examples/ruby/custom_type.rs b/examples/ruby/custom_type.rs new file mode 100644 index 0000000..e775a7f --- /dev/null +++ b/examples/ruby/custom_type.rs @@ -0,0 +1,54 @@ +use bevy::prelude::*; +use bevy_scriptum::prelude::*; +use bevy_scriptum::runtimes::ruby::magnus; +use bevy_scriptum::runtimes::ruby::magnus::Module as _; +use bevy_scriptum::runtimes::ruby::magnus::Object as _; +use bevy_scriptum::runtimes::ruby::prelude::*; +use bevy_scriptum::ScriptingError; + +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .add_scripting::(|runtime| { + runtime.add_function(String::from("hello_bevy"), || { + println!("hello bevy, called from script"); + }); + }) + .add_systems(Startup, startup) + .run(); +} + +#[magnus::wrap(class = "MyType")] +struct MyType { + my_field: u32, +} + +impl MyType { + fn new() -> Self { + Self { my_field: 42 } + } + + fn my_method(&self) -> u32 { + self.my_field + } +} + +fn startup( + mut commands: Commands, + scripting_runtime: ResMut, + assets_server: Res, +) { + scripting_runtime + .with_engine_thread(|ruby| { + let my_type = ruby.define_class("MyType", ruby.class_object())?; + my_type.define_singleton_method("new", magnus::function!(MyType::new, 0))?; + my_type.define_method("my_method", magnus::method!(MyType::my_method, 0))?; + + Ok::<(), ScriptingError>(()) + }) + .unwrap(); + + commands.spawn(Script::::new( + assets_server.load("examples/ruby/custom_type.rb"), + )); +} diff --git a/src/lib.rs b/src/lib.rs index fb5ced6..844940a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -273,7 +273,7 @@ use self::{ systems::{process_new_scripts, reload_scripts}, }; -#[cfg(any(feature = "rhai", feature = "lua", feature = "ruby"))] +#[cfg(any(feature = "rhai", feature = "lua"))] const ENTITY_VAR_NAME: &str = "entity"; /// An error that can occur when internal [ScriptingPlugin] systems are being executed diff --git a/src/runtimes/lua.rs b/src/runtimes/lua.rs index 347030a..0926649 100644 --- a/src/runtimes/lua.rs +++ b/src/runtimes/lua.rs @@ -287,7 +287,6 @@ impl Runtime for LuaRuntime { &mut self, f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static, ) -> T { - log::warn!("runtime can be used on current thread, wil run on current thread"); self.with_engine_mut(f) } @@ -295,7 +294,6 @@ impl Runtime for LuaRuntime { &self, f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static, ) -> T { - log::warn!("runtime can be used on current thread, wil run on current thread"); self.with_engine(f) } diff --git a/src/runtimes/rhai.rs b/src/runtimes/rhai.rs index 00d6fe4..ca2b950 100644 --- a/src/runtimes/rhai.rs +++ b/src/runtimes/rhai.rs @@ -196,7 +196,6 @@ impl Runtime for RhaiRuntime { &mut self, f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static, ) -> T { - log::warn!("runtime can be used on current thread, wil run on current thread"); self.with_engine_mut(f) } @@ -204,7 +203,6 @@ impl Runtime for RhaiRuntime { &self, f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static, ) -> T { - log::warn!("runtime can be used on current thread, wil run on current thread"); self.with_engine(f) } diff --git a/src/runtimes/ruby.rs b/src/runtimes/ruby.rs index 195a4c3..0a9f18f 100644 --- a/src/runtimes/ruby.rs +++ b/src/runtimes/ruby.rs @@ -322,6 +322,7 @@ impl Runtime for RubyRuntime { type RawEngine = magnus::Ruby; + // TODO: it should be somehow possible to remove 'static here fn with_engine_thread_mut( &mut self, f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static, @@ -329,6 +330,7 @@ impl Runtime for RubyRuntime { self.execute_in_thread_mut(f) } + // TODO: it should be somehow possible to remove 'static here fn with_engine_thread( &self, f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static, @@ -476,6 +478,10 @@ impl Runtime for RubyRuntime { } } +pub mod magnus { + pub use magnus::*; +} + pub mod prelude { pub use super::{BevyEntity, BevyVec3, RubyRuntime, RubyScript, RubyScriptData}; }