pub struct BehaviorLoader { /* private fields */ }Expand description
Loader for creating behavior trees from JSON configuration.
The BehaviorLoader takes a BehaviorConfig (which can be loaded from JSON)
and uses a NodeRegistry to instantiate the complete behavior tree.
§Features
- Recursive composition parsing (sequence/selector/parallel nodes)
- Config reference resolution from the config map
- Inline config support
- Clear error messages for missing node types
§Example
use mecha10_behavior_runtime::prelude::*;
use mecha10_behavior_runtime::{NodeRegistry, BehaviorLoader};
use serde_json::json;
// Create and populate registry
let mut registry = NodeRegistry::new();
registry.register("my_behavior", |_| Ok(Box::new(MyBehavior)));
// Load behavior tree from JSON
let json = r#"{
"name": "test_behavior",
"root": {
"type": "sequence",
"children": [
{
"type": "node",
"node": "my_behavior"
}
]
}
}"#;
let config = BehaviorConfig::from_json(json).unwrap();
let loader = BehaviorLoader::new(registry);
let behavior = loader.load(&config).unwrap();Implementations§
Source§impl BehaviorLoader
impl BehaviorLoader
Sourcepub fn new(registry: NodeRegistry) -> Self
pub fn new(registry: NodeRegistry) -> Self
Sourcepub fn load(&self, config: &BehaviorConfig) -> Result<BoxedBehavior>
pub fn load(&self, config: &BehaviorConfig) -> Result<BoxedBehavior>
Load a behavior tree from a configuration.
This is the main entry point for loading behavior trees. It takes a
BehaviorConfig (typically loaded from JSON) and recursively instantiates
all nodes in the tree.
§Arguments
config- The behavior configuration to load
§Returns
A boxed behavior node representing the root of the tree, or an error if:
- A referenced node type is not registered
- A config reference is invalid
- A factory function fails
§Example
use mecha10_behavior_runtime::{BehaviorConfig, NodeRegistry, BehaviorLoader};
let loader = BehaviorLoader::new(registry);
let json = r#"{
"name": "test",
"root": {
"type": "node",
"node": "test"
}
}"#;
let config = BehaviorConfig::from_json(json).unwrap();
let behavior = loader.load(&config).unwrap();Sourcepub fn load_from_json(&self, json: &str) -> Result<BoxedBehavior>
pub fn load_from_json(&self, json: &str) -> Result<BoxedBehavior>
Load a behavior tree from a JSON string.
This is a convenience method that combines parsing and loading.
§Arguments
json- JSON string containing a BehaviorConfig
§Example
use mecha10_behavior_runtime::{NodeRegistry, BehaviorLoader};
let loader = BehaviorLoader::new(registry);
let json = r#"{
"name": "test",
"root": {
"type": "node",
"node": "test"
}
}"#;
let behavior = loader.load_from_json(json).unwrap();Sourcepub fn load_from_file(&self, path: impl AsRef<Path>) -> Result<BoxedBehavior>
pub fn load_from_file(&self, path: impl AsRef<Path>) -> Result<BoxedBehavior>
Load a behavior tree from a JSON file.
This is a convenience method that reads a file and loads the behavior tree.
§Arguments
path- Path to the JSON file
§Example
use mecha10_behavior_runtime::{NodeRegistry, BehaviorLoader};
let loader = BehaviorLoader::new(registry);
let behavior = loader.load_from_file("behaviors/patrol.json").unwrap();Sourcepub fn registry(&self) -> &NodeRegistry
pub fn registry(&self) -> &NodeRegistry
Get a reference to the underlying node registry.
This can be useful for inspecting registered types or adding new types after the loader has been created.
Trait Implementations§
Source§impl Clone for BehaviorLoader
impl Clone for BehaviorLoader
Source§fn clone(&self) -> BehaviorLoader
fn clone(&self) -> BehaviorLoader
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for BehaviorLoader
impl RefUnwindSafe for BehaviorLoader
impl Send for BehaviorLoader
impl Sync for BehaviorLoader
impl Unpin for BehaviorLoader
impl UnwindSafe for BehaviorLoader
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more