TinyAdventure/ui/debug/behaviour.gd

93 lines
2.9 KiB
GDScript
Raw Normal View History

extends MarginContainer
@onready var behavior_debug_view: BehaviorTreeView = %BehaviorDebugView
2024-12-28 22:10:15 +01:00
@onready var mark_npc_for_debug: CheckBox = %MarkNPCForDebug
@onready var npc_option_button: OptionButton = %NPCOptionButton
2024-12-28 22:10:15 +01:00
var _debug_agent:NonPlayerCharacter = null
var _debug_bt_player:BTPlayer = null
func _process(_delta: float) -> void:
2024-12-28 22:10:15 +01:00
if npc_option_button.item_count == 0:
update_npc_option_button()
if _debug_bt_player == null:
return
var instance: BTInstance = _debug_bt_player.get_bt_instance()
var data: BehaviorTreeData = BehaviorTreeData.create_from_bt_instance(instance)
behavior_debug_view.update_tree(data)
2024-12-28 22:10:15 +01:00
func update_npc_option_button() -> void:
var npc_nodes:Array = get_tree().get_nodes_in_group("non_player_character")
var needs_refresh = false
if npc_nodes.size() != npc_option_button.item_count:
needs_refresh = true
else:
for i in range(npc_nodes.size()):
if npc_option_button.get_item_text(i) != str(npc_nodes[i].get_path()):
needs_refresh = true
break
if needs_refresh:
npc_option_button.clear()
for node:Node in npc_nodes:
var node_name:String = node.get_path().get_name(node.get_path().get_name_count() - 1)
npc_option_button.add_item(node_name)
npc_option_button.set_item_tooltip(npc_option_button.item_count - 1, node.get_path())
func mark_agent_active_for_debug() -> void:
if _debug_bt_player == null:
return
var instance: BTInstance = _debug_bt_player.get_bt_instance()
var blackboard: Blackboard = instance.get_blackboard()
if mark_npc_for_debug.pressed:
blackboard.set_var("debug_npc_path", _debug_agent.get_path())
func unmark_agent_active_for_debug() -> void:
if _debug_agent == null:
return
var instance: BTInstance = _debug_bt_player.get_bt_instance()
var blackboard: Blackboard = instance.get_blackboard()
if mark_npc_for_debug.pressed:
blackboard.set_var("debug_npc_path", NodePath())
func _on_npc_name_edit_text_submitted(new_text: String) -> void:
2024-12-28 22:10:15 +01:00
unmark_agent_active_for_debug()
2024-12-28 22:10:15 +01:00
_debug_agent = get_tree().root.find_child(new_text, true, false) as NonPlayerCharacter
2024-12-28 22:10:15 +01:00
if not is_instance_valid(_debug_agent):
push_error("Cannot show npc behavior: could not find node with name '%s'." % new_text)
return
2024-12-28 22:10:15 +01:00
_debug_bt_player = _debug_agent.find_child("BTPlayer") as BTPlayer
if _debug_bt_player == null:
behavior_debug_view.clear()
push_error("Cannot show npc behavior: no BTPlayer found.")
2024-12-28 22:10:15 +01:00
return
if mark_npc_for_debug.pressed:
mark_agent_active_for_debug()
func _on_mark_npc_for_debug_toggled(toggled_on: bool) -> void:
if toggled_on:
mark_agent_active_for_debug()
func _on_npc_option_button_item_selected(index: int) -> void:
_debug_agent = get_tree().root.get_node(npc_option_button.get_item_tooltip(index)) as NonPlayerCharacter
if _debug_agent:
_debug_bt_player = _debug_agent.find_child("BTPlayer") as BTPlayer
if _debug_bt_player == null:
behavior_debug_view.clear()
push_error("Cannot show npc behavior: no BTPlayer found.")
return