From 4c0b2a140520155a05afec35903d550527002fe2 Mon Sep 17 00:00:00 2001 From: Seldom <38388947+Seldom-SE@users.noreply.github.com> Date: Fri, 12 Jan 2024 22:38:17 -0700 Subject: [PATCH] Fix triggers reinitializing every tick --- README.md | 2 ++ examples/chase.rs | 3 +++ src/machine.rs | 2 ++ 3 files changed, 7 insertions(+) diff --git a/README.md b/README.md index 611f36e..15b7b54 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,8 @@ improvement, feel free to submit an issue or pr! `clamped_value_unbounded`, `just_pressed`, `just_released`, `pressed`, `value`, `value_max`, `value_min`, and `value_unbounded` - `on_event`: triggers when it reads an event of the given type + - Bevy's [built-in run conditions](https://docs.rs/bevy/latest/bevy/ecs/schedule/common_conditions/index.html) + also work as triggers. - `AnyState` state, that can be used in type parameters to represent any state - Transition builders that allow dataflow from outgoing states and triggers to incoming states (`StateMachine::trans_builder`) diff --git a/examples/chase.rs b/examples/chase.rs index 730ab4b..f586651 100644 --- a/examples/chase.rs +++ b/examples/chase.rs @@ -32,6 +32,9 @@ fn init(mut commands: Commands, asset_server: Res) { // define the trigger as a closure within this function so it can use variables in the scope // (namely, `player`). For the sake of example, we also define this trigger as an external // function later. + // + // Triggers are reinitialized after each transition, so they won't read events that occured in a + // previous state, `Local`s are reset between transitions, etc. let near_player = move |In(entity): In, transforms: Query<&Transform>| { let distance = transforms .get(player) diff --git a/src/machine.rs b/src/machine.rs index 169620f..cb4457b 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -276,6 +276,8 @@ impl StateMachine { for (_, transition) in &mut self.transitions { transition.init(world); } + + self.init_transitions = false; } /// Runs all transitions until one is actually taken. If one is taken, logs the transition and