bevy-sequential-actions icon indicating copy to clipboard operation
bevy-sequential-actions copied to clipboard

Add cleanup of actions for despawned agents

Open hikikones opened this issue 5 months ago • 0 comments

Entities can be despawned at any moment, even an agent with a running action and more in the queue. Thanks to the new hooks and observers in Bevy 0.14, the Action trait methods on_stop, on_remove and on_drop are now called whenever an agent is despawned. In other words, you no longer need to worry about cleaning up actions when despawning.

To facilitate this, the on_stop, on_remove and on_drop methods now take Option<Entity> for agent. Being None, you know the agent does not exist anymore.

fn on_stop(&mut self, agent: Option<Entity>, world: &mut World, reason: StopReason) {
    // Do nothing if agent has been despawned.
    let Some(agent) = agent else { return };

    // ...
}

In addition, this PR also replaces all unwraps with logging, and more gracefully handles user errors such as despawning an agent in the on_add method.

hikikones avatar Sep 14 '24 20:09 hikikones