Skip to content

Commit

Permalink
Add missing notification for child blackboards when a key on the pare…
Browse files Browse the repository at this point in the history
…nt blackboard channges. Fixes #17
  • Loading branch information
meniku committed Jun 24, 2019
1 parent c2b5795 commit 0f44cea
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
56 changes: 50 additions & 6 deletions Editor/Tests/BlackboardTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using NUnit.Framework;
namespace NPBehave
{
#pragma warning disable 618 // deprecation
#pragma warning disable 618 // deprecation

public class BlackboardTest
{
Expand All @@ -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;
});
Expand All @@ -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;
});
Expand All @@ -49,13 +49,13 @@ public void ShouldNotNotifyObserver_WhenRemovedDuringOtherObserver()
System.Action<Blackboard.Type, object> obs1 = null;
System.Action<Blackboard.Type, object> 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;
Expand Down Expand Up @@ -86,8 +86,52 @@ public void NewDefaultValuesShouldBeCompatible()
{
Assert.AreEqual(this.sut.Get<bool>("not-existing"), this.sut.GetBool("not-existing"));
Assert.AreEqual(this.sut.Get<int>("not-existing"), this.sut.GetInt("not-existing"));
// Assert.AreEqual(this.sut.Get<float>("not-existing"), this.sut.GetFloat("not-existing"));
// Assert.AreEqual(this.sut.Get<float>("not-existing"), this.sut.GetFloat("not-existing"));
Assert.AreEqual(this.sut.Get<UnityEngine.Vector3>("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);
}
}
}
1 change: 1 addition & 0 deletions Scripts/Blackboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 0f44cea

Please sign in to comment.