add example
This commit is contained in:
parent
9bcf3ee589
commit
2a2b2343d7
8 changed files with 72 additions and 6 deletions
|
|
@ -165,6 +165,12 @@ name = "current_entity_ruby"
|
||||||
path = "examples/ruby/current_entity.rs"
|
path = "examples/ruby/current_entity.rs"
|
||||||
required-features = ["ruby"]
|
required-features = ["ruby"]
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "custom_type_ruby"
|
||||||
|
path = "examples/ruby/custom_type.rs"
|
||||||
|
required-features = ["ruby"]
|
||||||
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tracing-subscriber = "0.3.18"
|
tracing-subscriber = "0.3.18"
|
||||||
mlua = { version = "0.9.8", features = ["luajit", "vendored", "send"] }
|
mlua = { version = "0.9.8", features = ["luajit", "vendored", "send"] }
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,3 @@
|
||||||
get_name(Bevy::Entity.current).and_then do |name|
|
get_name(Bevy::Entity.current).and_then do |name|
|
||||||
print(name)
|
puts(name)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
4
assets/examples/ruby/custom_type.rb
Normal file
4
assets/examples/ruby/custom_type.rb
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
# Create a new instance of MyType
|
||||||
|
my_type = MyType.new()
|
||||||
|
# Call registered method
|
||||||
|
puts(my_type.my_method)
|
||||||
54
examples/ruby/custom_type.rs
Normal file
54
examples/ruby/custom_type.rs
Normal 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"),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
@ -273,7 +273,7 @@ use self::{
|
||||||
systems::{process_new_scripts, reload_scripts},
|
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";
|
const ENTITY_VAR_NAME: &str = "entity";
|
||||||
|
|
||||||
/// An error that can occur when internal [ScriptingPlugin] systems are being executed
|
/// An error that can occur when internal [ScriptingPlugin] systems are being executed
|
||||||
|
|
|
||||||
|
|
@ -287,7 +287,6 @@ impl Runtime for LuaRuntime {
|
||||||
&mut self,
|
&mut self,
|
||||||
f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static,
|
f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static,
|
||||||
) -> T {
|
) -> T {
|
||||||
log::warn!("runtime can be used on current thread, wil run on current thread");
|
|
||||||
self.with_engine_mut(f)
|
self.with_engine_mut(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -295,7 +294,6 @@ impl Runtime for LuaRuntime {
|
||||||
&self,
|
&self,
|
||||||
f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static,
|
f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static,
|
||||||
) -> T {
|
) -> T {
|
||||||
log::warn!("runtime can be used on current thread, wil run on current thread");
|
|
||||||
self.with_engine(f)
|
self.with_engine(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -196,7 +196,6 @@ impl Runtime for RhaiRuntime {
|
||||||
&mut self,
|
&mut self,
|
||||||
f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static,
|
f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static,
|
||||||
) -> T {
|
) -> T {
|
||||||
log::warn!("runtime can be used on current thread, wil run on current thread");
|
|
||||||
self.with_engine_mut(f)
|
self.with_engine_mut(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -204,7 +203,6 @@ impl Runtime for RhaiRuntime {
|
||||||
&self,
|
&self,
|
||||||
f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static,
|
f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static,
|
||||||
) -> T {
|
) -> T {
|
||||||
log::warn!("runtime can be used on current thread, wil run on current thread");
|
|
||||||
self.with_engine(f)
|
self.with_engine(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -322,6 +322,7 @@ impl Runtime for RubyRuntime {
|
||||||
|
|
||||||
type RawEngine = magnus::Ruby;
|
type RawEngine = magnus::Ruby;
|
||||||
|
|
||||||
|
// TODO: it should be somehow possible to remove 'static here
|
||||||
fn with_engine_thread_mut<T: Send + 'static>(
|
fn with_engine_thread_mut<T: Send + 'static>(
|
||||||
&mut self,
|
&mut self,
|
||||||
f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static,
|
f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static,
|
||||||
|
|
@ -329,6 +330,7 @@ impl Runtime for RubyRuntime {
|
||||||
self.execute_in_thread_mut(f)
|
self.execute_in_thread_mut(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: it should be somehow possible to remove 'static here
|
||||||
fn with_engine_thread<T: Send + 'static>(
|
fn with_engine_thread<T: Send + 'static>(
|
||||||
&self,
|
&self,
|
||||||
f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static,
|
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 mod prelude {
|
||||||
pub use super::{BevyEntity, BevyVec3, RubyRuntime, RubyScript, RubyScriptData};
|
pub use super::{BevyEntity, BevyVec3, RubyRuntime, RubyScript, RubyScriptData};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue