Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Add TimeUpdateStrategy resource for manual Time updating #6159

Closed
wants to merge 12 commits into from
Prev Previous commit
Next Next commit
rename UpdateTime to DesiredTime
  • Loading branch information
ShinySapphic committed Oct 4, 2022
commit 401f5f6d10413aae904573efd8eea7019c31981a
10 changes: 5 additions & 5 deletions crates/bevy_time/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ impl Plugin for TimePlugin {
}
}

/// Resource for updating time with a specific value.
/// Resource for updating time with a desired value.
///
/// See [`update_with_interval`](self::TimeUpdater::update_with_instant) for more details.
#[derive(Resource)]
struct UpdateTime(Instant);
struct DesiredTime(Instant);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this isn't pub?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. I just assumed since it was going to be inserted from the method that it didn't need to be accessed externally. Now that I think about it, there's probably some use cases where you might want to access it externally so I'll go ahead and make it public.


/// Channel resource used to receive time from render world
#[derive(Resource)]
Expand All @@ -70,7 +70,7 @@ pub fn create_time_channels() -> (TimeSender, TimeReceiver) {
/// there to this system through channels. Otherwise the time is updated in this system.
fn time_system(
mut time: ResMut<Time>,
update_time: Option<Res<UpdateTime>>,
wanted_time: Option<Res<DesiredTime>>,
time_recv: Option<Res<TimeReceiver>>,
mut has_received_time: Local<bool>,
) {
Expand All @@ -83,8 +83,8 @@ fn time_system(
warn!("time_system did not receive the time from the render world! Calculations depending on the time may be incorrect.");
}
} else {
if let Some(update_time) = update_time {
time.update_with_instant(update_time.0);
if let Some(wanted_time) = wanted_time {
time.update_with_instant(wanted_time.0);
} else {
time.update();
}
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_time/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy_ecs::{reflect::ReflectResource, system::Resource};
use bevy_reflect::{FromReflect, Reflect};
use bevy_utils::{Duration, Instant};

use crate::UpdateTime;
use crate::DesiredTime;

const SECONDS_PER_HOUR: u64 = 60 * 60;

Expand Down Expand Up @@ -182,9 +182,9 @@ impl TimeUpdater for bevy_app::App {
/// This is ordinarily done automatically, but setting this time value directly can be useful when writing tests,
/// dealing with networking code, or similar.
fn update_with_instant(&mut self, instant: Instant) {
self.world.insert_resource(UpdateTime(instant));
self.world.insert_resource(DesiredTime(instant));
self.update();
self.world.remove_resource::<UpdateTime>();
self.world.remove_resource::<DesiredTime>();
}
}

Expand Down