From dbbc74bb648aa6b94803d75bf3e97f24f0573e09 Mon Sep 17 00:00:00 2001 From: Joseph Cumines Date: Fri, 31 Jan 2020 00:50:49 +1000 Subject: [PATCH] refactor(sequence+selector): pass through errors unmodified Any error values will not be munged directly by the Sequence or Selector implementations of Tick. The primary reason for this removal was it's general uselessness vs the amount of potential log spam that may be generated, when propagating an error upwards through a large tree. I also noticed I had flipped the comments somehow, oops. --- any_test.go | 4 ++-- selector.go | 10 ++++------ sequence.go | 10 ++++------ 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/any_test.go b/any_test.go index e085228..29a421c 100644 --- a/any_test.go +++ b/any_test.go @@ -137,7 +137,7 @@ func ExampleAny_resetBehavior() { //1 //failure //1 - //failure behaviortree.Sequence encountered error with child at index 0: some_error + //failure some_error //1 //2 //success @@ -282,7 +282,7 @@ func TestAny_nilChildTick(t *testing.T) { if status != Failure { t.Error(status) } - if err == nil || err.Error() != `behaviortree.Sequence encountered error with child at index 0: behaviortree.Node cannot tick a node with a nil tick` { + if err == nil || err.Error() != `behaviortree.Node cannot tick a node with a nil tick` { t.Error(err) } } diff --git a/selector.go b/selector.go index 9b0add3..ef062b8 100644 --- a/selector.go +++ b/selector.go @@ -16,15 +16,13 @@ package behaviortree -import "fmt" - -// Selector is a tick implementation that will succeed if no children fail, returning running if any children return -// running, propagating any error +// Selector is a tick implementation that ticks each child sequentially, until the the first error (returning the +// error), the first non-failure status (returning the status), or all children are ticked (returning failure) func Selector(children []Node) (Status, error) { - for i, c := range children { + for _, c := range children { status, err := c.Tick() if err != nil { - return Failure, fmt.Errorf("behaviortree.Selector encountered error with child at index %d: %s", i, err.Error()) + return Failure, err } if status == Running { return Running, nil diff --git a/sequence.go b/sequence.go index 23786f4..5aa4931 100644 --- a/sequence.go +++ b/sequence.go @@ -16,15 +16,13 @@ package behaviortree -import "fmt" - -// Sequence is a tick implementation that will succeed if any children succeed, returning running if any children -// return running, propagating any error +// Sequence is a tick implementation that ticks each child sequentially, until the the first error (returning the +// error), the first non-success status (returning the status), or all children are ticked (returning success) func Sequence(children []Node) (Status, error) { - for i, c := range children { + for _, c := range children { status, err := c.Tick() if err != nil { - return Failure, fmt.Errorf("behaviortree.Sequence encountered error with child at index %d: %s", i, err.Error()) + return Failure, err } if status == Running { return Running, nil