Skip to content
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

Update trigger_observers to operate over slices of data #14354

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
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
14 changes: 5 additions & 9 deletions crates/bevy_ecs/src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,11 +682,7 @@ impl<'w> BundleInserter<'w> {
add_bundle.mutated.iter().copied(),
);
if archetype.has_replace_observer() {
deferred_world.trigger_observers(
ON_REPLACE,
entity,
add_bundle.mutated.iter().copied(),
);
deferred_world.trigger_observers(ON_REPLACE, entity, &add_bundle.mutated);
}
}

Expand Down Expand Up @@ -842,11 +838,11 @@ impl<'w> BundleInserter<'w> {
unsafe {
deferred_world.trigger_on_add(new_archetype, entity, add_bundle.added.iter().cloned());
if new_archetype.has_add_observer() {
deferred_world.trigger_observers(ON_ADD, entity, add_bundle.added.iter().cloned());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the change we're looking for :D

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alice-i-cecile @NthTensor Now curious about trigger_on_add and trigger_on_insert, should these get the same treatment? I also didn't have full context around the "whys" so maybe there's a reason to just apply it to specific methods.

deferred_world.trigger_observers(ON_ADD, entity, &add_bundle.added);
}
deferred_world.trigger_on_insert(new_archetype, entity, bundle_info.iter_components());
if new_archetype.has_insert_observer() {
deferred_world.trigger_observers(ON_INSERT, entity, bundle_info.iter_components());
deferred_world.trigger_observers(ON_INSERT, entity, bundle_info.components());
}
}

Expand Down Expand Up @@ -959,11 +955,11 @@ impl<'w> BundleSpawner<'w> {
unsafe {
deferred_world.trigger_on_add(archetype, entity, bundle_info.iter_components());
if archetype.has_add_observer() {
deferred_world.trigger_observers(ON_ADD, entity, bundle_info.iter_components());
deferred_world.trigger_observers(ON_ADD, entity, bundle_info.components());
}
deferred_world.trigger_on_insert(archetype, entity, bundle_info.iter_components());
if archetype.has_insert_observer() {
deferred_world.trigger_observers(ON_INSERT, entity, bundle_info.iter_components());
deferred_world.trigger_observers(ON_INSERT, entity, bundle_info.components());
}
};

Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/world/deferred_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,13 @@ impl<'w> DeferredWorld<'w> {
&mut self,
event: ComponentId,
entity: Entity,
components: impl Iterator<Item = ComponentId>,
components: &[ComponentId],
) {
Observers::invoke::<_>(
self.reborrow(),
event,
entity,
components,
components.iter().copied(),
&mut (),
&mut false,
);
Expand Down
16 changes: 12 additions & 4 deletions crates/bevy_ecs/src/world/entity_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,11 +1224,19 @@ impl<'w> EntityWorldMut<'w> {
unsafe {
deferred_world.trigger_on_replace(archetype, self.entity, archetype.components());
if archetype.has_replace_observer() {
deferred_world.trigger_observers(ON_REPLACE, self.entity, archetype.components());
deferred_world.trigger_observers(
ON_REPLACE,
self.entity,
&archetype.components().collect::<Vec<ComponentId>>(),
);
}
deferred_world.trigger_on_remove(archetype, self.entity, archetype.components());
if archetype.has_remove_observer() {
deferred_world.trigger_observers(ON_REMOVE, self.entity, archetype.components());
deferred_world.trigger_observers(
ON_REMOVE,
self.entity,
&archetype.components().collect::<Vec<ComponentId>>(),
);
}
}

Expand Down Expand Up @@ -1435,11 +1443,11 @@ unsafe fn trigger_on_replace_and_on_remove_hooks_and_observers(
) {
deferred_world.trigger_on_replace(archetype, entity, bundle_info.iter_components());
if archetype.has_replace_observer() {
deferred_world.trigger_observers(ON_REPLACE, entity, bundle_info.iter_components());
deferred_world.trigger_observers(ON_REPLACE, entity, bundle_info.components());
}
deferred_world.trigger_on_remove(archetype, entity, bundle_info.iter_components());
if archetype.has_remove_observer() {
deferred_world.trigger_observers(ON_REMOVE, entity, bundle_info.iter_components());
deferred_world.trigger_observers(ON_REMOVE, entity, bundle_info.components());
}
}

Expand Down