Skip to content

Fix #19219 by moving observer triggers out of resource_scope #19221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions crates/bevy_scene/src/scene_spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub struct SceneSpawner {
scenes_to_despawn: Vec<AssetId<DynamicScene>>,
instances_to_despawn: Vec<InstanceId>,
scenes_with_parent: Vec<(InstanceId, Entity)>,
instances_ready: Vec<(InstanceId, Option<Entity>)>,
}

/// Errors that can occur when spawning a scene.
Expand Down Expand Up @@ -337,8 +338,9 @@ impl SceneSpawner {
// Scenes with parents need more setup before they are ready.
// See `set_scene_instance_parent_sync()`.
if parent.is_none() {
// Defer via commands otherwise SceneSpawner is not available in the observer.
world.commands().trigger(SceneInstanceReady { instance_id });
// We trigger `SceneInstanceReady` events after processing all scenes
// SceneSpawner may not be available in the observer.
self.instances_ready.push((instance_id, None));
}
}
Err(SceneSpawnError::NonExistentScene { .. }) => {
Expand All @@ -362,8 +364,9 @@ impl SceneSpawner {
// Scenes with parents need more setup before they are ready.
// See `set_scene_instance_parent_sync()`.
if parent.is_none() {
// Defer via commands otherwise SceneSpawner is not available in the observer.
world.commands().trigger(SceneInstanceReady { instance_id });
// We trigger `SceneInstanceReady` events after processing all scenes
// SceneSpawner may not be available in the observer.
self.instances_ready.push((instance_id, None));
}
}
Err(SceneSpawnError::NonExistentRealScene { .. }) => {
Expand Down Expand Up @@ -398,12 +401,25 @@ impl SceneSpawner {
}
}

// We trigger `SceneInstanceReady` events after processing all scenes
// SceneSpawner may not be available in the observer.
self.instances_ready.push((instance_id, Some(parent)));
} else {
self.scenes_with_parent.push((instance_id, parent));
}
}
}

fn trigger_scene_ready_events(&mut self, world: &mut World) {
for (instance_id, parent) in self.instances_ready.drain(..) {
if let Some(parent) = parent {
// Defer via commands otherwise SceneSpawner is not available in the observer.
world
.commands()
.trigger_targets(SceneInstanceReady { instance_id }, parent);
} else {
self.scenes_with_parent.push((instance_id, parent));
// Defer via commands otherwise SceneSpawner is not available in the observer.
world.commands().trigger(SceneInstanceReady { instance_id });
}
}
}
Expand Down Expand Up @@ -477,6 +493,7 @@ pub fn scene_spawner_system(world: &mut World) {
.update_spawned_scenes(world, &updated_spawned_scenes)
.unwrap();
scene_spawner.set_scene_instance_parent_sync(world);
scene_spawner.trigger_scene_ready_events(world);
});
}

Expand Down