Skip to content

Commit b7e334a

Browse files
authored
Cooldown should save running_child so that its non-ActionLeaf childre… (#376)
* Cooldown should save running_child so that its non-ActionLeaf children can propagate interrupt and after_run events * Added tests for cooldown.gd
1 parent 67e4532 commit b7e334a

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
lines changed

addons/beehave/nodes/decorators/cooldown.gd

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ func tick(actor: Node, blackboard: Blackboard) -> int:
3939
blackboard.set_value("last_condition", c, str(actor.get_instance_id()))
4040
blackboard.set_value("last_condition_status", response, str(actor.get_instance_id()))
4141

42-
if response == RUNNING and c is ActionLeaf:
42+
if response == RUNNING:
4343
running_child = c
44-
blackboard.set_value("running_action", c, str(actor.get_instance_id()))
44+
if c is ActionLeaf:
45+
blackboard.set_value("running_action", c, str(actor.get_instance_id()))
4546

4647
if response != RUNNING:
4748
blackboard.set_value(cache_key, wait_time, str(actor.get_instance_id()))

test/actions/mock_action.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class_name MockAction
22
extends ActionLeaf
33

4-
@export_enum("Success", "Failure") var final_result: int = 0
4+
@export_enum("Success", "Failure", "Running") var final_result: int = 0
55
@export var running_frame_count: int = 0
66

77
signal started_running(actor, blackboard)

test/composites/mock_composite.gd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class_name MockComposite
2+
extends Composite
3+
4+
@export_enum("Success", "Failure", "Running") var final_result: int = 0
5+
6+
signal interrupted(actor, blackboard)
7+
8+
func interrupt(actor: Node, blackboard: Blackboard) -> void:
9+
interrupted.emit(actor, blackboard)
10+
11+
func tick(_actor: Node, _blackboard: Blackboard) -> int:
12+
return final_result

test/nodes/decorators/cooldown_test.gd

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,30 @@ extends GdUnitTestSuite
66

77
# TestSuite generated from
88
const __source = "res://addons/beehave/nodes/decorators/cooldown.gd"
9-
const __action = "res://test/actions/count_up_action.gd"
9+
const __action = "res://test/actions/mock_action.gd"
10+
const __composite = "res://test/composites/mock_composite.gd"
1011
const __tree = "res://addons/beehave/nodes/beehave_tree.gd"
1112
const __blackboard = "res://addons/beehave/blackboard.gd"
1213

1314
var tree: BeehaveTree
14-
var action: ActionLeaf
15+
var action: MockAction
16+
var composite: MockComposite
1517
var cooldown: CooldownDecorator
1618
var runner: GdUnitSceneRunner
1719

1820

1921
func before_test() -> void:
2022
tree = auto_free(load(__tree).new())
2123
action = auto_free(load(__action).new())
24+
composite = auto_free(load(__composite).new())
2225
cooldown = auto_free(load(__source).new())
2326

27+
# action setup
28+
action.interrupted.connect(_on_interrupted)
29+
30+
# composite setup
31+
composite.interrupted.connect(_on_interrupted)
32+
2433
var actor = auto_free(Node2D.new())
2534
var blackboard = auto_free(load(__blackboard).new())
2635

@@ -32,13 +41,46 @@ func before_test() -> void:
3241
runner = scene_runner(tree)
3342

3443

44+
func after_test():
45+
# resets blackboard
46+
tree.blackboard.set_value("interrupted", 0)
47+
48+
3549
func test_running_then_fail() -> void:
3650
cooldown.wait_time = 1.0
37-
action.status = BeehaveNode.RUNNING
51+
action.final_result = BeehaveNode.RUNNING
3852
assert_that(tree.tick()).is_equal(BeehaveNode.RUNNING)
39-
action.status = BeehaveNode.SUCCESS
53+
action.final_result = BeehaveNode.SUCCESS
4054
assert_that(tree.tick()).is_equal(BeehaveNode.SUCCESS)
41-
action.status = BeehaveNode.RUNNING
55+
action.final_result = BeehaveNode.RUNNING
4256
assert_that(tree.tick()).is_equal(BeehaveNode.FAILURE)
4357
await runner.simulate_frames(1, 2000)
4458
assert_that(tree.tick()).is_equal(BeehaveNode.RUNNING)
59+
60+
61+
func test_interrupt_propagates_when_actionleaf() -> void:
62+
cooldown.wait_time = 1.0
63+
action.final_result = BeehaveNode.RUNNING
64+
tree.tick()
65+
tree.interrupt()
66+
67+
var times_interrupted = tree.blackboard.get_value("interrupted", 0)
68+
assert_that(times_interrupted).is_equal(1)
69+
70+
71+
func test_interrupt_propagates_when_composite() -> void:
72+
cooldown.remove_child(action)
73+
cooldown.add_child(composite)
74+
75+
cooldown.wait_time = 1.0
76+
composite.final_result = BeehaveNode.RUNNING
77+
tree.tick()
78+
tree.interrupt()
79+
80+
var times_interrupted = tree.blackboard.get_value("interrupted", 0)
81+
assert_that(times_interrupted).is_equal(1)
82+
83+
84+
func _on_interrupted(_actor, blackboard):
85+
var started = blackboard.get_value("interrupted", 0)
86+
blackboard.set_value("interrupted", started + 1)

0 commit comments

Comments
 (0)