Skip to content

Commit a223c1a

Browse files
authored
Make it easier to write tests involving fixed timesteps (#21705)
# Objective - For running tests involving fixed timesteps, I want to advance the time so that every call to `app.update()` triggers a fixed update - Right now, the most precise way to do that to my knowledge is ```rust app.insert_resource(TimeUpdateStrategy::ManualDuration(Time::<Fixed>::default().timestep())) ``` - But this is a bit clumsy, and also doesn't work when the fixed timestep isn't at the default. Some more boilerplate would be needed in the test setup to deal with that: ```rust app.insert_resource(TimeUpdateStrategy::ManualDuration(app.world().resource::<Time<Fixed>>().timestep())) ``` - Good luck adapting this for the case when the fixed timestep ever changes at runtime lol ## Solution - Create an alternative strategy called `TimeUpdateStrategy::FixedTimesteps` that advances time by the fixed timestep automatically: ```rust // Now `app.update()` will run the fixed loop exactly once! app.insert_resource(TimeUpdateStrategy::FixedTimesteps(1)) ``` ## Testing - None, as this is trivial
1 parent ae60d84 commit a223c1a

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

crates/bevy_time/src/fixed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{time::Time, virt::Virtual};
1010
///
1111
/// A specialization of the [`Time`] structure. **For method documentation, see
1212
/// [`Time<Fixed>#impl-Time<Fixed>`].**
13-
///
13+
///
1414
/// It is automatically inserted as a resource by
1515
/// [`TimePlugin`](crate::TimePlugin) and updated based on
1616
/// [`Time<Virtual>`](Virtual). The fixed clock is automatically set as the

crates/bevy_time/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ pub enum TimeUpdateStrategy {
118118
ManualInstant(Instant),
119119
/// [`Time`] will be incremented by the specified [`Duration`] each frame.
120120
ManualDuration(Duration),
121+
/// [`Time`] will be incremented by the fixed timestep each frame, multiplied by the specified factor `n`.
122+
/// This means that a call to [`App::update`] will always run the fixed loop exactly n times.
123+
FixedTimesteps(u32),
121124
}
122125

123126
/// Channel resource used to receive time from the render world.
@@ -144,6 +147,7 @@ pub fn create_time_channels() -> (TimeSender, TimeReceiver) {
144147
pub fn time_system(
145148
mut real_time: ResMut<Time<Real>>,
146149
mut virtual_time: ResMut<Time<Virtual>>,
150+
fixed_time: Res<Time<Fixed>>,
147151
mut time: ResMut<Time>,
148152
update_strategy: Res<TimeUpdateStrategy>,
149153
#[cfg(feature = "std")] time_recv: Option<Res<TimeReceiver>>,
@@ -175,6 +179,9 @@ pub fn time_system(
175179
}
176180
TimeUpdateStrategy::ManualInstant(instant) => real_time.update_with_instant(*instant),
177181
TimeUpdateStrategy::ManualDuration(duration) => real_time.update_with_duration(*duration),
182+
TimeUpdateStrategy::FixedTimesteps(factor) => {
183+
real_time.update_with_duration(fixed_time.timestep() * *factor);
184+
}
178185
}
179186

180187
update_virtual_time(&mut time, &mut virtual_time, &real_time);

0 commit comments

Comments
 (0)