Skip to content

Commit 7a39963

Browse files
authored
feat: add ScriptValue override for printing opaque values (#380)
# Summary - Means printing reflect types containing script values will print them nicer
1 parent a6a989c commit 7a39963

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

crates/bevy_mod_scripting_core/src/bindings/pretty_print.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ impl ReflectReferencePrinter {
174174
}
175175
id if id == TypeId::of::<char>() => downcast_case!(v, output, char),
176176
id if id == TypeId::of::<bool>() => downcast_case!(v, output, bool),
177+
id if id == TypeId::of::<ScriptValue>() => downcast_case!(v, output, ScriptValue),
177178
_ => {
178179
output.push_str(
179180
v.get_represented_type_info()
@@ -654,7 +655,7 @@ impl<K: DisplayWithWorld + 'static, V: DisplayWithWorld + 'static> DisplayWithWo
654655

655656
#[cfg(test)]
656657
mod test {
657-
use bevy::prelude::AppTypeRegistry;
658+
use bevy::{prelude::AppTypeRegistry, reflect::Reflect};
658659

659660
use crate::bindings::{
660661
function::script_function::AppScriptFunctionRegistry, AppReflectAllocator,
@@ -778,4 +779,29 @@ mod test {
778779

779780
assert_eq!(map.display_value_with_world(world.clone()), "{hello: true}");
780781
}
782+
783+
#[test]
784+
fn test_script_value_in_reference() {
785+
let mut world = setup_world();
786+
let world = WorldGuard::new_exclusive(&mut world);
787+
788+
#[derive(Reflect)]
789+
struct Test {
790+
val: ScriptValue,
791+
}
792+
793+
let test = Test {
794+
val: ScriptValue::Bool(true),
795+
};
796+
797+
let allocator = world.allocator();
798+
let mut allocator_write = allocator.write();
799+
800+
let reflect_reference = ReflectReference::new_allocated(test, &mut allocator_write);
801+
drop(allocator_write);
802+
assert_eq!(
803+
reflect_reference.display_value_with_world(world.clone()),
804+
"{val: Reflect(ScriptValue(Bool(true)))}"
805+
);
806+
}
781807
}

0 commit comments

Comments
 (0)