Skip to content

Commit

Permalink
fix(despawn): don't try to despawn if an entity doesn't exist
Browse files Browse the repository at this point in the history
Fixes: #70
  • Loading branch information
zkat committed Dec 9, 2023
1 parent 2e493d6 commit bdf51c3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
15 changes: 9 additions & 6 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
20 changes: 15 additions & 5 deletions src/thinker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ pub fn thinker_component_detach_system(
q: Query<(Entity, &HasThinker), Without<ThinkerBuilder>>,
) {
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::<HasThinker>();
}
}
Expand All @@ -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();
}
}
}
}
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit bdf51c3

Please sign in to comment.