use bevy_ecs::prelude::*;
fn main() {
let mut world = World::new();
world.insert_resource(Events::<MyEvent>::default());
let mut schedule = Schedule::default();
#[derive(StageLabel)]
enum Stages {
First,
Second,
}
let mut first = SystemStage::parallel();
first.add_system(Events::<MyEvent>::update_system);
schedule.add_stage(Stages::First, first);
let mut second = SystemStage::parallel();
second.add_system(sending_system);
second.add_system(receiving_system.after(sending_system));
schedule.add_stage_after(Stages::First, Stages::Second, second);
for iteration in 1..=10 {
println!("Simulating frame {iteration}/10");
schedule.run(&mut world);
}
}
struct MyEvent {
pub message: String,
pub random_value: f32,
}
fn sending_system(mut event_writer: EventWriter<MyEvent>) {
let random_value: f32 = rand::random();
if random_value > 0.5 {
event_writer.send(MyEvent {
message: "A random event with value > 0.5".to_string(),
random_value,
});
}
}
fn receiving_system(mut event_reader: EventReader<MyEvent>) {
for my_event in event_reader.iter() {
println!(
" Received message {:?}, with random value of {}",
my_event.message, my_event.random_value
);
}
}