|
1 |
| -use super::script_value::{LuaScriptValue, LUA_CALLER_CONTEXT}; |
| 1 | +use std::any::TypeId; |
| 2 | + |
2 | 3 | use bevy_mod_scripting_core::{
|
3 | 4 | bindings::{
|
4 | 5 | pretty_print::DisplayWithWorld, script_value::ScriptValue, ReflectReference,
|
5 | 6 | ThreadWorldContainer, WorldContainer,
|
6 | 7 | },
|
7 | 8 | error::InteropError,
|
8 |
| - reflection_extensions::TypeIdExtensions, |
| 9 | + reflection_extensions::{TypeIdExtensions, TypeInfoExtensions}, |
9 | 10 | };
|
10 | 11 | use mlua::{MetaMethod, UserData, UserDataMethods};
|
11 |
| -use std::any::TypeId; |
| 12 | + |
| 13 | +use super::script_value::{LuaScriptValue, LUA_CALLER_CONTEXT}; |
12 | 14 |
|
13 | 15 | /// Lua UserData wrapper for [`bevy_mod_scripting_core::bindings::ReflectReference`].
|
14 | 16 | /// Acts as a lua reflection interface. Any value which is registered in the type registry can be interacted with using this type.
|
@@ -79,13 +81,24 @@ impl UserData for LuaReflectReference {
|
79 | 81 | let key: ScriptValue = key.into();
|
80 | 82 | let value: ScriptValue = value.into();
|
81 | 83 | let type_id = self_.tail_type_id(world.clone())?.or_fake_id();
|
82 |
| - |
| 84 | + |
| 85 | + let func_name = { |
| 86 | + let type_registry = world.type_registry(); |
| 87 | + let type_registry = type_registry.read(); |
| 88 | + let type_info = type_registry.get_type_info(type_id).ok_or_else(|| { |
| 89 | + InteropError::missing_type_data( |
| 90 | + type_id, |
| 91 | + "Type was not registered, could not determine conversion strategy." |
| 92 | + .to_owned(), |
| 93 | + ) |
| 94 | + })?; |
| 95 | + if type_info.is_map() { "insert" } else { "set" } |
| 96 | + }; |
83 | 97 | let func = world
|
84 |
| - .lookup_function([type_id, TypeId::of::<ReflectReference>()], "set") |
| 98 | + .lookup_function([type_id, TypeId::of::<ReflectReference>()], func_name) |
85 | 99 | .map_err(|f| {
|
86 | 100 | InteropError::missing_function(TypeId::of::<ReflectReference>(), f)
|
87 | 101 | })?;
|
88 |
| - |
89 | 102 | let out = func.call(
|
90 | 103 | vec![ScriptValue::Reference(self_), key, value],
|
91 | 104 | LUA_CALLER_CONTEXT,
|
|
0 commit comments