Skip to content

Commit f2905c7

Browse files
committed
re-do newindex
1 parent 7743b2e commit f2905c7

File tree

3 files changed

+49
-45
lines changed

3 files changed

+49
-45
lines changed

assets/scripts/bevy_api.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ function on_event()
2525
print(comp.option_usize)
2626
comp.option_usize = nil
2727
print(comp.option_usize)
28-
29-
-- print("\nvec")
28+
world:exit()
29+
print("\nvec")
3030
-- print(table_to_string(comp.vec_of_usize))
31-
-- comp.vec_of_usize = {42,69,72}
31+
comp.vec_of_usize = {42,69,72}
3232
-- comp.vec_of_usize[1] = 0
3333
-- print(comp.vec_of_usize[2])
3434
-- print(table_to_string(comp.vec_of_usize))

crates/bevy_mod_scripting_core/src/bindings/script_val.rs

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -277,11 +277,6 @@ impl FromScriptValue for dyn PartialReflect {
277277
world: WorldGuard,
278278
target_type_id: TypeId,
279279
) -> Option<ScriptResult<Box<dyn PartialReflect>>> {
280-
println!(
281-
"type: {:?}",
282-
target_type_id.display_with_world(world.clone())
283-
);
284-
285280
match target_type_id {
286281
// TODO: if these types ever support reflect, we can uncomment these lines
287282
// For some of these we specifically require the borrowed static variant, this will never let you use a dynamically created string from the script
@@ -344,7 +339,6 @@ impl FromScriptValue for dyn PartialReflect {
344339
return u128::from_script_value(value, world, target_type_id)
345340
}
346341
t if t == TypeId::of::<usize>() => {
347-
println!("usize {:?}", value);
348342
return usize::from_script_value(value, world, target_type_id);
349343
}
350344
// t if t == TypeId::of::<Box<str>>() => return <Box<str>>::from_script_value(value, world, target_type_id),
@@ -1451,24 +1445,6 @@ mod test {
14511445
.unwrap()
14521446
.reflect_partial_eq(&"hello")
14531447
.unwrap());
1454-
1455-
println!(
1456-
"{:?}",
1457-
<dyn PartialReflect>::from_script_value(
1458-
ScriptValue::Reference(option_reference.clone()),
1459-
guard.clone(),
1460-
TypeId::of::<Option<usize>>()
1461-
)
1462-
);
1463-
1464-
println!(
1465-
"heL: {:?}",
1466-
<dyn PartialReflect>::from_script_value(
1467-
ScriptValue::Reference(usize_reference.clone()),
1468-
guard.clone(),
1469-
TypeId::of::<Option<usize>>()
1470-
)
1471-
);
14721448
assert!(<dyn PartialReflect>::from_script_value(
14731449
ScriptValue::Reference(usize_reference.clone()),
14741450
guard.clone(),
@@ -1499,14 +1475,6 @@ mod test {
14991475
.reflect_partial_eq(&Some(Some(2usize)))
15001476
.unwrap());
15011477

1502-
println!(
1503-
"heL: {:?}",
1504-
<dyn PartialReflect>::from_script_value(
1505-
ScriptValue::Unit,
1506-
guard.clone(),
1507-
TypeId::of::<Option<Option<usize>>>()
1508-
)
1509-
);
15101478
assert!(<dyn PartialReflect>::from_script_value(
15111479
ScriptValue::Unit,
15121480
guard.clone(),
@@ -1553,14 +1521,6 @@ mod test {
15531521
.reflect_partial_eq(&Some("hello".to_string()))
15541522
.unwrap());
15551523

1556-
println!(
1557-
"{:?}",
1558-
<dyn PartialReflect>::from_script_value(
1559-
ScriptValue::Unit,
1560-
guard.clone(),
1561-
TypeId::of::<Option<String>>()
1562-
)
1563-
);
15641524
assert!(<dyn PartialReflect>::from_script_value(
15651525
ScriptValue::Unit,
15661526
guard.clone(),

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use bevy_mod_scripting_core::{
2020
DeferredReflection, ReflectAllocator, ReflectRefIter, ReflectReference, ReflectionPathElem,
2121
TypeIdSource, WorldCallbackAccess,
2222
},
23-
error::{FunctionError, ScriptError, ScriptResult},
23+
error::{FunctionError, ScriptError, ScriptResult, ValueConversionError},
2424
new_deferred_reflection,
2525
reflection_extensions::{PartialReflectExt, TypeIdExtensions},
2626
Either,
@@ -318,11 +318,55 @@ impl UserData for LuaReflectReference {
318318
let mut reflect_path: ReflectionPathElem = key.try_into()?;
319319
reflect_path.convert_to_0_indexed();
320320
self_.index_path(reflect_path);
321-
let script_value = self_.into_script_value(world)?;
321+
let script_value = self_.with_reflect(world.clone(), |r| {
322+
<&dyn PartialReflect>::into_script_value(r, world.clone())
323+
})??;
322324
LuaScriptValue::from(script_value).into_lua(lua)
323325
},
324326
);
325327

328+
m.add_meta_function(
329+
MetaMethod::NewIndex,
330+
|lua, (self_, key, value): (LuaReflectReference, LuaScriptValue, LuaScriptValue)| {
331+
let mut self_: ReflectReference = self_.into();
332+
let key: ScriptValue = key.into();
333+
let value: ScriptValue = value.into();
334+
let mut reflect_path: ReflectionPathElem = key.try_into()?;
335+
reflect_path.convert_to_0_indexed();
336+
337+
let world = lua.get_world();
338+
339+
self_.index_path(reflect_path);
340+
self_.with_reflect_mut(world.clone(), |r| {
341+
let target_type_id = r
342+
.get_represented_type_info()
343+
.map(|i| i.type_id())
344+
.type_id_or_fake_id();
345+
let other = <dyn PartialReflect>::from_script_value(
346+
value,
347+
world.clone(),
348+
target_type_id,
349+
)
350+
.transpose()?
351+
.ok_or_else(|| ValueConversionError::TypeMismatch {
352+
expected_type: target_type_id.display_with_world(world.clone()).into(),
353+
actual_type: Some(
354+
r.get_represented_type_info()
355+
.map(|i| i.type_id())
356+
.type_id_or_fake_id()
357+
.display_with_world(world.clone())
358+
.into(),
359+
),
360+
})?;
361+
362+
r.try_apply(other.as_partial_reflect())?;
363+
Ok::<_, ScriptError>(())
364+
})??;
365+
366+
Ok(())
367+
},
368+
);
369+
326370
// m.add_meta_function(
327371
// MetaMethod::Index,
328372
// |l, (mut self_, key): (LuaReflectReference, Value)| {

0 commit comments

Comments
 (0)