Skip to content

Commit 78c6efa

Browse files
committed
set maps via default_set
1 parent bb789b9 commit 78c6efa

File tree

6 files changed

+79
-17
lines changed

6 files changed

+79
-17
lines changed
File renamed without changes.
File renamed without changes.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
local Resource = world.get_type_by_name("TestResourceWithVariousFields")
2+
local resource = world.get_resource(Resource)
3+
4+
resource.string_map["foo"] = "updated"
5+
resource.string_map["new_key"] = "new_value"
6+
7+
local resource_changed = world.get_resource(Resource)
8+
assert(resource_changed.string_map["foo"] == "updated", "Expected updated, got " .. resource_changed.string_map["foo"])
9+
assert(resource_changed.string_map["new_key"] == "new_value",
10+
"Expected new_value, got " .. resource_changed.string_map["new_key"])
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let Resource = world.get_type_by_name.call("TestResourceWithVariousFields");
2+
let resource = world.get_resource.call(Resource);
3+
4+
5+
resource.string_map["foo"] = "updated";
6+
resource.string_map["new_key"] = "new_value";
7+
8+
let resource_changed = world.get_resource.call(Resource);
9+
assert(resource_changed.string_map["foo"] == "updated", "Expected updated, got " + resource_changed.string_map["foo"]);
10+
assert(resource_changed.string_map["new_key"] == "new_value", "Expected new_value, got " + resource_changed.string_map["new_key"]);

crates/bevy_mod_scripting_bindings/src/function/magic_functions.rs

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,22 +133,66 @@ impl MagicFunctions {
133133
value: ScriptValue,
134134
) -> Result<(), InteropError> {
135135
let world = ctxt.world()?;
136-
let mut path: ParsedPath = key.try_into()?;
137-
if ctxt.convert_to_0_indexed() {
138-
path.convert_to_0_indexed();
136+
137+
// Check if the reference is a map type
138+
let is_map = reference.with_reflect(world.clone(), |r| {
139+
matches!(r.reflect_ref(), ReflectRef::Map(_))
140+
})?;
141+
142+
if is_map {
143+
// Handle map setting specially - need to get the key type and convert the script value
144+
let key = <Box<dyn PartialReflect>>::from_script_ref(
145+
reference.key_type_id(world.clone())?.ok_or_else(|| {
146+
InteropError::unsupported_operation(
147+
reference.tail_type_id(world.clone()).unwrap_or_default(),
148+
Some(Box::new(key.clone())),
149+
"Could not get key type id. Are you trying to index into a type that's not a map?".to_owned(),
150+
)
151+
})?,
152+
key,
153+
world.clone(),
154+
)?;
155+
156+
// Get the value type for the map and convert the script value
157+
let value_type_id = reference.element_type_id(world.clone())?.ok_or_else(|| {
158+
InteropError::unsupported_operation(
159+
reference.tail_type_id(world.clone()).unwrap_or_default(),
160+
Some(Box::new(value.clone())),
161+
"Could not get value type id. Are you trying to set a value in a type that's not a map?".to_owned(),
162+
)
163+
})?;
164+
165+
let value = <Box<dyn PartialReflect>>::from_script_ref(
166+
value_type_id,
167+
value,
168+
world.clone(),
169+
)?;
170+
171+
reference.with_reflect_mut(world, |s| {
172+
s.try_insert_boxed(key, value)
173+
})??;
174+
} else {
175+
let mut path: ParsedPath = key.try_into()?;
176+
if ctxt.convert_to_0_indexed() {
177+
path.convert_to_0_indexed();
178+
}
179+
reference.index_path(path);
180+
181+
let target_type_id = reference.with_reflect(world.clone(), |r| {
182+
r.get_represented_type_info()
183+
.map(|i| i.type_id())
184+
.or_fake_id()
185+
})?;
186+
187+
let other = <Box<dyn PartialReflect>>::from_script_ref(target_type_id, value, world.clone())?;
188+
189+
reference.with_reflect_mut(world, |r| {
190+
r.try_apply(other.as_partial_reflect())
191+
.map_err(InteropError::reflect_apply_error)
192+
})??;
139193
}
140-
reference.index_path(path);
141-
reference.with_reflect_mut(world.clone(), |r| {
142-
let target_type_id = r
143-
.get_represented_type_info()
144-
.map(|i| i.type_id())
145-
.or_fake_id();
146-
let other =
147-
<Box<dyn PartialReflect>>::from_script_ref(target_type_id, value, world.clone())?;
148-
r.try_apply(other.as_partial_reflect())
149-
.map_err(InteropError::reflect_apply_error)?;
150-
Ok::<_, InteropError>(())
151-
})?
194+
195+
Ok(())
152196
}
153197
}
154198

rust-toolchain.toml

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)