Skip to content

Commit 8cd64f5

Browse files
Implement blackboard immutable reference function (#47)
First of all, thank you for creating such a great framework. In the code I’m currently working on, I was using an older version of bonsai-bt. However, as the version was updated, an issue arose. The interface was modified to integrate the blackboard into the bt, which caused problems with the existing logic that relied on an immutable borrow of the blackboard. To address this, I added an immutable reference function. Could you take a look and let me know if it looks okay?
1 parent 1aa74af commit 8cd64f5

7 files changed

Lines changed: 16 additions & 10 deletions

File tree

bonsai/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ name = "bonsai-bt"
1212
readme = "../README.md"
1313
repository = "https://github.com/sollimann/bonsai.git"
1414
rust-version = "1.80.0"
15-
version = "0.9.0"
15+
version = "0.10.0"
1616

1717
[lib]
1818
name = "bonsai_bt"

bonsai/src/bt.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,15 @@ impl<A: Clone, B> BT<A, B> {
6767
}
6868
}
6969

70+
/// Retrieve an immutable reference to the blackboard for
71+
/// this Behavior Tree
72+
pub fn blackboard(&self) -> &B {
73+
&self.bb
74+
}
75+
7076
/// Retrieve a mutable reference to the blackboard for
7177
/// this Behavior Tree
72-
pub fn get_blackboard(&mut self) -> &mut B {
78+
pub fn blackboard_mut(&mut self) -> &mut B {
7379
&mut self.bb
7480
}
7581

bonsai/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
//! }).unwrap();
6464
//!
6565
//! // update counter in blackboard
66-
//! let bb = bt.get_blackboard();
66+
//! let bb = bt.blackboard_mut();
6767
//!
6868
//! bb.entry("count".to_string())
6969
//! .and_modify(|count| *count = acc)
@@ -107,7 +107,7 @@
107107
//! let a = tick(a, 0.5, &mut bt);
108108
//! assert_eq!(a, 1);
109109
//!
110-
//! let bb = bt.get_blackboard();
110+
//! let bb = bt.blackboard_mut();
111111
//! let count = bb.get("count").unwrap();
112112
//! assert_eq!(*count, 1);
113113
//!

bonsai/tests/blackboard_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn tick(mut acc: i32, dt: f64, bt: &mut BT<TestActions, HashMap<String, i32>>) -
3131
.unwrap();
3232

3333
// update counter in blackboard
34-
let bb = bt.get_blackboard();
34+
let bb = bt.blackboard_mut();
3535

3636
bb.entry("count".to_string())
3737
.and_modify(|count| *count = acc)
@@ -64,7 +64,7 @@ fn test_crate_bt() {
6464
let a = tick(a, 0.5, &mut bt);
6565
assert_eq!(a, 1);
6666

67-
let bb = bt.get_blackboard();
67+
let bb = bt.blackboard_mut();
6868
let count = bb.get("count").unwrap();
6969
assert_eq!(*count, 1);
7070
}

examples/src/3d/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ fn game_tick(
9090
let e: Event = UpdateArgs { dt }.into();
9191

9292
// get data from blackboard
93-
let db = bt.get_blackboard();
93+
let db = bt.blackboard_mut();
9494
let inc: u64 = db.get("count").map_or(Some(0), |x| x.as_u64()).unwrap();
9595

9696
let mut last_pos = mouse_pos(0.0, 0.0);
@@ -197,7 +197,7 @@ fn game_tick(
197197
).unwrap();
198198

199199
// update blackboard
200-
let db = bt.get_blackboard();
200+
let db = bt.blackboard_mut();
201201

202202
// update count
203203
let _count = db

examples/src/boids/boid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub fn game_tick(dt: f32, cursor: mint::Point2<f32>, boid: &mut Boid, other_boid
6060

6161
// unwrap bt for boid
6262
let mut bt = boid.bt.clone();
63-
let db = bt.get_blackboard();
63+
let db = bt.blackboard_mut();
6464
let win_width: f32 = *db.get("win_width").unwrap();
6565
let win_height: f32 = *db.get("win_height").unwrap();
6666

examples/src/simple_npc_ai/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ fn main() {
174174
}
175175
println!(
176176
"NPC shot {} times during the simulation.",
177-
bt.get_blackboard().times_shot
177+
bt.blackboard_mut().times_shot
178178
);
179179
}
180180

0 commit comments

Comments
 (0)