pass entity

This commit is contained in:
Jaroslaw Konik 2025-05-16 07:00:00 +02:00
parent fccc822f9a
commit c5a9f54695
5 changed files with 81 additions and 17 deletions

View file

@ -0,0 +1,3 @@
def test_func
rust_func($entity)
end

View file

@ -1,6 +1,7 @@
use bevy::{
asset::Asset,
ecs::{component::Component, entity::Entity, resource::Resource, schedule::ScheduleLabel},
log,
math::Vec3,
reflect::TypePath,
};
@ -260,16 +261,18 @@ impl Runtime for LuaRuntime {
fn with_engine_thread_mut<T: Send + 'static>(
&mut self,
_f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static,
f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static,
) -> T {
todo!()
log::warn!("runtime can be used on current thread, wil run on current thread");
self.with_engine_mut(f)
}
fn with_engine_thread<T: Send + 'static>(
&self,
_f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static,
f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static,
) -> T {
todo!()
log::warn!("runtime can be used on current thread, wil run on current thread");
self.with_engine(f)
}
fn is_current_thread() -> bool {

View file

@ -3,6 +3,7 @@ use std::fmt::Debug;
use bevy::{
asset::Asset,
ecs::{component::Component, entity::Entity, resource::Resource, schedule::ScheduleLabel},
log,
math::Vec3,
reflect::TypePath,
};
@ -163,16 +164,18 @@ impl Runtime for RhaiRuntime {
fn with_engine_thread_mut<T: Send + 'static>(
&mut self,
_f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static,
f: impl FnOnce(&mut Self::RawEngine) -> T + Send + 'static,
) -> T {
todo!()
log::warn!("runtime can be used on current thread, wil run on current thread");
self.with_engine_mut(f)
}
fn with_engine_thread<T: Send + 'static>(
&self,
_f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static,
f: impl FnOnce(&Self::RawEngine) -> T + Send + 'static,
) -> T {
todo!()
log::warn!("runtime can be used on current thread, wil run on current thread");
self.with_engine(f)
}
fn is_current_thread() -> bool {

View file

@ -127,6 +127,12 @@ unsafe impl TypedData for Promise<(), RubyValue> {
}
}
impl TryConvert for Promise<(), RubyValue> {
fn try_convert(val: magnus::Value) -> Result<Self, magnus::Error> {
TryConvert::try_convert(val).map(|p: &Self| p.clone())
}
}
fn then(r_self: magnus::Value) -> magnus::Value {
let promise: &Promise<(), RubyValue> = TryConvert::try_convert(r_self).unwrap();
let ruby = Ruby::get().unwrap();
@ -143,6 +149,7 @@ fn then(r_self: magnus::Value) -> magnus::Value {
.into_value()
}
#[derive(Clone)]
#[magnus::wrap(class = "BevyEntity")]
pub struct BevyEntity(pub Entity);
@ -152,6 +159,13 @@ impl BevyEntity {
}
}
impl TryConvert for BevyEntity {
fn try_convert(val: magnus::Value) -> Result<Self, magnus::Error> {
TryConvert::try_convert(val).map(|p: &Self| p.clone())
}
}
#[derive(Clone)]
#[magnus::wrap(class = "BevyVec3")]
pub struct BevyVec3(pub Vec3);
@ -169,6 +183,12 @@ impl BevyVec3 {
}
}
impl TryConvert for BevyVec3 {
fn try_convert(val: magnus::Value) -> Result<Self, magnus::Error> {
TryConvert::try_convert(val).map(|p: &Self| p.clone())
}
}
impl Default for RubyRuntime {
fn default() -> Self {
let (lock, cvar) = &*Arc::clone(&RUBY_THREAD);

View file

@ -65,7 +65,7 @@ trait AssertStateKeyValue {
#[cfg(any(feature = "rhai", feature = "lua", feature = "ruby"))]
macro_rules! scripting_tests {
($runtime:ty, $script:literal, $extension:literal) => {
($runtime:ty, $script:literal, $extension:literal, $entity_type: ty) => {
use super::*;
#[test]
@ -380,7 +380,7 @@ macro_rules! scripting_tests {
}
#[test]
fn entity_variable_is_available_in_callback() {
fn entity_variable_index_is_available_in_callback() {
let mut app = build_test_app();
#[derive(Default, Resource)]
@ -412,12 +412,12 @@ macro_rules! scripting_tests {
}
#[test]
fn entity_variable_is_available_in_eval() {
fn entity_variable_index_is_available_in_eval() {
let mut app = build_test_app();
#[derive(Default, Resource)]
struct State {
index: u32,
index: Option<u32>,
}
app.world_mut().init_resource::<State>();
@ -426,7 +426,7 @@ macro_rules! scripting_tests {
runtime.add_function(
String::from("rust_func"),
|In((index,)): In<(u32,)>, mut res: ResMut<State>| {
res.index = index;
res.index = Some(index);
},
);
});
@ -439,7 +439,39 @@ macro_rules! scripting_tests {
assert_eq!(
app.world().get_resource::<State>().unwrap().index,
entity.index()
Some(entity.index())
);
}
#[test]
fn pass_entity_from_script() {
let mut app = build_test_app();
#[derive(Default, Resource)]
struct State {
index: Option<u32>,
}
app.world_mut().init_resource::<State>();
app.add_scripting::<$runtime>(|runtime| {
runtime.add_function(
String::from("rust_func"),
|In((entity,)): In<($entity_type,)>, mut res: ResMut<State>| {
res.index = Some(entity.0.index());
},
);
});
let entity = run_script::<$runtime, _, _>(
&mut app,
format!("tests/{}/pass_entity_from_script.{}", $script, $extension).to_string(),
call_script_on_update_from_rust::<$runtime>,
);
assert_eq!(
app.world().get_resource::<State>().unwrap().index,
Some(entity.index())
);
}
};
@ -450,6 +482,9 @@ mod rhai_tests {
use bevy::prelude::*;
use bevy_scriptum::runtimes::rhai::prelude::*;
#[derive(Clone)]
struct BevyEntity(Entity);
impl AssertStateKeyValue for RhaiRuntime {
type ScriptData = RhaiScriptData;
@ -472,7 +507,7 @@ mod rhai_tests {
}
}
scripting_tests!(RhaiRuntime, "rhai", "rhai");
scripting_tests!(RhaiRuntime, "rhai", "rhai", BevyEntity);
}
#[cfg(feature = "lua")]
@ -514,7 +549,7 @@ mod lua_tests {
}
}
scripting_tests!(LuaRuntime, "lua", "lua");
scripting_tests!(LuaRuntime, "lua", "lua", BevyEntity);
}
#[cfg(feature = "ruby")]
@ -563,5 +598,5 @@ mod ruby_tests {
}
}
scripting_tests!(RubyRuntime, "ruby", "rb");
scripting_tests!(RubyRuntime, "ruby", "rb", BevyEntity);
}