From d60b715411e0f906daccc0f5ae509e44c5ab6dc7 Mon Sep 17 00:00:00 2001 From: Hennadii Chernyshchyk Date: Tue, 15 Aug 2023 22:45:01 +0300 Subject: [PATCH] Add `SceneInstanceReady` (#9313) # Objective Closes #9115, replaces #9117. ## Solution Emit event when scene is ready. --- ## Changelog ### Added - `SceneInstanceReady` event when scene becomes ready. --- crates/bevy_scene/src/lib.rs | 1 + crates/bevy_scene/src/scene_spawner.rs | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/bevy_scene/src/lib.rs b/crates/bevy_scene/src/lib.rs index 655675e3f3..7725627891 100644 --- a/crates/bevy_scene/src/lib.rs +++ b/crates/bevy_scene/src/lib.rs @@ -40,6 +40,7 @@ impl Plugin for ScenePlugin { app.add_asset::() .add_asset::() .init_asset_loader::() + .add_event::() .init_resource::() .add_systems(SpawnScene, (scene_spawner, scene_spawner_system).chain()); } diff --git a/crates/bevy_scene/src/scene_spawner.rs b/crates/bevy_scene/src/scene_spawner.rs index 732b8593f1..4816ee6682 100644 --- a/crates/bevy_scene/src/scene_spawner.rs +++ b/crates/bevy_scene/src/scene_spawner.rs @@ -2,7 +2,7 @@ use crate::{DynamicScene, Scene}; use bevy_asset::{AssetEvent, Assets, Handle}; use bevy_ecs::{ entity::{Entity, EntityMap}, - event::{Events, ManualEventReader}, + event::{Event, Events, ManualEventReader}, reflect::AppTypeRegistry, system::{Command, Resource}, world::{Mut, World}, @@ -12,6 +12,15 @@ use bevy_utils::{tracing::error, HashMap, HashSet}; use thiserror::Error; use uuid::Uuid; +/// Emitted when [`crate::SceneInstance`] becomes ready to use. +/// +/// See also [`SceneSpawner::instance_is_ready`]. +#[derive(Event)] +pub struct SceneInstanceReady { + /// Entity to which the scene was spawned as a child. + pub parent: Entity, +} + /// Information about a scene instance. #[derive(Debug)] pub struct InstanceInfo { @@ -285,6 +294,8 @@ impl SceneSpawner { child: entity, } .apply(world); + + world.send_event(SceneInstanceReady { parent }); } } } else {