Skip to content

Commit

Permalink
Add process thread to tree (#251)
Browse files Browse the repository at this point in the history
* ✨ make process thread configurable

* fix editor warnings
  • Loading branch information
bitbrain authored Nov 17, 2023
1 parent 92c497c commit 6597b75
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 21 deletions.
72 changes: 56 additions & 16 deletions addons/beehave/nodes/beehave_tree.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ enum {
RUNNING
}

enum ProcessThread {
IDLE,
PHYSICS
}

signal tree_enabled
signal tree_disabled


## Wether this behavior tree should be enabled or not.
@export var enabled: bool = true:
set(value):
enabled = value
set_physics_process(enabled)

set_physics_process(enabled and process_thread == ProcessThread.PHYSICS)
set_process(enabled and process_thread == ProcessThread.IDLE)
if value:
tree_enabled.emit()
else:
Expand All @@ -28,8 +34,27 @@ signal tree_disabled
get:
return enabled


## An optional node path this behavior tree should apply to.
@export_node_path var actor_node_path : NodePath
@export_node_path var actor_node_path : NodePath:
set(anp):
actor_node_path = anp
if actor_node_path:
actor = get_node(actor_node_path)
else:
actor = get_parent()
if Engine.is_editor_hint():
update_configuration_warnings()


## Whether to run this tree in a physics or idle thread.
@export var process_thread:ProcessThread = ProcessThread.PHYSICS:
set(value):
process_thread = value
set_physics_process(enabled and process_thread == ProcessThread.PHYSICS)
set_process(enabled and process_thread == ProcessThread.IDLE)



## Custom blackboard node. An internal blackboard will be used
## if no blackboard is provided explicitly.
Expand Down Expand Up @@ -62,6 +87,7 @@ signal tree_disabled

BeehaveDebuggerMessages.unregister_tree(get_instance_id())


var actor : Node
var status : int = -1

Expand All @@ -70,33 +96,47 @@ var _process_time_metric_name : String
var _process_time_metric_value : float = 0.0
var _can_send_message: bool = false


func _ready() -> void:
if Engine.is_editor_hint():
return
if not process_thread:
process_thread = ProcessThread.PHYSICS

if actor_node_path:
actor = get_node(actor_node_path)
else:
actor = get_parent()

if not blackboard:
_internal_blackboard = Blackboard.new()
add_child(_internal_blackboard, false, Node.INTERNAL_MODE_BACK)

actor = get_parent()
if actor_node_path:
actor = get_node(actor_node_path)


# Get the name of the parent node name for metric
var parent_name = actor.name
_process_time_metric_name = "beehave [microseconds]/process_time_%s-%s" % [parent_name, get_instance_id()]
_process_time_metric_name = "beehave [microseconds]/process_time_%s-%s" % [actor.name, get_instance_id()]

set_physics_process(enabled and process_thread == ProcessThread.PHYSICS)
set_process(enabled and process_thread == ProcessThread.IDLE)

# Register custom metric to the engine
if custom_monitor:
if custom_monitor and not Engine.is_editor_hint():
Performance.add_custom_monitor(_process_time_metric_name, _get_process_time_metric_value)
BeehaveGlobalMetrics.register_tree(self)

set_physics_process(enabled)
BeehaveGlobalDebugger.register_tree(self)
BeehaveDebuggerMessages.register_tree(_get_debugger_data(self))
if Engine.is_editor_hint():
update_configuration_warnings.call_deferred()
else:
BeehaveGlobalDebugger.register_tree(self)
BeehaveDebuggerMessages.register_tree(_get_debugger_data(self))


func _physics_process(delta: float) -> void:
_process_internally(delta)


func _process(delta: float) -> void:
_process_internally(delta)


func _process_internally(delta: float) -> void:
if Engine.is_editor_hint():
return

Expand Down
4 changes: 4 additions & 0 deletions examples/BeehaveTestScene.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ script = ExtResource("3_4a21r")
[node name="BeehaveTree" type="Node" parent="ColorChangingSprite" node_paths=PackedStringArray("blackboard")]
unique_name_in_owner = true
script = ExtResource("3_hid2l")
actor_node_path = null
process_thread = null
blackboard = NodePath("../../Blackboard")
custom_monitor = true

Expand Down Expand Up @@ -67,6 +69,8 @@ texture = SubResource("CompressedTexture2D_atdvc")

[node name="AnotherTree" type="Node" parent="AnotherSprite" node_paths=PackedStringArray("blackboard")]
script = ExtResource("3_hid2l")
actor_node_path = null
process_thread = null
blackboard = NodePath("../../Blackboard")
custom_monitor = null

Expand Down
11 changes: 6 additions & 5 deletions test/UnitTestScene.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=8 format=3]
[gd_scene load_steps=8 format=3 uid="uid://diw3kjj050wdy"]

[ext_resource type="Script" path="res://addons/beehave/blackboard.gd" id="1_27ukk"]
[ext_resource type="Script" path="res://test/UnitTestScene.gd" id="1_embiv"]
Expand All @@ -17,9 +17,10 @@ script = ExtResource("1_27ukk")

[node name="Node2D" type="Node2D" parent="."]

[node name="BeehaveTree" type="Node" parent="Node2D"]
[node name="BeehaveTree" type="Node" parent="Node2D" node_paths=PackedStringArray("blackboard")]
unique_name_in_owner = true
script = ExtResource("2_phgmn")
blackboard = NodePath("@Node@22048")

[node name="SequenceComposite" type="Node" parent="Node2D/BeehaveTree"]
script = ExtResource("4_px6t4")
Expand Down Expand Up @@ -80,15 +81,15 @@ script = ExtResource("6_brhwp")

[node name="BeehaveTree" type="Node" parent="EmptyTree" node_paths=PackedStringArray("blackboard")]
script = ExtResource("2_phgmn")
blackboard = NodePath("")
blackboard = NodePath("@Node@22045")

[node name="OnlyOneActionTree" type="Node2D" parent="."]

[node name="BeehaveTree" type="Node" parent="OnlyOneActionTree" node_paths=PackedStringArray("blackboard")]
script = ExtResource("2_phgmn")
enabled = false
actor_node_path = NodePath("..")
blackboard = NodePath("")
blackboard = NodePath("@Node@22046")

[node name="CountUpAction" type="Node" parent="OnlyOneActionTree/BeehaveTree"]
script = ExtResource("6_brhwp")
Expand All @@ -98,7 +99,7 @@ script = ExtResource("6_brhwp")
[node name="BeehaveTree" type="Node" parent="DeactivatedTree" node_paths=PackedStringArray("blackboard")]
script = ExtResource("2_phgmn")
actor_node_path = NodePath("..")
blackboard = NodePath("")
blackboard = NodePath("@Node@22047")

[node name="CountUpAction" type="Node" parent="DeactivatedTree/BeehaveTree"]
script = ExtResource("6_brhwp")

0 comments on commit 6597b75

Please sign in to comment.