Skip to content

Commit

Permalink
Make State pub(crate).
Browse files Browse the repository at this point in the history
Users shouldn't be messing with the internal state of the behavior tree. That could result in the behavior tree producing all sorts of strange behavior.
  • Loading branch information
andriyDev committed Jan 12, 2025
1 parent a995adc commit a6d7653
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 119 deletions.
10 changes: 2 additions & 8 deletions bonsai/src/bt.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Debug;

use crate::visualizer::NodeType;
use crate::{ActionArgs, Behavior, State, Status, UpdateEvent};
use crate::{state::State, ActionArgs, Behavior, Status, UpdateEvent};
use petgraph::dot::{Config, Dot};
use petgraph::Graph;

Expand Down Expand Up @@ -32,7 +32,7 @@ impl<K> BlackBoard<K> {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct BT<A, K> {
/// constructed behavior tree
pub state: State<A>,
state: State<A>,
/// keep the initial state
initial_behavior: Behavior<A>,
/// blackboard
Expand Down Expand Up @@ -78,12 +78,6 @@ impl<A: Clone, K> BT<A, K> {
&mut self.bb
}

/// Retrieve a mutable reference to the internal state
/// of the Behavior Tree
pub fn get_state(bt: &mut BT<A, K>) -> &mut State<A> {
&mut bt.state
}

/// The behavior tree is a stateful data structure in which the immediate
/// state of the BT is allocated and updated in heap memory through the lifetime
/// of the BT. The state of the BT is said to be `transient` meaning upon entering
Expand Down
2 changes: 1 addition & 1 deletion bonsai/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ pub use behavior::Behavior::{

pub use bt::BT;
pub use event::{Event, Timer, UpdateArgs, UpdateEvent};
pub use state::{ActionArgs, State, RUNNING};
pub use state::{ActionArgs, RUNNING};
pub use status::Status::{self, Failure, Running, Success};

mod behavior;
Expand Down
2 changes: 1 addition & 1 deletion bonsai/src/sequence.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::status::Status::*;
use crate::{event::UpdateEvent, ActionArgs, Behavior, State, Status, RUNNING};
use crate::{event::UpdateEvent, state::State, ActionArgs, Behavior, Status, RUNNING};

pub struct SequenceArgs<'a, A, E, F, B> {
pub select: bool,
Expand Down
2 changes: 1 addition & 1 deletion bonsai/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct ActionArgs<'a, E: 'a, A: 'a> {
/// Keeps track of a behavior.
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum State<A> {
pub(crate) enum State<A> {
/// Executes an action.
ActionState(A),
/// Converts `Success` into `Failure` and vice versa.
Expand Down
2 changes: 1 addition & 1 deletion bonsai/src/visualizer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(dead_code, unused_imports, unused_variables)]
use crate::{Behavior, Select, Sequence, State, BT};
use crate::{state::State, Behavior, Select, Sequence, BT};
use petgraph::{graph::Graph, stable_graph::NodeIndex, Direction::Outgoing};
use std::{collections::VecDeque, fmt::Debug};

Expand Down
2 changes: 1 addition & 1 deletion bonsai/src/when_all.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::status::Status::*;
use crate::{event::UpdateEvent, ActionArgs, State, Status, RUNNING};
use crate::{event::UpdateEvent, state::State, ActionArgs, Status, RUNNING};

// `WhenAll` and `WhenAny` share same algorithm.
//
Expand Down
Loading

0 comments on commit a6d7653

Please sign in to comment.