Comment on page
State Machines
Interact with the Rive State Machine in Bevy
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);
}
}
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,
});
}
}
Last modified 27d ago