Skip to content

Commit

Permalink
Register trees in debugger tab again when added back to scene (#344)
Browse files Browse the repository at this point in the history
* request ready when unregistering tree

If the tree is added back to the scene, the _ready
function will be called which will register the tree
again.

* verify debug tree registration on scene changes
  • Loading branch information
Snaiel authored Jun 26, 2024
1 parent f80784d commit cf04ed3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
11 changes: 8 additions & 3 deletions addons/beehave/nodes/beehave_tree.gd
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ var _can_send_message: bool = false


func _ready() -> void:
get_tree().node_added.connect(_on_scene_tree_node_added_removed.bind(true))
get_tree().node_removed.connect(_on_scene_tree_node_added_removed.bind(false))
var connect_scene_tree_signal = func(signal_name: String, is_added: bool):
if not get_tree().is_connected(signal_name, _on_scene_tree_node_added_removed.bind(is_added)):
get_tree().connect(signal_name, _on_scene_tree_node_added_removed.bind(is_added))
connect_scene_tree_signal.call("node_added", true)
connect_scene_tree_signal.call("node_removed", false)

if not process_thread:
process_thread = ProcessThread.PHYSICS
Expand Down Expand Up @@ -156,7 +159,9 @@ func _on_scene_tree_node_added_removed(node: Node, is_added: bool) -> void:
)
else:
sgnal.connect(
func() -> void: BeehaveDebuggerMessages.unregister_tree(get_instance_id())
func() -> void:
BeehaveDebuggerMessages.unregister_tree(get_instance_id())
request_ready()
)


Expand Down
16 changes: 15 additions & 1 deletion examples/beehave_test_scene.gd
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
extends Node2D

@onready var sprite := $ColorChangingSprite
@onready var another_sprite := $AnotherSprite
@onready var tree := %BeehaveTree
@onready var condition_label := %ConditionLabel
@onready var action_label := %ActionLabel


func _process(delta:float) -> void:
if Input.is_action_pressed("ui_left"):
sprite.position.x -= 200 * delta
Expand All @@ -28,3 +28,17 @@ func _process(delta:float) -> void:
action_label.text = str(tree.get_running_action(), " -> RUNNING")
else:
action_label.text = "no running action"

# The following are used to verify if the beehave trees show up or get
# removed from the debug panel when various scene operations occur.
if Input.is_action_just_pressed("ui_text_delete") and another_sprite.is_inside_tree():
another_sprite.get_parent().remove_child(another_sprite)

if Input.is_action_just_pressed("ui_accept") and not another_sprite.is_inside_tree():
add_child(another_sprite)

if Input.is_action_just_pressed("ui_undo"):
get_tree().reload_current_scene()

if Input.is_action_just_pressed("ui_cut"):
another_sprite.reparent(get_node("Background"))

0 comments on commit cf04ed3

Please sign in to comment.