Instead of hand-rolling state machines to sequence the effects of various ECS systems, spawn tasks as entities and declare explicit temporal dependencies between them.
```rust use legion::prelude::; use legion_task::;
struct SaySomething(&'static str); impl<'a> TaskComponent<'a> for SaySomething { type Data = (); fn run(&mut self, data: &mut Self::Data) -> bool { println!("{}", self.0); true } }
struct PushValue { value: usize, }
impl<'a> TaskComponent<'a> for PushValue {
type Data = Vec
fn makestatictaskgraph(cmd: &mut CommandBuffer) { // Any component that implements TaskComponent can be spawned. let taskgraph: TaskGraph = seq!( @SaySomething("hello"), fork!( @PushValue { value: 1 }, @PushValue { value: 2 }, @PushValue { value: 3 } ), @SaySomething("goodbye") ); task_graph.assemble(OnCompletion::Delete, cmd); }
fn makedynamictaskgraph(cmd: &mut CommandBuffer) { let first: TaskGraph = task!(@SaySomething("hello")); let mut middle: TaskGraph = emptygraph!(); for i in 0..10 { middle = fork!(middle, @PushValue { value: i }); } let last: TaskGraph = task!(@SaySomething("goodbye")); let taskgraph: TaskGraph = seq!(first, middle, last); taskgraph.assemble(OnCompletion::Delete, cmd); }
fn buildsaysomethingtaskrunnersystem() -> Box
fn buildpushvaluetaskrunnersystem() -> Box
fn makeschedule() -> Schedule { Schedule::builder() .addsystem(buildsaysomethingtaskrunnersystem()) .addsystem(buildpushvaluetaskrunnersystem()) .addsystem(buildtaskmanagersystem("taskmanager")) .build() } ```