@@ -133,22 +133,66 @@ impl MagicFunctions {
133
133
value : ScriptValue ,
134
134
) -> Result < ( ) , InteropError > {
135
135
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
+ } ) ??;
139
193
}
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 ( ( ) )
152
196
}
153
197
}
154
198
0 commit comments