diff --git a/src/actions.rs b/src/actions.rs index b6d6183..68a09ba 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -287,8 +287,9 @@ pub fn steps_system( let step_state = step_state.clone(); let mut seq_state = states.get_mut(seq_ent).expect("idk"); *seq_state = step_state; - cmd.entity(steps_action.active_ent.entity()) - .despawn_recursive(); + if let Some(ent) = cmd.get_entity(steps_action.active_ent.entity()) { + ent.despawn_recursive(); + } } Success if steps_action.active_step == steps_action.steps.len() - 1 => { // We're done! Let's just be successful @@ -297,15 +298,17 @@ pub fn steps_system( let step_state = step_state.clone(); let mut seq_state = states.get_mut(seq_ent).expect("idk"); *seq_state = step_state; - cmd.entity(steps_action.active_ent.entity()) - .despawn_recursive(); + if let Some(ent) = cmd.get_entity(steps_action.active_ent.entity()) { + ent.despawn_recursive(); + } } Success => { #[cfg(feature = "trace")] trace!("Step succeeded, but there's more steps. Spawning next action."); // Deactivate current step and go to the next step - cmd.entity(steps_action.active_ent.entity()) - .despawn_recursive(); + if let Some(ent) = cmd.get_entity(steps_action.active_ent.entity()) { + ent.despawn_recursive(); + } steps_action.active_step += 1; let step_builder = steps_action.steps[steps_action.active_step].clone(); diff --git a/src/thinker.rs b/src/thinker.rs index e028510..0eb25a6 100644 --- a/src/thinker.rs +++ b/src/thinker.rs @@ -263,7 +263,9 @@ pub fn thinker_component_detach_system( q: Query<(Entity, &HasThinker), Without>, ) { for (actor, HasThinker(thinker)) in q.iter() { - cmd.entity(*thinker).despawn_recursive(); + if let Some(ent) = cmd.get_entity(*thinker) { + ent.despawn_recursive(); + } cmd.entity(actor).remove::(); } } @@ -276,7 +278,9 @@ pub fn actor_gone_cleanup( for (child, Actor(actor)) in q.iter() { if actors.get(*actor).is_err() { // Actor is gone. Let's clean up. - cmd.entity(child).despawn_recursive(); + if let Some(ent) = cmd.get_entity(child) { + ent.despawn_recursive(); + } } } } @@ -350,7 +354,9 @@ pub fn thinker_system( match state { ActionState::Success | ActionState::Failure => { debug!("Action already wrapped up on its own. Cleaning up action in Thinker."); - cmd.entity(current.0 .0).despawn_recursive(); + if let Some(ent) = cmd.get_entity(current.0 .0) { + ent.despawn_recursive(); + } thinker.current_action = None; } ActionState::Cancelled => { @@ -430,7 +436,9 @@ pub fn thinker_system( "Action completed and nothing was picked. Despawning action entity.", ); // Despawn the action itself. - cmd.entity(action_ent.0).despawn_recursive(); + if let Some(ent) = cmd.get_entity(action_ent.0) { + ent.despawn_recursive(); + } thinker.current_action = None; } else if *curr_action_state == ActionState::Init { *curr_action_state = ActionState::Requested; @@ -536,7 +544,9 @@ fn exec_picked_action( ActionState::Init | ActionState::Success | ActionState::Failure => { debug!("Previous action already completed. Despawning action entity.",); // Despawn the action itself. - cmd.entity(action_ent.0).despawn_recursive(); + if let Some(ent) = cmd.get_entity(action_ent.0) { + ent.despawn_recursive(); + } if let Some((Scorer(ent), score)) = scorer_info { let scorer_span = scorer_spans.get(*ent).expect("Where is it?"); let _guard = scorer_span.span.enter();