Skip to content

Commit

Permalink
Do not interrupt action after it finishes (#212)
Browse files Browse the repository at this point in the history
* do not interrupt action after it finishes

* removed whitespace
  • Loading branch information
lostptr authored Aug 29, 2023
1 parent 3c05bd6 commit 78b89ff
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
12 changes: 12 additions & 0 deletions addons/beehave/nodes/composites/selector.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@icon("../../icons/selector.svg")
class_name SelectorComposite extends Composite


var last_execution_index: int = 0


Expand All @@ -27,9 +28,11 @@ func tick(actor: Node, blackboard: Blackboard) -> int:

match response:
SUCCESS:
_cleanup_running_task(c, actor, blackboard)
c.after_run(actor, blackboard)
return SUCCESS
FAILURE:
_cleanup_running_task(c, actor, blackboard)
last_execution_index += 1
c.after_run(actor, blackboard)
RUNNING:
Expand All @@ -51,6 +54,15 @@ func interrupt(actor: Node, blackboard: Blackboard) -> void:
super(actor, blackboard)


## Changes `running_action` and `running_child` after the node finishes executing.
func _cleanup_running_task(finished_action: Node, actor: Node, blackboard: Blackboard):
var blackboard_name = str(actor.get_instance_id())
if finished_action == running_child:
running_child = null
if finished_action == blackboard.get_value("running_action", null, blackboard_name):
blackboard.set_value("running_action", null, blackboard_name)


func get_class_name() -> Array[StringName]:
var classes := super()
classes.push_back(&"SelectorComposite")
Expand Down
14 changes: 13 additions & 1 deletion addons/beehave/nodes/composites/sequence.gd
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ func tick(actor: Node, blackboard: Blackboard) -> int:

match response:
SUCCESS:
_cleanup_running_task(c, actor, blackboard)
successful_index += 1
c.after_run(actor, blackboard)
FAILURE:
_cleanup_running_task(c, actor, blackboard)
# Interrupt any child that was RUNNING before.
interrupt(actor, blackboard)
c.after_run(actor, blackboard)
Expand All @@ -53,11 +55,21 @@ func tick(actor: Node, blackboard: Blackboard) -> int:
func interrupt(actor: Node, blackboard: Blackboard) -> void:
_reset()
super(actor, blackboard)



func _reset() -> void:
successful_index = 0


## Changes `running_action` and `running_child` after the node finishes executing.
func _cleanup_running_task(finished_action: Node, actor: Node, blackboard: Blackboard):
var blackboard_name = str(actor.get_instance_id())
if finished_action == running_child:
running_child = null
if finished_action == blackboard.get_value("running_action", null, blackboard_name):
blackboard.set_value("running_action", null, blackboard_name)


func get_class_name() -> Array[StringName]:
var classes := super()
classes.push_back(&"SequenceComposite")
Expand Down
25 changes: 25 additions & 0 deletions test/nodes/composites/selector_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,28 @@ func test_clear_running_child_after_run() -> void:
action2.status = BeehaveNode.FAILURE
tree.tick()
assert_that(selector.running_child).is_equal(null)


func test_not_interrupt_first_after_finished() -> void:
var action3 = auto_free(load(__count_up_action).new())
selector.add_child(action3)
var running_action: Node
var blackboard_name: String = str(actor.get_instance_id())

action1.status = BeehaveNode.RUNNING
action2.status = BeehaveNode.FAILURE
action3.status = BeehaveNode.RUNNING

assert_that(selector.tick(actor, blackboard)).is_equal(BeehaveNode.RUNNING)
assert_that(action1.count).is_equal(1)
assert_that(action2.count).is_equal(0)
assert_that(action3.count).is_equal(0)

action1.status = BeehaveNode.FAILURE
assert_that(selector.tick(actor, blackboard)).is_equal(BeehaveNode.RUNNING)
assert_that(action1.count).is_equal(2)
assert_that(action2.count).is_equal(1)
assert_that(action3.count).is_equal(1)

selector.remove_child(action3)

22 changes: 22 additions & 0 deletions test/nodes/composites/sequence_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,25 @@ func test_clear_running_child_after_run() -> void:
action2.status = BeehaveNode.SUCCESS
tree.tick()
assert_that(sequence.running_child).is_equal(null)


func test_not_interrupt_first_after_finished() -> void:
var action3 = auto_free(load(__count_up_action).new())
sequence.add_child(action3)

action1.status = BeehaveNode.RUNNING
action2.status = BeehaveNode.SUCCESS
action3.status = BeehaveNode.RUNNING

assert_that(sequence.tick(actor, blackboard)).is_equal(BeehaveNode.RUNNING)
assert_that(action1.count).is_equal(1)
assert_that(action2.count).is_equal(0)
assert_that(action3.count).is_equal(0)

action1.status = BeehaveNode.SUCCESS
assert_that(sequence.tick(actor, blackboard)).is_equal(BeehaveNode.RUNNING)
assert_that(action1.count).is_equal(2)
assert_that(action2.count).is_equal(1)
assert_that(action3.count).is_equal(1)

sequence.remove_child(action3)

0 comments on commit 78b89ff

Please sign in to comment.