Skip to main content

Crate behaviortree_derive

Crate behaviortree_derive 

Source
Expand description

Derive macros for Behaviors. There are 4 derive macros available:

  • Action
  • Condition
  • Control
  • Decorator

On struct level there are the attributes

  • #[behavior(no_create)]: will suppress the derive of create_fn(…)
  • #[behavior(no_register)]: will suppress the derive of register()
  • #[behavior(no_register_with)]: will suppress the derive of register_with(…)
  • #[behavior(groot2)]: marks the behavior as one that is internally known to Groot2

On field level there are the attributes

  • #[behavior(parameter)]: defines a field as a parameter for create_fn(...) and register_with(...) The values must be given when using the register_with(...) method. A parameter must implement Clone.
  • #[behavior(default = <Expression>)]: defines a default value for a field. ‘Expression’ can be any Rust expression that creates an appropriate value out of nothing.
  • #[behavior(parameter, default = <Expression>)]: combines both. The field is a parameter of create_fn(...) and register_with(...), and ‘Expression’ provides the value that the parameterless register() passes for it instead of Default::default().
  • #[behavior(portlist)]: marks the field that holds the behavior’s port list. This attribute cannot be combined with any other attribute.

Fields without any of these attributes are initialised with Default::default().

§Generated items

  • an implementation of BehaviorExecution with the BehaviorKind of the macro,
  • with a portlist field an implementation of PortCollectionProvider,
  • fn create_fn(<parameters>), the private creation function of the behavior,
  • pub fn register(registry, name), registering the behavior under name, using the defaults of the parameters,
  • pub fn register_with(registry, name, <parameters>), only if there are parameters.

The struct itself has to implement the trait Behavior. With a portlist field it also needs a function port_collection_init, which is created by the macro port_array_init!.

§Usage

Example uses the derive macro Action, the others work respectively. A runnable version is in the documentation of the crate behaviortree, which re-exports the macros.

ⓘ
use behaviortree::prelude::*;

#[derive(Action, Debug)]
struct SaySomething {
    // given at registration with `register_with(...)`, `register(...)` uses the default
    #[behavior(parameter, default = String::from("Robot says"))]
    prefix: String,
    // initialised with `Default::default()`
    count: u32,
    #[behavior(portlist)]
    portlist: PortArray<1>,
}

#[async_trait::async_trait]
impl Behavior for SaySomething {
    async fn tick(
        &mut self,
        _behavior: &mut BehaviorData,
        _children: &mut BehaviorTreeElementList,
        _runtime: &SharedRuntime,
    ) -> BehaviorResult {
        let msg = self.portlist.get::<String>("message").await?;
        self.count += 1;
        println!("{} [{}]: {msg}", self.prefix, self.count);
        Ok(BehaviorState::Success)
    }
}

impl SaySomething {
    port_array_init! {1,
        create_in_entry_parseable!("message", String)
    }
}

let mut factory = BehaviorTreeFactory::new()?;
SaySomething::register(&mut factory, "SaySomething")?;
SaySomething::register_with(&mut factory, "Shout", String::from("Robot shouts"))?;

§Errors

  • if used on anything other than a struct
  • if used on a struct with unnamed fields
  • if attributes are used in a wrong way

Derive Macros§

Action
Derive macro for an Action type Behavior, usage.
Condition
Derive macro for an Condition type Behavior, usage.
Control
Derive macro for an Control type Behavior, usage.
Decorator
Derive macro for an Decorator type Behavior, usage.