@@ -788,6 +788,46 @@ impl ReflectRefIter {
788788 } ;
789789 ( next, index)
790790 }
791+
792+ /// Returns the next element as a cloned value using `from_reflect`.
793+ /// Returns a fully reflected value (Box<dyn Reflect>) instead of a reference.
794+ /// Returns Ok(None) when the path is invalid (end of iteration).
795+ pub fn next_cloned ( & mut self , world : WorldGuard ) -> Result < Option < ReflectReference > , InteropError > {
796+ let index = match & mut self . index {
797+ IterationKey :: Index ( i) => {
798+ let idx = * i;
799+ * i += 1 ;
800+ idx
801+ }
802+ } ;
803+
804+ let element = self . base . with_reflect ( world. clone ( ) , |base_reflect| {
805+ match base_reflect. reflect_ref ( ) {
806+ bevy_reflect:: ReflectRef :: List ( list) => {
807+ list. get ( index) . map ( |item| {
808+ <dyn PartialReflect >:: from_reflect ( item, world. clone ( ) )
809+ } )
810+ }
811+ bevy_reflect:: ReflectRef :: Array ( array) => {
812+ array. get ( index) . map ( |item| {
813+ <dyn PartialReflect >:: from_reflect ( item, world. clone ( ) )
814+ } )
815+ }
816+ _ => None ,
817+ }
818+ } ) ?;
819+
820+ match element {
821+ Some ( result) => {
822+ let owned_value = result?;
823+ let allocator = world. allocator ( ) ;
824+ let mut allocator_guard = allocator. write ( ) ;
825+ let value_ref = ReflectReference :: new_allocated_boxed ( owned_value, & mut * allocator_guard) ;
826+ Ok ( Some ( value_ref) )
827+ }
828+ None => Ok ( None ) ,
829+ }
830+ }
791831}
792832
793833const fn list_index_access ( index : usize ) -> Access < ' static > {
@@ -842,6 +882,49 @@ impl ReflectMapRefIter {
842882 }
843883 } ) ?
844884 }
885+
886+ /// Returns the next map entry as a (key, value) tuple, cloning the values.
887+ /// Returns Ok(None) when there are no more entries.
888+ /// This uses `from_reflect` to clone the actual values instead of creating dynamic references.
889+ /// Returns fully reflected values (Box<dyn Reflect>) like `map_get_clone` does.
890+ pub fn next_cloned ( & mut self , world : WorldGuard ) -> Result < Option < ( ReflectReference , ReflectReference ) > , InteropError > {
891+ let idx = self . index ;
892+ self . index += 1 ;
893+
894+ // Access the map and get the entry at index
895+ self . base . with_reflect ( world. clone ( ) , |reflect| {
896+ match reflect. reflect_ref ( ) {
897+ ReflectRef :: Map ( map) => {
898+ if let Some ( ( key, value) ) = map. get_at ( idx) {
899+ let allocator = world. allocator ( ) ;
900+ let mut allocator_guard = allocator. write ( ) ;
901+
902+ let owned_key = <dyn PartialReflect >:: from_reflect ( key, world. clone ( ) ) ?;
903+ let key_ref = ReflectReference :: new_allocated_boxed (
904+ owned_key,
905+ & mut * allocator_guard
906+ ) ;
907+
908+ let owned_value = <dyn PartialReflect >:: from_reflect ( value, world. clone ( ) ) ?;
909+ let value_ref = ReflectReference :: new_allocated_boxed (
910+ owned_value,
911+ & mut * allocator_guard
912+ ) ;
913+
914+ drop ( allocator_guard) ;
915+ Ok ( Some ( ( key_ref, value_ref) ) )
916+ } else {
917+ Ok ( None )
918+ }
919+ }
920+ _ => Err ( InteropError :: unsupported_operation (
921+ reflect. get_represented_type_info ( ) . map ( |ti| ti. type_id ( ) ) ,
922+ None ,
923+ "map iteration on non-map type" . to_owned ( ) ,
924+ ) )
925+ }
926+ } ) ?
927+ }
845928}
846929
847930#[ profiling:: all_functions]
0 commit comments