Skip to content

A reactive(ish) system for bevy

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

Freyja-moth/bevy_notify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bevy Notify

A reactive(ish) system for the bevy game engine using relationships.

use bevy_notify::prelude::*;
use bevy::{prelude::*, ui_widgets::observe};

pub struct Health(pub u8);

let player = commands
    .spawn((
        Name::new("Player"),
        Health(100),
        MonitorSelf,
        observe(|mutation: On<Mutation<Health>>, health: Query<&Health>| -> Result<(), BevyError> {
            let current_health = health.get(mutation.entity)?;

            println!("My current health is {}", current_health);
        })
    ))
    .id();

commands.spawn((
    Name::new("Doctor"),
    Monitoring(player),
    NotifyChanged::<Health>::default(),
    observe(
        |mutation: On<Mutation<Health>>,
        mut health: Query<&mut Health>|
        -> Result<(), BevyError> {
            let mut health = health.get_mut(mutation.mutated)?;

            if health.0 <= 20 {
                health.0 += 20;
            }

            Ok(())
         },
     ),
));

Documentation

Docs aren't great atm, will improve in time but for the time being just make an issue whenever you run into something.

Future Work

Use many to many relationships once we have them so that multiple entities can be watched at once. For now the same effect can be achieved by using a custom relationship.

Also look into using ComponentId on the observer trigger to do fancy stuff like On<Mutation, Component>.

About

A reactive(ish) system for bevy

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages