Skip to content

Commit

Permalink
Changes ScheduleRunnerPlugin RunMode::Loop to run on fixed interval (#…
Browse files Browse the repository at this point in the history
…233)

* Changes ScheduleRunnerPlugin RunMode::Loop to run on fixed interval

* fix formatting
  • Loading branch information
mfrancis107 authored Aug 22, 2020
1 parent 7c3b49c commit 47f3a0b
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions crates/bevy_app/src/schedule_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use crate::{
event::{EventReader, Events},
plugin::Plugin,
};
use std::{thread, time::Duration};
use std::{
thread,
time::{Duration, Instant},
};

/// Determines the method used to run an [App]'s `Schedule`
#[derive(Copy, Clone, Debug)]
Expand Down Expand Up @@ -51,6 +54,8 @@ impl Plugin for ScheduleRunnerPlugin {
app.schedule.run(&mut app.world, &mut app.resources);
}
RunMode::Loop { wait } => loop {
let start_time = Instant::now();

if let Some(app_exit_events) = app.resources.get_mut::<Events<AppExit>>() {
if app_exit_event_reader.latest(&app_exit_events).is_some() {
break;
Expand All @@ -65,8 +70,13 @@ impl Plugin for ScheduleRunnerPlugin {
}
}

let end_time = Instant::now();

if let Some(wait) = wait {
thread::sleep(wait);
let exe_time = end_time - start_time;
if exe_time < wait {
thread::sleep(wait - exe_time);
}
}
},
}
Expand Down

0 comments on commit 47f3a0b

Please sign in to comment.