pub trait Task<M = ()>: Send + Sync {
// Provided methods
fn is_locked(&self, _memory: &M) -> bool { ... }
fn on_enter(&mut self, _memory: &mut M) { ... }
fn on_exit(&mut self, _memory: &mut M) { ... }
fn on_update(&mut self, _memory: &mut M) { ... }
fn on_process(&mut self, _memory: &mut M) -> bool { ... }
}
Expand description
Task represent unit of work, an action performed in a time.
Tasks can only manipulate what’s in the memory passed to their life-cycle methods so common way of manipulating the world with tasks is to make memory type that can either command the world or even better, put triggers in the memory before running decision maker, then after that read what changed in the memory and apply changes to the world.
Tasks are managed by calling their life-cycle methods by the decision makers so implement these if you need to do something during that life-cycle phase.
Example
use emergent::prelude::*;
struct Memory { counter: usize }
struct Increment;
impl Task<Memory> for Increment {
fn on_enter(&mut self, memory: &mut Memory) {
memory.counter += 1;
}
}
let mut memory = Memory { counter: 1 };
Increment.on_enter(&mut memory);
assert_eq!(memory.counter, 2);
Provided Methods§
sourcefn is_locked(&self, _memory: &M) -> bool
fn is_locked(&self, _memory: &M) -> bool
Tells if task is locked (it’s still running). Used by decision makers to tell if one can change its state (when current task is not locked).
sourcefn on_update(&mut self, _memory: &mut M)
fn on_update(&mut self, _memory: &mut M)
Action performed when task is active and gets updated.
sourcefn on_process(&mut self, _memory: &mut M) -> bool
fn on_process(&mut self, _memory: &mut M) -> bool
Action performed when task is active but decision maker did not changed its state. This one is applicable for making hierarchical decision makers (telling children decision makers to decide on new state, because some if not all decision makers are tasks).
Returns true
if this task decided on new state.