Skip to content

feat: Call custom get and set functions on the type when indexing. #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/bevy_mod_scripting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
- 'docs/**'


name: Check and Lint - bevy_mod_scripting
name: CI


env:
Expand Down
48 changes: 0 additions & 48 deletions crates/bevy_mod_scripting_core/src/bindings/function/namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,54 +97,6 @@ impl Namespace {
}
}

// impl RegisterNamespacedFunction for ScriptFunctionRegistry {
// fn register_namespaced_function<S, N, F, M>(&mut self, name: N, function: F)
// where
// N: Into<Cow<'static, str>>,
// S: IntoNamespace,
// F: ScriptFunction<'static, M>,
// {
// self.register(S::into_namespace(), name, function);
// }
// }

// impl GetNamespacedFunction for ScriptFunctionRegistry {
// fn iter_overloads_namespaced<N>(
// &self,
// name: N,
// namespace: Namespace,
// ) -> impl Iterator<Item = &DynamicScriptFunction>
// where
// N: Into<Cow<'static, str>>,
// {
// let cow: Cow<'static, str> = name.into();
// let function_name = namespace.function_name(cow);
// self.iter_overloads(function_name)
// }

// fn get_namespaced_function<N>(
// &self,
// name: N,
// namespace: Namespace,
// ) -> Option<&DynamicScriptFunction>
// where
// N: Into<Cow<'static, str>>,
// {
// let cow: Cow<'static, str> = name.into();
// let function_name = namespace.function_name(cow);
// self.get_first(&function_name)
// }

// fn has_namespaced_function<N>(&self, name: N, namespace: Namespace) -> bool
// where
// N: Into<Cow<'static, str>>,
// {
// let cow: Cow<'static, str> = name.into();
// let function_name = namespace.function_name(cow);
// self.contains(&function_name)
// }
// }

pub struct NamespaceBuilder<'a, N> {
namespace: PhantomData<N>,
pub world: &'a mut World,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl UserData for LuaReflectReference {
};

let func = world
.lookup_function([TypeId::of::<ReflectReference>()], "get")
.lookup_function([type_id, TypeId::of::<ReflectReference>()], "get")
.map_err(|f| {
InteropError::missing_function(TypeId::of::<ReflectReference>(), f)
})?;
Expand All @@ -76,9 +76,10 @@ impl UserData for LuaReflectReference {
let self_: ReflectReference = self_.into();
let key: ScriptValue = key.into();
let value: ScriptValue = value.into();
let type_id = self_.tail_type_id(world.clone())?.or_fake_id();

let func = world
.lookup_function([TypeId::of::<ReflectReference>()], "set")
.lookup_function([type_id, TypeId::of::<ReflectReference>()], "set")
.map_err(|f| {
InteropError::missing_function(TypeId::of::<ReflectReference>(), f)
})?;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
local Resource = world.get_type_by_name("TestResourceWithVariousFields")
local resource = world.get_resource(Resource)

resource.string = "Hello, World!"
resource.bool = true
resource.int = 42
resource.float = 3.0
resource.vec_usize = { 1, 2 }

assert(resource.string == "Hello, World!", "Expected 'Hello, World!', got " .. resource.string)
assert(resource.bool == true, "Expected true, got " .. tostring(resource.bool))
assert(resource.int == 42, "Expected 42, got " .. resource.int)
assert(resource.float == 3.0, "Expected 3.14, got " .. resource.float)
assert(resource.vec_usize[1] == 1, "Expected 1, got " .. resource.vec_usize[1])

resource.string = "Goodbye, World!"
resource.bool = false
resource.int = 24
resource.float = 1.0
resource.vec_usize = { 3, 4 }

assert(resource.string == "Goodbye, World!", "Expected 'Goodbye, World!', got " .. resource.string)
assert(resource.bool == false, "Expected false, got " .. tostring(resource.bool))
assert(resource.int == 24, "Expected 24, got " .. resource.int)
assert(resource.float == 1.0, "Expected 1.41, got " .. resource.float)
assert(resource.vec_usize[1] == 3, "Expected 3, got " .. resource.vec_usize[1])


Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,10 @@ impl CustomType for RhaiReflectReference {
let self_ = self_.0.clone();
let key = ScriptValue::from_dynamic(_index)?;
let value = ScriptValue::from_dynamic(_value)?;
let type_id = self_.tail_type_id(world.clone())?.or_fake_id();

let func = world
.lookup_function([TypeId::of::<ReflectReference>()], "set")
.lookup_function([type_id, TypeId::of::<ReflectReference>()], "set")
.map_err(|f| {
InteropError::missing_function(TypeId::of::<ReflectReference>(), f)
})?;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
let Resource = world.get_type_by_name.call("TestResourceWithVariousFields");
let resource = world.get_resource.call(Resource);

resource.string = "Hello, World!";
resource.bool = true;
resource.int = 42;
resource.float = 3.0;
resource.vec_usize = [ 1, 2 ];

assert(resource.string == "Hello, World!", "Expected 'Hello, World!', got " + resource.string);
assert(resource.bool == true, "Expected true, got " + resource.bool);
assert(resource.int == 42, "Expected 42, got " + resource.int);
assert(resource.float == 3.0, "Expected 3.14, got " + resource.float);
assert(resource.vec_usize[0] == 1, "Expected 1, got " + resource.vec_usize[1]);

resource.string = "Goodbye, World!";
resource.bool = false;
resource.int = 24;
resource.float = 1.0;
resource.vec_usize = [ 3, 4 ];

assert(resource.string == "Goodbye, World!", "Expected 'Goodbye, World!', got " + resource.string);
assert(resource.bool == false, "Expected false, got " + resource.bool);
assert(resource.int == 24, "Expected 24, got " + resource.int);
assert(resource.float == 1.0, "Expected 1.41, got " + resource.float);
assert(resource.vec_usize[0] == 3, "Expected 3, got " + resource.vec_usize[1]);


Loading