3个版本 (有破坏性)

0.3.0 2024年2月19日
0.2.0 2024年2月10日
0.1.0 2023年12月28日

#299 in 过程宏

MIT/Apache

41KB
651

Crates.io Docs

一个宏,用于从函数创建bevy的CommandsEntityCommands方法。

示例

use bevy_commandify::*;

#[command]
fn foo(world: &mut World, n: usize) {
    // Bear in mind that Commands *defer* work
    // This function will not be called immediately
    // It will be executed at the end of the current schedule 
    // Or when `apply_deferred` is next called within the current schedule
    let mut bar = world.resource_mut::<Bar>();
    **bar -= n;
}

/// Regular bevy systems can be used as commands, too
#[command]
fn irony(mut commands: Commands) {
    commands.foo(5);
}

#[entity_command]
/// Commands may optionally return `&mut Self` to allow chaining command calls
fn bar(world: &mut World, entity: Entity, n: usize) -> &mut Self {
    let mut component = world
        .query::<&mut Bar>()
        .get_mut(world, entity)
        .unwrap();
    **component -= n;
}

fn commands(mut commands: Commands) {
    // Fire our command directly
    commands.foo(10);
    // call it via the generated extension trait
    CommandsFooExt::foo(&mut commands, 10);
    // Add the command as a struct
    commands.add(FooCommand { n: 10 });
}

// Commands may also run against the world directly
fn exclusive_commands(world: &mut World) {
    world.foo(10);
    CommandsFooExt::foo(world, 10);
}

fn entity_commands(mut commands: Commands) {
    let entity = commands.spawn(Bar::default()).id();
    
    commands.entity(entity).bar(10).insert(Foo); // We can chain calls to other commands
    EntityCommandsBarExt::bar(&mut commands.entity(entity), 10);
    commands.entity(entity).add(BarEntityCommand { n: 10 });
}

fn exclusive_entity_commands(world: &mut World) {
    let entity = world.spawn(Bar(0)).id();
    
    world.entity_mut(entity).bar(10);
    EntityCommandsBarExt::bar(&mut world.entity_mut(entity), 10);
}

另请参阅示例测试

属性

以下属性对#[command]#[entity_command]都适用

  • #[command(no_trait)]阻止为Commands生成特性行为,但仍然会生成你可以添加的Command结构体
#[command(no_trait)]
fn foo(world: &mut World) { }

commands.foo(); // This will throw an error
commands.add(FooCommand); // This will still work
  • #[command(no_world)]阻止对WorldEntityWorldMut生成特性行为实现
#[command(no_trait)]
fn foo(world: &mut World) { }

world.foo(); // This will throw an error
commands.foo(); // This will still work
  • #[command(name = T)]将使用T作为生成方法和相关结构体/特性行为名称
#[command(name = "bar")]
fn foo(world: &mut World) { }

commands.bar();
CommandsBarExt::bar(&mut commands);
commands.add(BarCommand);
  • #[command(struct_name = T)] 将使用此名称生成结构体
#[command(struct_name = "Bar")]
fn foo(world: &mut World) { }

commands.foo();
CommandsFooExt::foo(&mut commands);
commands.add(Bar);
  • #[command(trait_name = T)] 将使用此名称生成特质
#[command(trait_name = "BarExt")]
fn foo(world: &mut World) { }

commands.foo();
BarExt::foo(&mut commands);
commands.add(FooCommand);
  • #[command(ecs = T)]#[command(bevy_ecs)] 用于指向正确的 bevy 包,如果您没有直接使用 bevy

兼容性

Bevy
0.13 0.3
0.12 0.1, 0.2

依赖

~0.5–1MB
~21K SLoC