Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Spawn and Trigger an Observer

While bevy_pipe_affect effects are somewhat minimal and don't include a command_add_observer, observers still integrate well with effects.

The following code examples are pulled from the observer cargo example.

  1. Create an event type (InflateEvent in this example)
use bevy::prelude::*;
use bevy_pipe_affect::prelude::*;

#[derive(Event)]
pub struct InflateEvent;

#[derive(Component)]
pub struct Inflatable;

fn inflate(_event: On<InflateEvent>) -> impl Effect + use<> {
    components_set_filtered_with::<_, _, With<Inflatable>>(|(transform,): (Transform,)| {
        (transform.with_scale(transform.scale * 1.1),)
    })
}

pub fn spawn_observer() -> impl Effect {
    command_spawn(Observer::new(inflate.pipe(affect)))
}

pub fn trigger_observer(input: Res<ButtonInput<KeyCode>>) -> Option<CommandTrigger<InflateEvent>> {
    if input.just_pressed(KeyCode::Space) {
        Some(command_trigger(InflateEvent))
    } else {
        None
    }
}
fn main() {}
  1. Create a system that accepts an On<InflateEvent> and returns an effect that will be ran by the observer.
use bevy::prelude::*;
use bevy_pipe_affect::prelude::*;

#[derive(Event)]
pub struct InflateEvent;

#[derive(Component)]
pub struct Inflatable;

fn inflate(_event: On<InflateEvent>) -> impl Effect + use<> {
    components_set_filtered_with::<_, _, With<Inflatable>>(|(transform,): (Transform,)| {
        (transform.with_scale(transform.scale * 1.1),)
    })
}

pub fn spawn_observer() -> impl Effect {
    command_spawn(Observer::new(inflate.pipe(affect)))
}

pub fn trigger_observer(input: Res<ButtonInput<KeyCode>>) -> Option<CommandTrigger<InflateEvent>> {
    if input.just_pressed(KeyCode::Space) {
        Some(command_trigger(InflateEvent))
    } else {
        None
    }
}
fn main() { bevy::ecs::system::assert_is_system(inflate.pipe(affect)) }
  1. Create a system that spawns an Observer component with command_spawn, using the previous system .pipe(affect)-ed as the observer system.
use bevy::prelude::*;
use bevy_pipe_affect::prelude::*;

#[derive(Event)]
pub struct InflateEvent;

#[derive(Component)]
pub struct Inflatable;

fn inflate(_event: On<InflateEvent>) -> impl Effect + use<> {
    components_set_filtered_with::<_, _, With<Inflatable>>(|(transform,): (Transform,)| {
        (transform.with_scale(transform.scale * 1.1),)
    })
}

pub fn spawn_observer() -> impl Effect {
    command_spawn(Observer::new(inflate.pipe(affect)))
}

pub fn trigger_observer(input: Res<ButtonInput<KeyCode>>) -> Option<CommandTrigger<InflateEvent>> {
    if input.just_pressed(KeyCode::Space) {
        Some(command_trigger(InflateEvent))
    } else {
        None
    }
}
fn main() { bevy::ecs::system::assert_is_system(spawn_observer.pipe(affect)) }
  1. Trigger your observer with the command_trigger effect.
use bevy::prelude::*;
use bevy_pipe_affect::prelude::*;

#[derive(Event)]
pub struct InflateEvent;

#[derive(Component)]
pub struct Inflatable;

fn inflate(_event: On<InflateEvent>) -> impl Effect + use<> {
    components_set_filtered_with::<_, _, With<Inflatable>>(|(transform,): (Transform,)| {
        (transform.with_scale(transform.scale * 1.1),)
    })
}

pub fn spawn_observer() -> impl Effect {
    command_spawn(Observer::new(inflate.pipe(affect)))
}

pub fn trigger_observer(input: Res<ButtonInput<KeyCode>>) -> Option<CommandTrigger<InflateEvent>> {
    if input.just_pressed(KeyCode::Space) {
        Some(command_trigger(InflateEvent))
    } else {
        None
    }
}
fn main() { bevy::ecs::system::assert_is_system(trigger_observer.pipe(affect)) }