Skip to main content
Bevy State Machines

State Machines

Interact with the Rive State Machine in Bevy.

For more information on Rive State Machines see the respective runtime and editor documentation.

Reading/Writing Inputs

Imports needed:

use rive_bevy::{
    events::{self, InputValue},
    RivePlugin, RiveStateMachine, SceneTarget, SpriteEntity, StateMachine,
};

The following code demonstrates reading and updating a Boolean state machine input.

Number and Trigger inputs can be accessed using get_number and get_trigger respectively.

fn update_state_machine_system(
    kbd: Res<Input<KeyCode>>,
    query: Query<(Entity, &mut RiveStateMachine)>,
) {
    if kbd.just_pressed(KeyCode::Return) {
        // Get the State Machine and its Entity
        let (_, state_machine) = query.get_single().expect("State machine not found");

        // Read the current value of an input
        let center_hover_current = state_machine.get_bool("centerHover").unwrap().get();

        // Update the input
        state_machine
            .get_bool("centerHover")
            .unwrap()
            .set(!center_hover_current);
    }
}

Alternatively, you can update an input using Bevy's EventWriter:

fn update_state_machine_system(
    kbd: Res<Input<KeyCode>>,
    query: Query<(Entity, &RiveStateMachine)>,
    mut input_events: EventWriter<events::Input>,
) {
    if kbd.just_pressed(KeyCode::Return) {
        // Get the State Machine and its Entity
        let (entity, _) = query.get_single().expect("State machine not found");

        // Send a new value to the input using Bevy events.
        input_events.send(events::Input {
            state_machine: entity,
            name: "shoot".into(), // name of the input
            value: events::InputValue::Trigger,
        });
    }
}

Additional Resources

  • Rive Bevy inputs example