Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions addons/beehave/nodes/beehave_tree.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class_name BeehaveTree extends Node
enum { SUCCESS, FAILURE, RUNNING }

enum ProcessThread { IDLE, PHYSICS, MANUAL }
enum TickMode { BOTH, PROCESS, PHYSICS, MANUAL }

signal tree_enabled
signal tree_disabled
Expand All @@ -25,6 +26,10 @@ signal tree_disabled
get:
return enabled

## How automatic ticking of the tree should be handled. The default is to
## tick on both _process and _physics_process.
@export var tick_mode: TickMode = TickMode.BOTH

## How often the tree should tick, in frames. The default value of 1 means
## tick() runs every frame.
@export var tick_rate: int = 1
Expand Down Expand Up @@ -164,11 +169,13 @@ func _on_scene_tree_node_added_removed(node: Node, is_added: bool) -> void:


func _physics_process(_delta: float) -> void:
tick()
if tick_mode == TickMode.BOTH or tick_mode == TickMode.PHYSICS:
tick()


func _process(_delta: float) -> void:
tick()
if tick_mode == TickMode.BOTH or tick_mode == TickMode.PROCESS:
tick()


func tick() -> int:
Expand Down Expand Up @@ -202,13 +209,13 @@ func tick() -> int:
if status != RUNNING:
blackboard.set_value("running_action", null, str(actor.get_instance_id()))
child.after_run(actor, blackboard)

if _can_send_message and not Engine.is_editor_hint():
BeehaveDebuggerMessages.process_end(get_instance_id(), blackboard.get_debug_data())

# Check the cost for this frame and save it for metric report
_process_time_metric_value = Time.get_ticks_usec() - start_time

return status


Expand Down
63 changes: 45 additions & 18 deletions test/beehave_tree_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,33 @@ func test_normal_tick() -> void:
scene.beehave_tree._physics_process(1.0)
assert_that(scene.beehave_tree.status).is_equal(BeehaveNode.SUCCESS)

func test_process_tick() -> void:
var scene = create_scene()
scene_runner(scene)
scene.beehave_tree.tick_mode = scene.beehave_tree.TickMode.PROCESS
scene.beehave_tree._physics_process(1.0)
assert_that(scene.beehave_tree.status).is_equal(-1)
scene.beehave_tree._process(1.0)
assert_that(scene.beehave_tree.status).is_equal(BeehaveNode.SUCCESS)

func test_physics_process_tick() -> void:
var scene = create_scene()
scene_runner(scene)
scene.beehave_tree.tick_mode = scene.beehave_tree.TickMode.PHYSICS
scene.beehave_tree._process(1.0)
assert_that(scene.beehave_tree.status).is_equal(-1)
scene.beehave_tree._physics_process(1.0)
assert_that(scene.beehave_tree.status).is_equal(BeehaveNode.SUCCESS)

func test_manual_tick() -> void:
var scene = create_scene()
scene_runner(scene)
scene.beehave_tree.tick_mode = scene.beehave_tree.TickMode.MANUAL
scene.beehave_tree._process(1.0)
scene.beehave_tree._physics_process(1.0)
assert_that(scene.beehave_tree.status).is_equal(-1)
scene.beehave_tree.tick()
assert_that(scene.beehave_tree.status).is_equal(BeehaveNode.SUCCESS)

func test_low_tick_rate() -> void:
var scene = create_scene()
Expand Down Expand Up @@ -121,11 +148,11 @@ func test_manual_mode_does_not_auto_tick() -> void:
scene_runner(scene)
scene.beehave_tree.process_thread = BeehaveTree.ProcessThread.MANUAL
scene.beehave_tree.enabled = true

# Set up count up action
scene.count_up_action.status = BeehaveNode.RUNNING
scene.beehave_tree.blackboard.set_value("custom_value", 0)

# Wait a bit to verify no auto-ticks
await get_tree().create_timer(0.1).timeout
assert_that(scene.beehave_tree.blackboard.get_value("custom_value")).is_equal(0)
Expand All @@ -136,15 +163,15 @@ func test_manual_mode_can_tick_manually() -> void:
scene_runner(scene)
scene.beehave_tree.process_thread = BeehaveTree.ProcessThread.MANUAL
scene.beehave_tree.enabled = true

# Set up count up action
scene.count_up_action.status = BeehaveNode.RUNNING
scene.beehave_tree.blackboard.set_value("custom_value", 0)

# Manual tick should increase counter
scene.beehave_tree.tick()
assert_that(scene.beehave_tree.blackboard.get_value("custom_value")).is_equal(1)

# Another manual tick should increase counter again
scene.beehave_tree.tick()
assert_that(scene.beehave_tree.blackboard.get_value("custom_value")).is_equal(2)
Expand All @@ -156,23 +183,23 @@ func test_manual_mode_respects_tick_rate() -> void:
scene.beehave_tree.process_thread = BeehaveTree.ProcessThread.MANUAL
scene.beehave_tree.tick_rate = 3
scene.beehave_tree.enabled = true

# Set up count up action
scene.count_up_action.status = BeehaveNode.RUNNING
scene.beehave_tree.blackboard.set_value("custom_value", 0)

# First tick should increase counter
scene.beehave_tree.tick()
assert_that(scene.beehave_tree.blackboard.get_value("custom_value")).is_equal(1)
# Second tick should not yet increase counter

# Second tick should not yet increase counter
scene.beehave_tree.tick()
assert_that(scene.beehave_tree.blackboard.get_value("custom_value")).is_equal(1)
# Second tick should not yet increase counter

# Second tick should not yet increase counter
scene.beehave_tree.tick()
assert_that(scene.beehave_tree.blackboard.get_value("custom_value")).is_equal(1)

# Fourth tick should increase counter
scene.beehave_tree.tick()
assert_that(scene.beehave_tree.blackboard.get_value("custom_value")).is_equal(2)
Expand All @@ -183,25 +210,25 @@ func test_manual_mode_can_be_disabled() -> void:
scene_runner(scene)
scene.beehave_tree.process_thread = BeehaveTree.ProcessThread.MANUAL
scene.beehave_tree.enabled = true

# Set up count up action
scene.count_up_action.status = BeehaveNode.RUNNING
scene.beehave_tree.blackboard.set_value("custom_value", 0)

# Should be able to tick when enabled
scene.beehave_tree.tick()
assert_that(scene.beehave_tree.blackboard.get_value("custom_value")).is_equal(1)

# Disable the tree
scene.beehave_tree.disable()

# Should not be able to tick when disabled
scene.beehave_tree.tick()
assert_that(scene.beehave_tree.blackboard.get_value("custom_value")).is_equal(1) # Value should not change

# Re-enable the tree
scene.beehave_tree.enable()

# Should be able to tick again
scene.beehave_tree.tick()
assert_that(scene.beehave_tree.blackboard.get_value("custom_value")).is_equal(2)