From 32fa022e95c89ecd117db210d4c18e4152460892 Mon Sep 17 00:00:00 2001 From: Adrien Quillet Date: Sat, 31 Dec 2022 18:29:06 +0100 Subject: [PATCH] Fix #3 : Custom performance monitor in BTRoot produces erros when node is removed from tree --- .../yet_another_behavior_tree/src/Nodes/BTRoot.gd | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/addons/yet_another_behavior_tree/src/Nodes/BTRoot.gd b/addons/yet_another_behavior_tree/src/Nodes/BTRoot.gd index 4f0ecf3..83a2197 100644 --- a/addons/yet_another_behavior_tree/src/Nodes/BTRoot.gd +++ b/addons/yet_another_behavior_tree/src/Nodes/BTRoot.gd @@ -53,6 +53,8 @@ var _execution_blackboard:BTBlackboard var _execution_start_time_ms:float var _execution_stop_time_ms:float +@onready var _performance_monitor_identifier:String = "BTRoot/%s-%s" % [get_name(), get_instance_id()] + #------------------------------------------ # Fonctions Godot redéfinies #------------------------------------------ @@ -69,7 +71,9 @@ func _ready() -> void: _blackboard = BTBlackboard.new() if not Engine.is_editor_hint(): - Performance.add_custom_monitor("BTRoot/%s-%s" % [get_name(), get_instance_id()], _compute_last_exec_time) + _add_custom_performance_monitor() + tree_entered.connect(_add_custom_performance_monitor) + tree_exited.connect(_remove_custom_performance_monitor) func _process(delta:float) -> void: if not Engine.is_editor_hint() and enabled and root_process_mode == BTRootProcessMode.PROCESS: @@ -133,6 +137,13 @@ func _do_execute(delta:float): _previous_running_nodes = running_nodes _register_execution_stop() +func _add_custom_performance_monitor() -> void: + if not Performance.has_custom_monitor(_performance_monitor_identifier): + Performance.add_custom_monitor(_performance_monitor_identifier, _compute_last_exec_time) + +func _remove_custom_performance_monitor() -> void: + if Performance.has_custom_monitor(_performance_monitor_identifier): + Performance.remove_custom_monitor(_performance_monitor_identifier) func _register_execution_start() -> void: _execution_start_time_ms = Time.get_ticks_msec()