@@ -496,11 +496,11 @@ pub mod common_conditions {
496
496
event:: { Event , EventReader } ,
497
497
prelude:: { Component , Query , With } ,
498
498
removal_detection:: RemovedComponents ,
499
- system:: { IntoSystem , Res , Resource , System } ,
499
+ system:: { IntoSystem , Local , Res , Resource , System } ,
500
500
} ;
501
501
502
- /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true`
503
- /// if the first time the condition is run and false every time after
502
+ /// A [`Condition`](super::Condition)-satisfying system that returns `true`
503
+ /// on the first time the condition is run and false every time after.
504
504
///
505
505
/// # Example
506
506
///
@@ -513,7 +513,7 @@ pub mod common_conditions {
513
513
/// # world.init_resource::<Counter>();
514
514
/// app.add_systems(
515
515
/// // `run_once` will only return true the first time it's evaluated
516
- /// my_system.run_if(run_once() ),
516
+ /// my_system.run_if(run_once),
517
517
/// );
518
518
///
519
519
/// fn my_system(mut counter: ResMut<Counter>) {
@@ -528,15 +528,12 @@ pub mod common_conditions {
528
528
/// app.run(&mut world);
529
529
/// assert_eq!(world.resource::<Counter>().0, 1);
530
530
/// ```
531
- pub fn run_once ( ) -> impl FnMut ( ) -> bool + Clone {
532
- let mut has_run = false ;
533
- move || {
534
- if !has_run {
535
- has_run = true ;
536
- true
537
- } else {
538
- false
539
- }
531
+ pub fn run_once ( mut has_run : Local < bool > ) -> bool {
532
+ if !* has_run {
533
+ * has_run = true ;
534
+ true
535
+ } else {
536
+ false
540
537
}
541
538
}
542
539
@@ -815,7 +812,7 @@ pub mod common_conditions {
815
812
}
816
813
}
817
814
818
- /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true`
815
+ /// A [`Condition`](super::Condition)-satisfying system that returns `true`
819
816
/// if the resource of the given type has had its value changed since the condition
820
817
/// was last checked.
821
818
///
@@ -841,7 +838,7 @@ pub mod common_conditions {
841
838
/// // `resource_changed_or_removed` will only return true if the
842
839
/// // given resource was just changed or removed (or added)
843
840
/// my_system.run_if(
844
- /// resource_changed_or_removed::<Counter>()
841
+ /// resource_changed_or_removed::<Counter>
845
842
/// // By default detecting changes will also trigger if the resource was
846
843
/// // just added, this won't work with my example so I will add a second
847
844
/// // condition to make sure the resource wasn't just added
@@ -877,25 +874,22 @@ pub mod common_conditions {
877
874
/// app.run(&mut world);
878
875
/// assert_eq!(world.contains_resource::<MyResource>(), true);
879
876
/// ```
880
- pub fn resource_changed_or_removed < T > ( ) -> impl FnMut ( Option < Res < T > > ) -> bool + Clone
877
+ pub fn resource_changed_or_removed < T > ( res : Option < Res < T > > , mut existed : Local < bool > ) -> bool
881
878
where
882
879
T : Resource ,
883
880
{
884
- let mut existed = false ;
885
- move |res : Option < Res < T > > | {
886
- if let Some ( value) = res {
887
- existed = true ;
888
- value. is_changed ( )
889
- } else if existed {
890
- existed = false ;
891
- true
892
- } else {
893
- false
894
- }
881
+ if let Some ( value) = res {
882
+ * existed = true ;
883
+ value. is_changed ( )
884
+ } else if * existed {
885
+ * existed = false ;
886
+ true
887
+ } else {
888
+ false
895
889
}
896
890
}
897
891
898
- /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true`
892
+ /// A [`Condition`](super::Condition)-satisfying system that returns `true`
899
893
/// if the resource of the given type has been removed since the condition was last checked.
900
894
///
901
895
/// # Example
@@ -910,7 +904,7 @@ pub mod common_conditions {
910
904
/// app.add_systems(
911
905
/// // `resource_removed` will only return true if the
912
906
/// // given resource was just removed
913
- /// my_system.run_if(resource_removed::<MyResource>() ),
907
+ /// my_system.run_if(resource_removed::<MyResource>),
914
908
/// );
915
909
///
916
910
/// #[derive(Resource, Default)]
@@ -932,25 +926,22 @@ pub mod common_conditions {
932
926
/// app.run(&mut world);
933
927
/// assert_eq!(world.resource::<Counter>().0, 1);
934
928
/// ```
935
- pub fn resource_removed < T > ( ) -> impl FnMut ( Option < Res < T > > ) -> bool + Clone
929
+ pub fn resource_removed < T > ( res : Option < Res < T > > , mut existed : Local < bool > ) -> bool
936
930
where
937
931
T : Resource ,
938
932
{
939
- let mut existed = false ;
940
- move |res : Option < Res < T > > | {
941
- if res. is_some ( ) {
942
- existed = true ;
943
- false
944
- } else if existed {
945
- existed = false ;
946
- true
947
- } else {
948
- false
949
- }
933
+ if res. is_some ( ) {
934
+ * existed = true ;
935
+ false
936
+ } else if * existed {
937
+ * existed = false ;
938
+ true
939
+ } else {
940
+ false
950
941
}
951
942
}
952
943
953
- /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true`
944
+ /// A [`Condition`](super::Condition)-satisfying system that returns `true`
954
945
/// if there are any new events of the given type since it was last called.
955
946
///
956
947
/// # Example
@@ -966,7 +957,7 @@ pub mod common_conditions {
966
957
/// # app.add_systems(bevy_ecs::event::event_update_system.before(my_system));
967
958
///
968
959
/// app.add_systems(
969
- /// my_system.run_if(on_event::<MyEvent>() ),
960
+ /// my_system.run_if(on_event::<MyEvent>),
970
961
/// );
971
962
///
972
963
/// #[derive(Event)]
@@ -986,12 +977,12 @@ pub mod common_conditions {
986
977
/// app.run(&mut world);
987
978
/// assert_eq!(world.resource::<Counter>().0, 1);
988
979
/// ```
989
- pub fn on_event < T : Event > ( ) -> impl FnMut ( EventReader < T > ) -> bool + Clone {
980
+ pub fn on_event < T : Event > ( mut reader : EventReader < T > ) -> bool {
990
981
// The events need to be consumed, so that there are no false positives on subsequent
991
982
// calls of the run condition. Simply checking `is_empty` would not be enough.
992
983
// PERF: note that `count` is efficient (not actually looping/iterating),
993
984
// due to Bevy having a specialized implementation for events.
994
- move | mut reader : EventReader < T > | reader. read ( ) . count ( ) > 0
985
+ reader. read ( ) . count ( ) > 0
995
986
}
996
987
997
988
/// A [`Condition`](super::Condition)-satisfying system that returns `true`
@@ -1031,15 +1022,15 @@ pub mod common_conditions {
1031
1022
!query. is_empty ( )
1032
1023
}
1033
1024
1034
- /// Generates a [`Condition`](super::Condition)-satisfying closure that returns `true`
1025
+ /// A [`Condition`](super::Condition)-satisfying system that returns `true`
1035
1026
/// if there are any entity with a component of the given type removed.
1036
- pub fn any_component_removed < T : Component > ( ) -> impl FnMut ( RemovedComponents < T > ) -> bool {
1027
+ pub fn any_component_removed < T : Component > ( mut removals : RemovedComponents < T > ) -> bool {
1037
1028
// `RemovedComponents` based on events and therefore events need to be consumed,
1038
1029
// so that there are no false positives on subsequent calls of the run condition.
1039
1030
// Simply checking `is_empty` would not be enough.
1040
1031
// PERF: note that `count` is efficient (not actually looping/iterating),
1041
1032
// due to Bevy having a specialized implementation for events.
1042
- move | mut removals : RemovedComponents < T > | removals . read ( ) . count ( ) != 0
1033
+ removals. read ( ) . count ( ) > 0
1043
1034
}
1044
1035
1045
1036
/// Generates a [`Condition`](super::Condition) that inverses the result of passed one.
@@ -1384,16 +1375,16 @@ mod tests {
1384
1375
fn distributive_run_if_compiles ( ) {
1385
1376
Schedule :: default ( ) . add_systems (
1386
1377
( test_system, test_system)
1387
- . distributive_run_if ( run_once ( ) )
1378
+ . distributive_run_if ( run_once)
1388
1379
. distributive_run_if ( resource_exists :: < TestResource > )
1389
1380
. distributive_run_if ( resource_added :: < TestResource > )
1390
1381
. distributive_run_if ( resource_changed :: < TestResource > )
1391
1382
. distributive_run_if ( resource_exists_and_changed :: < TestResource > )
1392
- . distributive_run_if ( resource_changed_or_removed :: < TestResource > ( ) )
1393
- . distributive_run_if ( resource_removed :: < TestResource > ( ) )
1394
- . distributive_run_if ( on_event :: < TestEvent > ( ) )
1383
+ . distributive_run_if ( resource_changed_or_removed :: < TestResource > )
1384
+ . distributive_run_if ( resource_removed :: < TestResource > )
1385
+ . distributive_run_if ( on_event :: < TestEvent > )
1395
1386
. distributive_run_if ( any_with_component :: < TestComponent > )
1396
- . distributive_run_if ( not ( run_once ( ) ) ) ,
1387
+ . distributive_run_if ( not ( run_once) ) ,
1397
1388
) ;
1398
1389
}
1399
1390
}
0 commit comments