#callback #bevy #bevy-ecs #ecs #ui #macro

moshimoshi

A small crate to sugar working with command callbacks in bevy

2 unstable releases

0.2.0 Sep 16, 2023
0.1.0 Sep 9, 2023

#2510 in Game dev

MIT/Apache

6KB

moshimoshi

A small crate to sugar working with command callbacks in bevy.

Crates.io Docs.rs

use bevy::prelude::*;
use moshimoshi::*;

#[derive(Component)]
struct Button;

#[derive(Component, Deref, DerefMut)]
struct OnClick(EntityCallback);

#[derive(Component, Deref, DerefMut)]
struct Counter(u32);

#[derive(Component)]
struct Text(String);

fn setup(mut commands: Commands) {
    commands.spawn((
        Button,
        Counter(0),
        Text("Click Me".to_string()),
        OnClick(moshi!([e: Entity], counter: Query<&mut Counter> => {
            **counter.get_mut(e).unwrap() += 1;
        }))
    ));
}

impl Button {
    fn update(mut commands: Commands, buttons: Query<(Entity, &OnClick), Changed<Button>>) {
        for (entity, callback) in buttons.iter() {
            commands.add(RunEntityCallback { entity, func: **callback });
        }
    }
}

fn main() {
    App::new()
        .add_systems(Update, (Button::update, apply_deferred).chain())
        .run()
}

Version table

Bevy version moshimoshi verison
0.11 0.1 - 0.2

Dependencies

~21–30MB
~475K SLoC