Skip to content

Commit

Permalink
refactor(sequence+selector): pass through errors unmodified
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joeycumines committed Jan 30, 2020
1 parent a5b8029 commit dbbc74b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
4 changes: 2 additions & 2 deletions any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func ExampleAny_resetBehavior() {
//1
//failure <nil>
//1
//failure behaviortree.Sequence encountered error with child at index 0: some_error
//failure some_error
//1
//2
//success <nil>
Expand Down Expand Up @@ -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)
}
}
Expand Down
10 changes: 4 additions & 6 deletions selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dbbc74b

Please sign in to comment.