Skip to content

Commit

Permalink
🐛 ensure blackboard is always available (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbrain authored Dec 4, 2023
1 parent 474a541 commit d607a20
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 43 deletions.
9 changes: 7 additions & 2 deletions addons/beehave/nodes/beehave_tree.gd
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ signal tree_disabled
_internal_blackboard = Blackboard.new()
add_child(_internal_blackboard, false, Node.INTERNAL_MODE_BACK)
get:
# in case blackboard is accessed before this node is,
# we need to ensure that the internal blackboard is used.
if not blackboard and not _internal_blackboard:
_internal_blackboard = Blackboard.new()
add_child(_internal_blackboard, false, Node.INTERNAL_MODE_BACK)
return blackboard if blackboard else _internal_blackboard

## When enabled, this tree is tracked individually
Expand Down Expand Up @@ -107,8 +112,8 @@ func _ready() -> void:
actor = get_parent()

if not blackboard:
_internal_blackboard = Blackboard.new()
add_child(_internal_blackboard, false, Node.INTERNAL_MODE_BACK)
# invoke setter to auto-initialise the blackboard.
self.blackboard = null

# Get the name of the parent node name for metric
_process_time_metric_name = "beehave [microseconds]/process_time_%s-%s" % [actor.name, get_instance_id()]
Expand Down
File renamed without changes.
70 changes: 35 additions & 35 deletions examples/BeehaveTestScene.tscn → examples/beehave_test_scene.tscn
Original file line number Diff line number Diff line change
@@ -1,103 +1,103 @@
[gd_scene load_steps=12 format=3 uid="uid://dxkotdttpvmb0"]

[ext_resource type="Script" path="res://examples/BeehaveTestScene.gd" id="1_s45or"]
[ext_resource type="Texture2D" uid="uid://uq5muso23nn5" path="res://splash.png" id="2_yf6a3"]
[ext_resource type="Script" path="res://examples/ColorChangingSprite.gd" id="3_4a21r"]
[ext_resource type="Script" path="res://addons/beehave/nodes/beehave_tree.gd" id="3_hid2l"]
[ext_resource type="Script" path="res://addons/beehave/nodes/composites/selector_reactive.gd" id="5_3qraw"]
[ext_resource type="Script" path="res://addons/beehave/nodes/composites/sequence.gd" id="5_ifctq"]
[ext_resource type="PackedScene" uid="uid://dl8hr0kerrxsv" path="res://examples/conditions/HasPositivePosition.tscn" id="6_7qx5k"]
[ext_resource type="PackedScene" uid="uid://c3msbcyat8v0n" path="res://examples/actions/SetModulateColor.tscn" id="7_b4xjm"]
[ext_resource type="PackedScene" uid="uid://dgbwmcsll3o7t" path="res://examples/conditions/HasNegativePosition.tscn" id="8_dorjh"]
[ext_resource type="Script" path="res://addons/beehave/blackboard.gd" id="9_k8008"]
[ext_resource type="Script" path="res://examples/beehave_test_scene.gd" id="1_18xg1"]
[ext_resource type="Texture2D" uid="uid://uq5muso23nn5" path="res://splash.png" id="2_2q57p"]
[ext_resource type="Script" path="res://examples/ColorChangingSprite.gd" id="3_shmq8"]
[ext_resource type="Script" path="res://addons/beehave/nodes/beehave_tree.gd" id="4_yb1gg"]
[ext_resource type="Script" path="res://addons/beehave/nodes/composites/selector_reactive.gd" id="5_l2xef"]
[ext_resource type="Script" path="res://addons/beehave/nodes/composites/sequence.gd" id="6_jtsdg"]
[ext_resource type="PackedScene" uid="uid://dl8hr0kerrxsv" path="res://examples/conditions/HasPositivePosition.tscn" id="7_iqb0a"]
[ext_resource type="PackedScene" uid="uid://c3msbcyat8v0n" path="res://examples/actions/SetModulateColor.tscn" id="8_0v48s"]
[ext_resource type="PackedScene" uid="uid://dgbwmcsll3o7t" path="res://examples/conditions/HasNegativePosition.tscn" id="9_x8ohm"]
[ext_resource type="Script" path="res://addons/beehave/blackboard.gd" id="10_y2p03"]

[sub_resource type="CompressedTexture2D" id="CompressedTexture2D_atdvc"]
load_path = "res://.godot/imported/icon.svg-218a8f2b3041327d8a5756f3a245f83b.ctex"

[node name="BeehaveTestScene" type="Node2D"]
script = ExtResource("1_s45or")
script = ExtResource("1_18xg1")

[node name="Background" type="Sprite2D" parent="."]
modulate = Color(0.882353, 0.882353, 0.882353, 0.141176)
position = Vector2(32, 46)
scale = Vector2(2.63487, 2.57664)
texture = ExtResource("2_yf6a3")
texture = ExtResource("2_2q57p")

[node name="ColorChangingSprite" type="Sprite2D" parent="."]
scale = Vector2(0.1, 0.1)
texture = SubResource("CompressedTexture2D_atdvc")
script = ExtResource("3_4a21r")
script = ExtResource("3_shmq8")

[node name="BeehaveTree" type="Node" parent="ColorChangingSprite" node_paths=PackedStringArray("blackboard")]
unique_name_in_owner = true
script = ExtResource("3_hid2l")
script = ExtResource("4_yb1gg")
blackboard = NodePath("../../Blackboard")
custom_monitor = true

[node name="SelectorComposite" type="Node" parent="ColorChangingSprite/BeehaveTree"]
script = ExtResource("5_3qraw")
script = ExtResource("5_l2xef")

[node name="SequenceComposite" type="Node" parent="ColorChangingSprite/BeehaveTree/SelectorComposite"]
script = ExtResource("5_ifctq")
script = ExtResource("6_jtsdg")

[node name="HasPositivePosition" parent="ColorChangingSprite/BeehaveTree/SelectorComposite/SequenceComposite" instance=ExtResource("6_7qx5k")]
[node name="HasPositivePosition" parent="ColorChangingSprite/BeehaveTree/SelectorComposite/SequenceComposite" instance=ExtResource("7_iqb0a")]

[node name="SetModulateColor" parent="ColorChangingSprite/BeehaveTree/SelectorComposite/SequenceComposite" instance=ExtResource("7_b4xjm")]
[node name="SetModulateColor" parent="ColorChangingSprite/BeehaveTree/SelectorComposite/SequenceComposite" instance=ExtResource("8_0v48s")]
modulate_color = Color(1, 0, 0, 1)

[node name="SequenceComposite2" type="Node" parent="ColorChangingSprite/BeehaveTree/SelectorComposite"]
script = ExtResource("5_ifctq")
script = ExtResource("6_jtsdg")

[node name="HasNegativePosition" parent="ColorChangingSprite/BeehaveTree/SelectorComposite/SequenceComposite2" instance=ExtResource("8_dorjh")]
[node name="HasNegativePosition" parent="ColorChangingSprite/BeehaveTree/SelectorComposite/SequenceComposite2" instance=ExtResource("9_x8ohm")]

[node name="SetModulateColor" parent="ColorChangingSprite/BeehaveTree/SelectorComposite/SequenceComposite2" instance=ExtResource("7_b4xjm")]
[node name="SetModulateColor" parent="ColorChangingSprite/BeehaveTree/SelectorComposite/SequenceComposite2" instance=ExtResource("8_0v48s")]
modulate_color = Color(0, 0, 1, 1)

[node name="SetModulateColor" parent="ColorChangingSprite/BeehaveTree/SelectorComposite" instance=ExtResource("7_b4xjm")]
[node name="SetModulateColor" parent="ColorChangingSprite/BeehaveTree/SelectorComposite" instance=ExtResource("8_0v48s")]

[node name="Camera2D" type="Camera2D" parent="ColorChangingSprite"]

[node name="Blackboard" type="Node" parent="."]
script = ExtResource("9_k8008")
script = ExtResource("10_y2p03")

[node name="AnotherSprite" type="Sprite2D" parent="."]
position = Vector2(182, 180)
scale = Vector2(0.1, 0.1)
texture = SubResource("CompressedTexture2D_atdvc")

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

[node name="SelectorComposite" type="Node" parent="AnotherSprite/AnotherTree"]
script = ExtResource("5_3qraw")
script = ExtResource("5_l2xef")

[node name="SequenceComposite" type="Node" parent="AnotherSprite/AnotherTree/SelectorComposite"]
script = ExtResource("5_ifctq")
script = ExtResource("6_jtsdg")

[node name="HasPositivePosition" parent="AnotherSprite/AnotherTree/SelectorComposite/SequenceComposite" instance=ExtResource("6_7qx5k")]
[node name="HasPositivePosition" parent="AnotherSprite/AnotherTree/SelectorComposite/SequenceComposite" instance=ExtResource("7_iqb0a")]

[node name="SetModulateColor" parent="AnotherSprite/AnotherTree/SelectorComposite/SequenceComposite" instance=ExtResource("7_b4xjm")]
[node name="SetModulateColor" parent="AnotherSprite/AnotherTree/SelectorComposite/SequenceComposite" instance=ExtResource("8_0v48s")]
modulate_color = Color(1, 0, 0, 1)

[node name="SequenceComposite2" type="Node" parent="AnotherSprite/AnotherTree/SelectorComposite"]
script = ExtResource("5_ifctq")
script = ExtResource("6_jtsdg")

[node name="HasNegativePosition" parent="AnotherSprite/AnotherTree/SelectorComposite/SequenceComposite2" instance=ExtResource("8_dorjh")]
[node name="HasNegativePosition" parent="AnotherSprite/AnotherTree/SelectorComposite/SequenceComposite2" instance=ExtResource("9_x8ohm")]

[node name="SetModulateColor" parent="AnotherSprite/AnotherTree/SelectorComposite/SequenceComposite2" instance=ExtResource("7_b4xjm")]
[node name="SetModulateColor" parent="AnotherSprite/AnotherTree/SelectorComposite/SequenceComposite2" instance=ExtResource("8_0v48s")]
modulate_color = Color(0, 0, 1, 1)

[node name="SequenceComposite2" type="Node" parent="AnotherSprite/AnotherTree/SelectorComposite/SequenceComposite2"]
script = ExtResource("5_ifctq")
script = ExtResource("6_jtsdg")

[node name="HasNegativePosition" parent="AnotherSprite/AnotherTree/SelectorComposite/SequenceComposite2/SequenceComposite2" instance=ExtResource("8_dorjh")]
[node name="HasNegativePosition" parent="AnotherSprite/AnotherTree/SelectorComposite/SequenceComposite2/SequenceComposite2" instance=ExtResource("9_x8ohm")]

[node name="SetModulateColor" parent="AnotherSprite/AnotherTree/SelectorComposite/SequenceComposite2/SequenceComposite2" instance=ExtResource("7_b4xjm")]
[node name="SetModulateColor" parent="AnotherSprite/AnotherTree/SelectorComposite/SequenceComposite2/SequenceComposite2" instance=ExtResource("8_0v48s")]
modulate_color = Color(0, 0, 1, 1)

[node name="SetModulateColor" parent="AnotherSprite/AnotherTree/SelectorComposite" instance=ExtResource("7_b4xjm")]
[node name="SetModulateColor" parent="AnotherSprite/AnotherTree/SelectorComposite" instance=ExtResource("8_0v48s")]

[node name="Camera2D" type="Camera2D" parent="AnotherSprite"]

Expand Down
2 changes: 1 addition & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ config_version=5
[application]

config/name="Beehave"
run/main_scene="res://examples/BeehaveTestScene.tscn"
run/main_scene="res://examples/beehave_test_scene.tscn"
config/features=PackedStringArray("4.2")
boot_splash/image="res://splash.png"
boot_splash/fullsize=false
Expand Down
18 changes: 16 additions & 2 deletions test/beehave_tree_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ extends GdUnitTestSuite
@warning_ignore("return_value_discarded")

# TestSuite generated from
const __source = "res://addons/beehave/beehave_tree.gd"
const __source = "res://addons/beehave/nodes/beehave_tree.gd"

func create_scene() -> Node2D:
return auto_free(load("res://test/UnitTestScene.tscn").instantiate())
return auto_free(load("res://test/unit_test_scene.tscn").instantiate())


func create_tree() -> BeehaveTree:
return auto_free(load(__source).new())


func test_normal_tick() -> void:
Expand Down Expand Up @@ -63,3 +67,13 @@ func test_interrupt_running_action() -> void:
scene.beehave_tree.interrupt()
assert_that(scene.beehave_tree.blackboard.get_value("custom_value")).is_equal(0)
assert_that(scene.count_up_action.status).is_equal(BeehaveNode.FAILURE)


func test_blackboard_not_initialized() -> void:
var tree = create_tree()
tree.actor = auto_free(Node2D.new())
var always_succeed = auto_free(AlwaysSucceedDecorator.new()) as AlwaysSucceedDecorator
always_succeed.add_child(auto_free(ActionLeaf.new()))
tree.add_child(always_succeed)
var result = tree.tick()
assert_that(result).is_equal(BeehaveNode.SUCCESS)
2 changes: 1 addition & 1 deletion test/blackboard_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func test_get_default() -> void:
assert_that(blackboard.get_value("my-key2", 234)).is_equal(234)

func test_blackboard_shared_between_trees() -> void:
var scene = auto_free(load("res://test/UnitTestScene.tscn").instantiate())
var scene = auto_free(load("res://test/unit_test_scene.tscn").instantiate())
var runner = scene_runner(scene)

await runner.simulate_frames(100)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extends GdUnitTestSuite
@warning_ignore("return_value_discarded")

# TestSuite generated from
const __source = "res://examples/BeehaveTestScene.tscn"
const __source = "res://examples/beehave_test_scene.tscn"

func create_scene() -> Node2D:
return auto_free(load(__source).instantiate())
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion test/UnitTestScene.tscn → test/unit_test_scene.tscn
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[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"]
[ext_resource type="Script" path="res://test/unit_test_scene.gd" id="1_embiv"]
[ext_resource type="Script" path="res://addons/beehave/nodes/beehave_tree.gd" id="2_phgmn"]
[ext_resource type="Script" path="res://addons/beehave/nodes/composites/sequence.gd" id="4_px6t4"]
[ext_resource type="Script" path="res://test/conditions/value_reached_condition.gd" id="5_rc0ra"]
Expand Down

0 comments on commit d607a20

Please sign in to comment.