From 35f1af522a0c13742f13abf5af04a3963463ad4d Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Wed, 25 Jun 2025 09:10:04 -0700 Subject: [PATCH] Reuse seeded rng in tilemap_chunk for more determinism (#19812) # Objective This example uses a seeded RNG for the initial setup, but new unseeded RNGs during each timed update. This is causing the example to produce different output each time in the [example report](https://bevyengine.github.io/bevy-example-runner/). image ## Solution Store and reuse the RNG, following the pattern used in other examples. ## Testing `cargo run --example tilemap_chunk` --- examples/2d/tilemap_chunk.rs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/examples/2d/tilemap_chunk.rs b/examples/2d/tilemap_chunk.rs index 35c2694ff5..8663b036b1 100644 --- a/examples/2d/tilemap_chunk.rs +++ b/examples/2d/tilemap_chunk.rs @@ -9,7 +9,7 @@ use rand_chacha::ChaCha8Rng; fn main() { App::new() - .add_plugins((DefaultPlugins.set(ImagePlugin::default_nearest()),)) + .add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) .add_systems(Startup, setup) .add_systems(Update, (update_tileset_image, update_tilemap)) .run(); @@ -18,8 +18,14 @@ fn main() { #[derive(Component, Deref, DerefMut)] struct UpdateTimer(Timer); +#[derive(Resource, Deref, DerefMut)] +struct SeededRng(ChaCha8Rng); + fn setup(mut commands: Commands, assets: Res) { + // 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. let mut rng = ChaCha8Rng::seed_from_u64(42); + let chunk_size = UVec2::splat(64); let tile_display_size = UVec2::splat(8); let indices: Vec> = (0..chunk_size.element_product()) @@ -39,6 +45,8 @@ fn setup(mut commands: Commands, assets: Res) { )); commands.spawn(Camera2d); + + commands.insert_resource(SeededRng(rng)); } fn update_tileset_image( @@ -55,12 +63,15 @@ fn update_tileset_image( } } -fn update_tilemap(time: Res