Skip to content

Commit b532a12

Browse files
committed
refactor(decorators): automatic handling of missing child
Decorators no longer require missing child logic built into them. They will always return a failure and exit early if a child is missing.
1 parent 466a074 commit b532a12

File tree

5 files changed

+32
-35
lines changed

5 files changed

+32
-35
lines changed

Assets/FluidBehaviorTree/Runtime/Decorators/DecoratorBase.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ public abstract class DecoratorBase : GenericTaskBase, ITaskParent {
2020

2121
public override TaskStatus Update () {
2222
base.Update();
23+
24+
if (Child == null) {
25+
if (Application.isPlaying) Debug.LogWarning(
26+
$"Decorator {Name} has no child. Force returning failure. Please fix");
27+
return TaskStatus.Failure;
28+
}
29+
2330
var status = OnUpdate();
2431
LastStatus = status;
2532

Assets/FluidBehaviorTree/Runtime/Decorators/DecoratorGeneric.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ public class DecoratorGeneric : DecoratorBase {
66
public Func<ITask, TaskStatus> updateLogic;
77

88
protected override TaskStatus OnUpdate () {
9-
if (Child == null) {
10-
return TaskStatus.Success;
11-
}
12-
139
if (updateLogic != null) {
1410
return updateLogic(Child);
1511
}

Assets/FluidBehaviorTree/Runtime/Decorators/Inverter.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ public class Inverter : DecoratorBase {
55
public override string IconPath { get; } = $"{PACKAGE_ROOT}/Invert.png";
66

77
protected override TaskStatus OnUpdate () {
8-
if (Child == null) {
9-
return TaskStatus.Success;
10-
}
11-
128
var childStatus = Child.Update();
139
var status = childStatus;
1410

Assets/FluidBehaviorTree/Tests/Editor/Decorators/DecoratorTest.cs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
namespace CleverCrow.Fluid.BTs.Testing {
77
public class DecoratorTest {
8+
private DecoratorExample _decorator;
9+
810
public class DecoratorExample : DecoratorBase {
911
public TaskStatus status;
1012

@@ -13,46 +15,49 @@ protected override TaskStatus OnUpdate () {
1315
}
1416
}
1517

16-
public class EnabledProperty {
18+
[SetUp]
19+
public void BeforeEach () {
20+
_decorator = new DecoratorExample();
21+
_decorator.AddChild(A.TaskStub().Build());
22+
}
23+
24+
public class EnabledProperty : DecoratorTest {
1725
[Test]
1826
public void Returns_true_if_child_is_set () {
19-
var decorator = new DecoratorExample();
20-
decorator.AddChild(A.TaskStub().Build());
21-
22-
Assert.IsTrue(decorator.Enabled);
27+
Assert.IsTrue(_decorator.Enabled);
2328
}
2429

2530
[Test]
2631
public void Returns_false_if_child_is_set_but_set_to_false () {
27-
var decorator = new DecoratorExample();
28-
decorator.AddChild(A.TaskStub().Build());
29-
decorator.Enabled = false;
32+
_decorator.Enabled = false;
3033

31-
Assert.IsFalse(decorator.Enabled);
34+
Assert.IsFalse(_decorator.Enabled);
3235
}
3336
}
3437

35-
public class UpdateMethod {
38+
public class UpdateMethod : DecoratorTest {
3639
[Test]
3740
public void Sets_LastUpdate_to_returned_status_value () {
38-
var decorator = new DecoratorExample();
39-
decorator.status = TaskStatus.Failure;
41+
_decorator.status = TaskStatus.Failure;
42+
43+
_decorator.Update();
4044

41-
decorator.Update();
45+
Assert.AreEqual(TaskStatus.Failure, _decorator.LastStatus);
46+
}
4247

43-
Assert.AreEqual(TaskStatus.Failure, decorator.LastStatus);
48+
[Test]
49+
public void It_should_return_failure_if_a_child_is_missing () {
50+
_decorator.Children.RemoveAt(0);
51+
Assert.AreEqual(TaskStatus.Failure, _decorator.Update());
4452
}
4553
}
4654

47-
public class EndMethod {
55+
public class EndMethod : DecoratorTest {
4856
[Test]
4957
public void Calls_end_on_child () {
50-
var decorator = new DecoratorExample();
51-
decorator.AddChild(A.TaskStub().Build());
52-
53-
decorator.End();
58+
_decorator.End();
5459

55-
decorator.Child.Received(1).End();
60+
_decorator.Child.Received(1).End();
5661
}
5762
}
5863
}

Assets/FluidBehaviorTree/Tests/Editor/Decorators/InverterTest.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ public void Returns_continue_when_child_returns_continue () {
2828

2929
Assert.AreEqual(TaskStatus.Continue, inverter.Update());
3030
}
31-
32-
[Test]
33-
public void Does_not_crash_if_no_child () {
34-
var inverter = new Inverter();
35-
36-
inverter.Update();
37-
}
3831
}
3932
}
4033
}

0 commit comments

Comments
 (0)