Skip to content

Commit db8a7c3

Browse files
authored
feat: Call custom get and set functions on the type when indexing. (#226)
* make sure to call custom get and set functions first * shorten CI name
1 parent dbfef74 commit db8a7c3

File tree

6 files changed

+62
-52
lines changed

6 files changed

+62
-52
lines changed

.github/workflows/bevy_mod_scripting.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on:
77
- 'docs/**'
88

99

10-
name: Check and Lint - bevy_mod_scripting
10+
name: CI
1111

1212

1313
env:

crates/bevy_mod_scripting_core/src/bindings/function/namespace.rs

-48
Original file line numberDiff line numberDiff line change
@@ -97,54 +97,6 @@ impl Namespace {
9797
}
9898
}
9999

100-
// impl RegisterNamespacedFunction for ScriptFunctionRegistry {
101-
// fn register_namespaced_function<S, N, F, M>(&mut self, name: N, function: F)
102-
// where
103-
// N: Into<Cow<'static, str>>,
104-
// S: IntoNamespace,
105-
// F: ScriptFunction<'static, M>,
106-
// {
107-
// self.register(S::into_namespace(), name, function);
108-
// }
109-
// }
110-
111-
// impl GetNamespacedFunction for ScriptFunctionRegistry {
112-
// fn iter_overloads_namespaced<N>(
113-
// &self,
114-
// name: N,
115-
// namespace: Namespace,
116-
// ) -> impl Iterator<Item = &DynamicScriptFunction>
117-
// where
118-
// N: Into<Cow<'static, str>>,
119-
// {
120-
// let cow: Cow<'static, str> = name.into();
121-
// let function_name = namespace.function_name(cow);
122-
// self.iter_overloads(function_name)
123-
// }
124-
125-
// fn get_namespaced_function<N>(
126-
// &self,
127-
// name: N,
128-
// namespace: Namespace,
129-
// ) -> Option<&DynamicScriptFunction>
130-
// where
131-
// N: Into<Cow<'static, str>>,
132-
// {
133-
// let cow: Cow<'static, str> = name.into();
134-
// let function_name = namespace.function_name(cow);
135-
// self.get_first(&function_name)
136-
// }
137-
138-
// fn has_namespaced_function<N>(&self, name: N, namespace: Namespace) -> bool
139-
// where
140-
// N: Into<Cow<'static, str>>,
141-
// {
142-
// let cow: Cow<'static, str> = name.into();
143-
// let function_name = namespace.function_name(cow);
144-
// self.contains(&function_name)
145-
// }
146-
// }
147-
148100
pub struct NamespaceBuilder<'a, N> {
149101
namespace: PhantomData<N>,
150102
pub world: &'a mut World,

crates/languages/bevy_mod_scripting_lua/src/bindings/reference.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl UserData for LuaReflectReference {
5757
};
5858

5959
let func = world
60-
.lookup_function([TypeId::of::<ReflectReference>()], "get")
60+
.lookup_function([type_id, TypeId::of::<ReflectReference>()], "get")
6161
.map_err(|f| {
6262
InteropError::missing_function(TypeId::of::<ReflectReference>(), f)
6363
})?;
@@ -76,9 +76,10 @@ impl UserData for LuaReflectReference {
7676
let self_: ReflectReference = self_.into();
7777
let key: ScriptValue = key.into();
7878
let value: ScriptValue = value.into();
79+
let type_id = self_.tail_type_id(world.clone())?.or_fake_id();
7980

8081
let func = world
81-
.lookup_function([TypeId::of::<ReflectReference>()], "set")
82+
.lookup_function([type_id, TypeId::of::<ReflectReference>()], "set")
8283
.map_err(|f| {
8384
InteropError::missing_function(TypeId::of::<ReflectReference>(), f)
8485
})?;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
local Resource = world.get_type_by_name("TestResourceWithVariousFields")
2+
local resource = world.get_resource(Resource)
3+
4+
resource.string = "Hello, World!"
5+
resource.bool = true
6+
resource.int = 42
7+
resource.float = 3.0
8+
resource.vec_usize = { 1, 2 }
9+
10+
assert(resource.string == "Hello, World!", "Expected 'Hello, World!', got " .. resource.string)
11+
assert(resource.bool == true, "Expected true, got " .. tostring(resource.bool))
12+
assert(resource.int == 42, "Expected 42, got " .. resource.int)
13+
assert(resource.float == 3.0, "Expected 3.14, got " .. resource.float)
14+
assert(resource.vec_usize[1] == 1, "Expected 1, got " .. resource.vec_usize[1])
15+
16+
resource.string = "Goodbye, World!"
17+
resource.bool = false
18+
resource.int = 24
19+
resource.float = 1.0
20+
resource.vec_usize = { 3, 4 }
21+
22+
assert(resource.string == "Goodbye, World!", "Expected 'Goodbye, World!', got " .. resource.string)
23+
assert(resource.bool == false, "Expected false, got " .. tostring(resource.bool))
24+
assert(resource.int == 24, "Expected 24, got " .. resource.int)
25+
assert(resource.float == 1.0, "Expected 1.41, got " .. resource.float)
26+
assert(resource.vec_usize[1] == 3, "Expected 3, got " .. resource.vec_usize[1])
27+
28+

crates/languages/bevy_mod_scripting_rhai/src/bindings/reference.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -320,9 +320,10 @@ impl CustomType for RhaiReflectReference {
320320
let self_ = self_.0.clone();
321321
let key = ScriptValue::from_dynamic(_index)?;
322322
let value = ScriptValue::from_dynamic(_value)?;
323+
let type_id = self_.tail_type_id(world.clone())?.or_fake_id();
323324

324325
let func = world
325-
.lookup_function([TypeId::of::<ReflectReference>()], "set")
326+
.lookup_function([type_id, TypeId::of::<ReflectReference>()], "set")
326327
.map_err(|f| {
327328
InteropError::missing_function(TypeId::of::<ReflectReference>(), f)
328329
})?;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
let Resource = world.get_type_by_name.call("TestResourceWithVariousFields");
2+
let resource = world.get_resource.call(Resource);
3+
4+
resource.string = "Hello, World!";
5+
resource.bool = true;
6+
resource.int = 42;
7+
resource.float = 3.0;
8+
resource.vec_usize = [ 1, 2 ];
9+
10+
assert(resource.string == "Hello, World!", "Expected 'Hello, World!', got " + resource.string);
11+
assert(resource.bool == true, "Expected true, got " + resource.bool);
12+
assert(resource.int == 42, "Expected 42, got " + resource.int);
13+
assert(resource.float == 3.0, "Expected 3.14, got " + resource.float);
14+
assert(resource.vec_usize[0] == 1, "Expected 1, got " + resource.vec_usize[1]);
15+
16+
resource.string = "Goodbye, World!";
17+
resource.bool = false;
18+
resource.int = 24;
19+
resource.float = 1.0;
20+
resource.vec_usize = [ 3, 4 ];
21+
22+
assert(resource.string == "Goodbye, World!", "Expected 'Goodbye, World!', got " + resource.string);
23+
assert(resource.bool == false, "Expected false, got " + resource.bool);
24+
assert(resource.int == 24, "Expected 24, got " + resource.int);
25+
assert(resource.float == 1.0, "Expected 1.41, got " + resource.float);
26+
assert(resource.vec_usize[0] == 3, "Expected 3, got " + resource.vec_usize[1]);
27+
28+

0 commit comments

Comments
 (0)