don't error when sending HierarchyEvents when Event type not registered (#7031)
# Objective - Loading a gltf files prints many errors ``` ERROR bevy_ecs::world: Unable to send event `bevy_hierarchy::events::HierarchyEvent` Event must be added to the app with `add_event()` https://docs.rs/bevy/*/bevy/app/struct.App.html#method.add_event ``` - Loading a gltf file create a world for a scene where events are not registered. Executing hierarchy commands on that world should not print error ## Solution - Revert part of #6921 - don't use `world.send_event` / `world.send_event_batch` from commands
This commit is contained in:
parent
7763b5ec74
commit
f1a21db250
@ -2,11 +2,20 @@ use crate::{Children, HierarchyEvent, Parent};
|
|||||||
use bevy_ecs::{
|
use bevy_ecs::{
|
||||||
bundle::Bundle,
|
bundle::Bundle,
|
||||||
entity::Entity,
|
entity::Entity,
|
||||||
|
prelude::Events,
|
||||||
system::{Command, Commands, EntityCommands},
|
system::{Command, Commands, EntityCommands},
|
||||||
world::{EntityMut, World},
|
world::{EntityMut, World},
|
||||||
};
|
};
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
|
|
||||||
|
// Do not use `world.send_event_batch` as it prints error message when the Events are not available in the world,
|
||||||
|
// even though it's a valid use case to execute commands on a world without events. Loading a GLTF file for example
|
||||||
|
fn push_events(world: &mut World, events: impl IntoIterator<Item = HierarchyEvent>) {
|
||||||
|
if let Some(mut moved) = world.get_resource_mut::<Events<HierarchyEvent>>() {
|
||||||
|
moved.extend(events);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn push_child_unchecked(world: &mut World, parent: Entity, child: Entity) {
|
fn push_child_unchecked(world: &mut World, parent: Entity, child: Entity) {
|
||||||
let mut parent = world.entity_mut(parent);
|
let mut parent = world.entity_mut(parent);
|
||||||
if let Some(mut children) = parent.get_mut::<Children>() {
|
if let Some(mut children) = parent.get_mut::<Children>() {
|
||||||
@ -58,13 +67,16 @@ fn update_old_parent(world: &mut World, child: Entity, parent: Entity) {
|
|||||||
}
|
}
|
||||||
remove_from_children(world, previous_parent, child);
|
remove_from_children(world, previous_parent, child);
|
||||||
|
|
||||||
world.send_event(HierarchyEvent::ChildMoved {
|
push_events(
|
||||||
child,
|
world,
|
||||||
previous_parent,
|
[HierarchyEvent::ChildMoved {
|
||||||
new_parent: parent,
|
child,
|
||||||
});
|
previous_parent,
|
||||||
|
new_parent: parent,
|
||||||
|
}],
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
world.send_event(HierarchyEvent::ChildAdded { child, parent });
|
push_events(world, [HierarchyEvent::ChildAdded { child, parent }]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,7 +107,7 @@ fn update_old_parents(world: &mut World, parent: Entity, children: &[Entity]) {
|
|||||||
events.push(HierarchyEvent::ChildAdded { child, parent });
|
events.push(HierarchyEvent::ChildAdded { child, parent });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
world.send_event_batch(events);
|
push_events(world, events);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_children(parent: Entity, children: &[Entity], world: &mut World) {
|
fn remove_children(parent: Entity, children: &[Entity], world: &mut World) {
|
||||||
@ -114,7 +126,7 @@ fn remove_children(parent: Entity, children: &[Entity], world: &mut World) {
|
|||||||
world.entity_mut(child).remove::<Parent>();
|
world.entity_mut(child).remove::<Parent>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
world.send_event_batch(events);
|
push_events(world, events);
|
||||||
|
|
||||||
let mut parent = world.entity_mut(parent);
|
let mut parent = world.entity_mut(parent);
|
||||||
if let Some(mut parent_children) = parent.get_mut::<Children>() {
|
if let Some(mut parent_children) = parent.get_mut::<Children>() {
|
||||||
@ -337,10 +349,13 @@ impl<'w> WorldChildBuilder<'w> {
|
|||||||
pub fn spawn(&mut self, bundle: impl Bundle + Send + Sync + 'static) -> EntityMut<'_> {
|
pub fn spawn(&mut self, bundle: impl Bundle + Send + Sync + 'static) -> EntityMut<'_> {
|
||||||
let entity = self.world.spawn((bundle, Parent(self.parent))).id();
|
let entity = self.world.spawn((bundle, Parent(self.parent))).id();
|
||||||
push_child_unchecked(self.world, self.parent, entity);
|
push_child_unchecked(self.world, self.parent, entity);
|
||||||
self.world.send_event(HierarchyEvent::ChildAdded {
|
push_events(
|
||||||
child: entity,
|
self.world,
|
||||||
parent: self.parent,
|
[HierarchyEvent::ChildAdded {
|
||||||
});
|
child: entity,
|
||||||
|
parent: self.parent,
|
||||||
|
}],
|
||||||
|
);
|
||||||
self.world.entity_mut(entity)
|
self.world.entity_mut(entity)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -348,10 +363,13 @@ impl<'w> WorldChildBuilder<'w> {
|
|||||||
pub fn spawn_empty(&mut self) -> EntityMut<'_> {
|
pub fn spawn_empty(&mut self) -> EntityMut<'_> {
|
||||||
let entity = self.world.spawn(Parent(self.parent)).id();
|
let entity = self.world.spawn(Parent(self.parent)).id();
|
||||||
push_child_unchecked(self.world, self.parent, entity);
|
push_child_unchecked(self.world, self.parent, entity);
|
||||||
self.world.send_event(HierarchyEvent::ChildAdded {
|
push_events(
|
||||||
child: entity,
|
self.world,
|
||||||
parent: self.parent,
|
[HierarchyEvent::ChildAdded {
|
||||||
});
|
child: entity,
|
||||||
|
parent: self.parent,
|
||||||
|
}],
|
||||||
|
);
|
||||||
self.world.entity_mut(entity)
|
self.world.entity_mut(entity)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -465,7 +483,7 @@ impl<'w> BuildWorldChildren for EntityMut<'w> {
|
|||||||
if let Some(parent) = self.remove::<Parent>().map(|p| p.get()) {
|
if let Some(parent) = self.remove::<Parent>().map(|p| p.get()) {
|
||||||
self.world_scope(|world| {
|
self.world_scope(|world| {
|
||||||
remove_from_children(world, parent, child);
|
remove_from_children(world, parent, child);
|
||||||
world.send_event(HierarchyEvent::ChildRemoved { child, parent });
|
push_events(world, [HierarchyEvent::ChildRemoved { child, parent }]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user