diff --git a/Editor/Tests/BlackboardTest.cs b/Editor/Tests/BlackboardTest.cs index 9a2cfdd5..8568f713 100644 --- a/Editor/Tests/BlackboardTest.cs +++ b/Editor/Tests/BlackboardTest.cs @@ -1,7 +1,7 @@ using NUnit.Framework; namespace NPBehave { - #pragma warning disable 618 // deprecation +#pragma warning disable 618 // deprecation public class BlackboardTest { @@ -19,7 +19,7 @@ public void SetUp() public void ShouldNotNotifyObservers_WhenNoClockUpdate() { bool notified = false; - this.sut.AddObserver("test", (Blackboard.Type type, object value) => + this.sut.AddObserver("test", ( Blackboard.Type type, object value ) => { notified = true; }); @@ -32,7 +32,7 @@ public void ShouldNotNotifyObservers_WhenNoClockUpdate() public void ShouldNotifyObservers_WhenClockUpdate() { bool notified = false; - this.sut.AddObserver("test", (Blackboard.Type type, object value) => + this.sut.AddObserver("test", ( Blackboard.Type type, object value ) => { notified = true; }); @@ -49,13 +49,13 @@ public void ShouldNotNotifyObserver_WhenRemovedDuringOtherObserver() System.Action obs1 = null; System.Action obs2 = null; - obs1 = (Blackboard.Type type, object value) => + obs1 = ( Blackboard.Type type, object value ) => { Assert.IsFalse(notified); notified = true; this.sut.RemoveObserver("test", obs2); }; - obs2 = (Blackboard.Type type, object value) => + obs2 = ( Blackboard.Type type, object value ) => { Assert.IsFalse(notified); notified = true; @@ -86,8 +86,52 @@ public void NewDefaultValuesShouldBeCompatible() { Assert.AreEqual(this.sut.Get("not-existing"), this.sut.GetBool("not-existing")); Assert.AreEqual(this.sut.Get("not-existing"), this.sut.GetInt("not-existing")); -// Assert.AreEqual(this.sut.Get("not-existing"), this.sut.GetFloat("not-existing")); + // Assert.AreEqual(this.sut.Get("not-existing"), this.sut.GetFloat("not-existing")); Assert.AreEqual(this.sut.Get("not-existing"), this.sut.GetVector3("not-existing")); } + + + // check for https://github.com/meniku/NPBehave/issues/17 + [Test] + public void ShouldListenToEvents_WhenUsingChildBlackboard() + { + Blackboard rootBlackboard = new Blackboard(clock); + Blackboard blackboard = new Blackboard(rootBlackboard, clock); + + // our mock nodes we want to query for status + MockNode firstChild = new MockNode(false); // false -> fail when aborted + MockNode secondChild = new MockNode(false); + + // conditions for each subtree that listen the BB for events + BlackboardCondition firstCondition = new BlackboardCondition("branch1", Operator.IS_EQUAL, true, Stops.IMMEDIATE_RESTART, firstChild); + BlackboardCondition secondCondition = new BlackboardCondition("branch2", Operator.IS_EQUAL, true, Stops.IMMEDIATE_RESTART, secondChild); + + // set up the tree + Selector selector = new Selector(firstCondition, secondCondition); + TestRoot behaviorTree = new TestRoot(blackboard, clock, selector); + + // intially we want to activate branch2 + rootBlackboard.Set("branch2", true); + + // start the tree + behaviorTree.Start(); + + // tick the timer to ensure the blackboard notifies the nodes + clock.Update(0.1f); + + // verify the second child is running + Assert.AreEqual(Node.State.INACTIVE, firstChild.CurrentState); + Assert.AreEqual(Node.State.ACTIVE, secondChild.CurrentState); + + // change keys so the first conditions get true, too + rootBlackboard.Set("branch1", true); + + // tick the timer to ensure the blackboard notifies the nodes + clock.Update(0.1f); + + // now we should be in branch1 + Assert.AreEqual(Node.State.ACTIVE, firstChild.CurrentState); + Assert.AreEqual(Node.State.INACTIVE, secondChild.CurrentState); + } } } \ No newline at end of file diff --git a/Scripts/Blackboard.cs b/Scripts/Blackboard.cs index 2dc86cf9..d429f4aa 100644 --- a/Scripts/Blackboard.cs +++ b/Scripts/Blackboard.cs @@ -286,6 +286,7 @@ private void NotifiyObservers() foreach (Blackboard child in children) { child.notifications.AddRange(notifications); + child.clock.AddTimer(0f, 0, child.NotifiyObservers); } notifications.Clear();