Confused about the timing of event updates and consumption. #12362
-
I am trying the following code to learn about the use bevy::prelude::*;
use std::io::{self, BufRead};
use std::time::Duration;
fn runner(mut app: App) {
let stdin = io::stdin();
for line in stdin.lock().lines() {
if let Err(err) = line {
println!("read err: {:#}", err);
break;
}
match line.unwrap().as_str() {
"" => {
app.update();
}
"u" => {
println!("UNPAUSE: resuming virtual clock");
app.world.resource_mut::<Time<Virtual>>().unpause();
}
_ => {
println!("QUITTING!");
break;
}
}
}
}
fn print_real_time(time: Res<Time<Real>>) {
println!(
"PreUpdate: this is real time clock, delta is {:?} and elapsed is {:?}",
time.delta(),
time.elapsed()
);
}
fn print_fixed_time(time: Res<Time>) {
println!(
"FixedUpdate: this is generic time clock inside fixed, delta is {:?} and elapsed is {:?}",
time.delta(),
time.elapsed()
);
}
fn print_time(time: Res<Time>) {
println!(
"Update: this is generic time clock, delta is {:?} and elapsed is {:?}",
time.delta(),
time.elapsed()
);
}
fn main() {
App::new()
.add_plugins(MinimalPlugins)
.insert_resource(Time::<Virtual>::from_max_delta(Duration::from_secs(5)))
.insert_resource(Time::<Fixed>::from_duration(Duration::from_secs(1)))
.insert_resource(Counter(0))
.add_event::<MyEvent>()
.add_systems(PreUpdate, print_real_time)
.add_systems(FixedUpdate, (
print_fixed_time,
send_event,
read_event
).chain())
.add_systems(Update, print_time)
.set_runner(runner)
.run();
}
#[derive(Event, Default)]
struct MyEvent;
#[derive(Resource)]
struct Counter(i32);
fn send_event(mut events: EventWriter<MyEvent>, mut counter: ResMut<Counter>) {
if counter.0 == 0 {
counter.0 = 1;
events.send_default();
}
}
fn read_event(events: EventReader<MyEvent>) {
println!("events.len: {:?}", events.len());
} According to my current understanding, the old event queue should be cleared after each call to
There are two On the other hand, if I don't trigger
The event queue is cleared after the second Hope someone could shed some light on this for me. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Events are buffered until both |
Beta Was this translation helpful? Give feedback.
Events are buffered until both
FixedUpdate
andUpdate
run twice, to give the chance to see them to all systems. See #10077 for the PR that added this behaviour.