Skip to content

Commit

Permalink
Fix triggers reinitializing every tick
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldom-SE committed Jan 13, 2024
1 parent 118d18e commit 4c0b2a1
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
Expand Down
3 changes: 3 additions & 0 deletions examples/chase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ fn init(mut commands: Commands, asset_server: Res<AssetServer>) {
// 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<Entity>, transforms: Query<&Transform>| {
let distance = transforms
.get(player)
Expand Down
2 changes: 2 additions & 0 deletions src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4c0b2a1

Please sign in to comment.