Skip to content

Commit 05543cf

Browse files
mockersfItsDoot
authored andcommitted
don't error when sending HierarchyEvents when Event type not registered (bevyengine#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 bevyengine#6921 - don't use `world.send_event` / `world.send_event_batch` from commands
1 parent 24e3559 commit 05543cf

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

crates/bevy_hierarchy/src/child_builder.rs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@ use crate::{Children, HierarchyEvent, Parent};
22
use bevy_ecs::{
33
bundle::Bundle,
44
entity::Entity,
5+
prelude::Events,
56
system::{Command, Commands, EntityCommands},
67
world::{EntityMut, World},
78
};
89
use smallvec::SmallVec;
910

11+
// Do not use `world.send_event_batch` as it prints error message when the Events are not available in the world,
12+
// even though it's a valid use case to execute commands on a world without events. Loading a GLTF file for example
13+
fn push_events(world: &mut World, events: impl IntoIterator<Item = HierarchyEvent>) {
14+
if let Some(mut moved) = world.get_resource_mut::<Events<HierarchyEvent>>() {
15+
moved.extend(events);
16+
}
17+
}
18+
1019
fn push_child_unchecked(world: &mut World, parent: Entity, child: Entity) {
1120
let mut parent = world.entity_mut(parent);
1221
if let Some(mut children) = parent.get_mut::<Children>() {
@@ -58,13 +67,16 @@ fn update_old_parent(world: &mut World, child: Entity, parent: Entity) {
5867
}
5968
remove_from_children(world, previous_parent, child);
6069

61-
world.send_event(HierarchyEvent::ChildMoved {
62-
child,
63-
previous_parent,
64-
new_parent: parent,
65-
});
70+
push_events(
71+
world,
72+
[HierarchyEvent::ChildMoved {
73+
child,
74+
previous_parent,
75+
new_parent: parent,
76+
}],
77+
);
6678
} else {
67-
world.send_event(HierarchyEvent::ChildAdded { child, parent });
79+
push_events(world, [HierarchyEvent::ChildAdded { child, parent }]);
6880
}
6981
}
7082

@@ -95,7 +107,7 @@ fn update_old_parents(world: &mut World, parent: Entity, children: &[Entity]) {
95107
events.push(HierarchyEvent::ChildAdded { child, parent });
96108
}
97109
}
98-
world.send_event_batch(events);
110+
push_events(world, events);
99111
}
100112

101113
fn remove_children(parent: Entity, children: &[Entity], world: &mut World) {
@@ -114,7 +126,7 @@ fn remove_children(parent: Entity, children: &[Entity], world: &mut World) {
114126
world.entity_mut(child).remove::<Parent>();
115127
}
116128
}
117-
world.send_event_batch(events);
129+
push_events(world, events);
118130

119131
let mut parent = world.entity_mut(parent);
120132
if let Some(mut parent_children) = parent.get_mut::<Children>() {
@@ -337,21 +349,27 @@ impl<'w> WorldChildBuilder<'w> {
337349
pub fn spawn(&mut self, bundle: impl Bundle + Send + Sync + 'static) -> EntityMut<'_> {
338350
let entity = self.world.spawn((bundle, Parent(self.parent))).id();
339351
push_child_unchecked(self.world, self.parent, entity);
340-
self.world.send_event(HierarchyEvent::ChildAdded {
341-
child: entity,
342-
parent: self.parent,
343-
});
352+
push_events(
353+
self.world,
354+
[HierarchyEvent::ChildAdded {
355+
child: entity,
356+
parent: self.parent,
357+
}],
358+
);
344359
self.world.entity_mut(entity)
345360
}
346361

347362
/// Spawns an [`Entity`] with no components and inserts it into the children defined by the [`WorldChildBuilder`] which adds the [`Parent`] component to it.
348363
pub fn spawn_empty(&mut self) -> EntityMut<'_> {
349364
let entity = self.world.spawn(Parent(self.parent)).id();
350365
push_child_unchecked(self.world, self.parent, entity);
351-
self.world.send_event(HierarchyEvent::ChildAdded {
352-
child: entity,
353-
parent: self.parent,
354-
});
366+
push_events(
367+
self.world,
368+
[HierarchyEvent::ChildAdded {
369+
child: entity,
370+
parent: self.parent,
371+
}],
372+
);
355373
self.world.entity_mut(entity)
356374
}
357375

@@ -465,7 +483,7 @@ impl<'w> BuildWorldChildren for EntityMut<'w> {
465483
if let Some(parent) = self.remove::<Parent>().map(|p| p.get()) {
466484
self.world_scope(|world| {
467485
remove_from_children(world, parent, child);
468-
world.send_event(HierarchyEvent::ChildRemoved { child, parent });
486+
push_events(world, [HierarchyEvent::ChildRemoved { child, parent }]);
469487
});
470488
}
471489
self

0 commit comments

Comments
 (0)