add example

This commit is contained in:
Jaroslaw Konik 2025-05-23 21:10:35 +02:00
parent 9bcf3ee589
commit 2a2b2343d7
8 changed files with 72 additions and 6 deletions

View file

@ -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"] }

View file

@ -1,3 +1,3 @@
get_name(Bevy::Entity.current).and_then do |name|
print(name)
puts(name)
end

View file

@ -0,0 +1,4 @@
# Create a new instance of MyType
my_type = MyType.new()
# Call registered method
puts(my_type.my_method)

View file

@ -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::<RubyRuntime>(|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<RubyRuntime>,
assets_server: Res<AssetServer>,
) {
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::<RubyScript>::new(
assets_server.load("examples/ruby/custom_type.rb"),
));
}

View file

@ -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

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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<T: Send + 'static>(
&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<T: Send + 'static>(
&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};
}