Skip to content

Commit 6bb0473

Browse files
authored
Remove task in handle_tasks in async_compute example (#20175)
# Objective - It seems counterintuitive that the task removes itself - More than once I have tried copying the code from this example and then remove the content of the task to replace it with what I need and then I get errors about a task being polled after it's already completed because I never removed it. ## Solution - Remove the task in the handle_tasks system after it's done being "handled". ## Testing - Ran the example and it still works as expected
1 parent d1aab7c commit 6bb0473

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

examples/async_tasks/async_compute.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ fn spawn_tasks(mut commands: Commands) {
8989
Mesh3d(box_mesh_handle),
9090
MeshMaterial3d(box_material_handle),
9191
transform,
92-
))
93-
// Task is complete, so remove task component from entity
94-
.remove::<ComputeTransform>();
92+
));
9593
});
9694

9795
command_queue
@@ -108,11 +106,16 @@ fn spawn_tasks(mut commands: Commands) {
108106
/// tasks to see if they're complete. If the task is complete it takes the result, adds a
109107
/// new [`Mesh3d`] and [`MeshMaterial3d`] to the entity using the result from the task's work, and
110108
/// removes the task component from the entity.
111-
fn handle_tasks(mut commands: Commands, mut transform_tasks: Query<&mut ComputeTransform>) {
112-
for mut task in &mut transform_tasks {
109+
fn handle_tasks(
110+
mut commands: Commands,
111+
mut transform_tasks: Query<(Entity, &mut ComputeTransform)>,
112+
) {
113+
for (entity, mut task) in &mut transform_tasks {
113114
if let Some(mut commands_queue) = block_on(future::poll_once(&mut task.0)) {
114115
// append the returned command queue to have it execute later
115116
commands.append(&mut commands_queue);
117+
// Task is complete, so remove task component from entity
118+
commands.entity(entity).remove::<ComputeTransform>();
116119
}
117120
}
118121
}

0 commit comments

Comments
 (0)