Conditional Commands
This crate implements three extension traits that allow for conditional component, bundle and child insertion without the need for an intermediate EntityCommands or EntityMut binding.
ConditionalInsertBundleExtforEntityCommandsandEntityMut
Methods:insert_ifinsert_if_elseinsert_someinsert_some_or
ConditionalChildBuilderExtforEntityCommands
Methods:with_children_if
ConditionalWorldChildBuilderExtforEntityMut
Methods:with_children_if
Supports Bevy 0.9
Usage
To add to a project either use:
cargo add conditional_commands
or manually add to your Cargo.toml:
[]
= "0.9.0"
A Motivating But Contrived ECS Fizz-Buzz Example
use *;
use *;
;
;
;
;
With Conditional Commands the intermediate EntityCommands binding in no longer required.
ConditionalInsertBundleExt is also implemented for EntityMut:
;
;
Bundles passed to the _else/_or methods don't need to be the same type,
as seen in the above example with the Even and Odd components.
Use insert_some to insert the inner value of an optional bundle, if present.
commands.spawn
.insert_some
.insert_some_or;
Examples
cargo run --example exclusive
cargo run --example fizz_buzz
cargo run --example insert_if
cargo run --example insert_if_2
cargo run --example with_children_if
Notes
-
I haven't done any benchmarking. For performance critical systems it should be better to spawn entire bundles at once.
-
Earlier versions of this crate have both eager and lazy versions of each insertion method. I'm not sure the eager versions had any advantages (beyond no
||), so they are gone. -
I wasn't able to quite finesse the lifetimes to get a single generic child builder trait for both
EntityCommandsandEntityMut.