mirror of
https://github.com/bevyengine/bevy
synced 2024-12-18 17:13:10 +00:00
make example external_source_external_thread deterministic (#16574)
# Objective - make example external_source_external_thread deterministic ## Solution - Don't depend on real time
This commit is contained in:
parent
2309c07e8e
commit
4282c3fe40
1 changed files with 5 additions and 8 deletions
|
@ -5,14 +5,15 @@ use bevy::prelude::*;
|
|||
use crossbeam_channel::{bounded, Receiver};
|
||||
use rand::{Rng, SeedableRng};
|
||||
use rand_chacha::ChaCha8Rng;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
fn main() {
|
||||
App::new()
|
||||
.add_event::<StreamEvent>()
|
||||
.add_plugins(DefaultPlugins)
|
||||
.add_systems(Startup, setup)
|
||||
.add_systems(Update, (read_stream, spawn_text, move_text))
|
||||
.add_systems(Update, (spawn_text, move_text))
|
||||
.add_systems(FixedUpdate, read_stream)
|
||||
.insert_resource(Time::<Fixed>::from_seconds(0.5))
|
||||
.run();
|
||||
}
|
||||
|
||||
|
@ -25,7 +26,7 @@ struct StreamEvent(u32);
|
|||
fn setup(mut commands: Commands) {
|
||||
commands.spawn(Camera2d);
|
||||
|
||||
let (tx, rx) = bounded::<u32>(10);
|
||||
let (tx, rx) = bounded::<u32>(1);
|
||||
std::thread::spawn(move || {
|
||||
// We're seeding the PRNG here to make this example deterministic for testing purposes.
|
||||
// This isn't strictly required in practical use unless you need your app to be deterministic.
|
||||
|
@ -33,12 +34,8 @@ fn setup(mut commands: Commands) {
|
|||
loop {
|
||||
// Everything here happens in another thread
|
||||
// This is where you could connect to an external data source
|
||||
let start_time = Instant::now();
|
||||
let duration = Duration::from_secs_f32(rng.gen_range(0.0..0.2));
|
||||
while start_time.elapsed() < duration {
|
||||
// Spinning for 'duration', simulating doing hard work!
|
||||
}
|
||||
|
||||
// This will block until the previous value has been read in system `read_stream`
|
||||
tx.send(rng.gen_range(0..2000)).unwrap();
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue