#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

#2095 in Game dev

40 downloads per month

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

~18–27MB
~410K SLoC