Skip to content

Commit

Permalink
Send SceneInstanceReady only once per scene
Browse files Browse the repository at this point in the history
  • Loading branch information
daxpedda committed Dec 17, 2023
1 parent 746361b commit 23afd0e
Showing 1 changed file with 56 additions and 2 deletions.
58 changes: 56 additions & 2 deletions crates/bevy_scene/src/scene_spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,10 @@ impl SceneSpawner {
child: entity,
}
.apply(world);

world.send_event(SceneInstanceReady { parent });
}
}

world.send_event(SceneInstanceReady { parent });
} else {
self.scenes_with_parent.push((instance_id, parent));
}
Expand Down Expand Up @@ -434,3 +434,57 @@ pub fn scene_spawner_system(world: &mut World) {
scene_spawner.set_scene_instance_parent_sync(world);
});
}

#[cfg(test)]
mod tests {
use bevy_app::App;
use bevy_asset::{AssetPlugin, AssetServer};
use bevy_ecs::{
component::Component,
event::EventReader,
reflect::ReflectComponent,
system::{Commands, Res, ResMut, RunSystemOnce},
world::World,
};

use bevy_reflect::Reflect;

use crate::{DynamicScene, SceneInstanceReady, ScenePlugin, SceneSpawner};

#[derive(Component, Reflect, Default)]
#[reflect(Component)]
struct ComponentA;

#[test]
fn event() {
let mut app = App::new();
app.add_plugins((AssetPlugin::default(), ScenePlugin));

app.register_type::<ComponentA>();
app.world.spawn(ComponentA);
app.world.spawn(ComponentA);

// Build scene.
let scene =
app.world
.run_system_once(|world: &World, asset_server: Res<'_, AssetServer>| {
asset_server.add(DynamicScene::from_world(world))
});

// Spawn scene.
app.world.run_system_once(
move |mut commands: Commands<'_, '_>, mut scene_spawner: ResMut<'_, SceneSpawner>| {
let scene_entity = commands.spawn_empty();
scene_spawner.spawn_dynamic_as_child(scene.clone(), scene_entity.id());
},
);

app.update();

app.world.run_system_once(
move |mut ev_scene: EventReader<'_, '_, SceneInstanceReady>| {
assert_eq!(ev_scene.read().len(), 1);
},
);
}
}

0 comments on commit 23afd0e

Please sign in to comment.