From db933cd62003a41e73399b50c0d8b1bbb7a947f0 Mon Sep 17 00:00:00 2001 From: Yan Wittmann Date: Sat, 4 Jan 2025 16:42:32 +0100 Subject: [PATCH] Started to introduce RUNNING concept --- project/scripts/player/tree/BehaviorTree.gd | 2 +- project/scripts/player/tree/Task.gd | 15 +++++++++++++++ .../scripts/player/tree/impl/base/TaskSelector.gd | 4 +--- .../scripts/player/tree/impl/base/TaskSequence.gd | 4 +--- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/project/scripts/player/tree/BehaviorTree.gd b/project/scripts/player/tree/BehaviorTree.gd index 8164a84..b565371 100644 --- a/project/scripts/player/tree/BehaviorTree.gd +++ b/project/scripts/player/tree/BehaviorTree.gd @@ -26,4 +26,4 @@ func populate_blackboard(): func game_tick() -> void: populate_blackboard() - behavior_tree.run(blackboard) + behavior_tree.internal_run(blackboard) diff --git a/project/scripts/player/tree/Task.gd b/project/scripts/player/tree/Task.gd index 4603ea9..c45079b 100644 --- a/project/scripts/player/tree/Task.gd +++ b/project/scripts/player/tree/Task.gd @@ -5,6 +5,21 @@ enum {FAILURE = -1, SUCCESS = 1, RUNNING = 0} var status: int = FAILURE +func internal_run(p_blackboard: Dictionary) -> void: + p_blackboard["current_task"] = self + + if status == RUNNING: + for c in self.get_children(): + if not c.status == RUNNING: + continue + c.internal_run(p_blackboard) + if c.status != SUCCESS: + status = c.status + return + + run(p_blackboard) + + func run(p_blackboard: Dictionary) -> void: pass diff --git a/project/scripts/player/tree/impl/base/TaskSelector.gd b/project/scripts/player/tree/impl/base/TaskSelector.gd index 0619e00..a71f8a8 100644 --- a/project/scripts/player/tree/impl/base/TaskSelector.gd +++ b/project/scripts/player/tree/impl/base/TaskSelector.gd @@ -3,9 +3,7 @@ extends Task func run(blackboard: Dictionary) -> void: for c in self.get_children(): - if status == RUNNING and not c.status == RUNNING: - continue - c.run(blackboard) + c.internal_run(blackboard) if c.status != FAILURE: status = c.status return diff --git a/project/scripts/player/tree/impl/base/TaskSequence.gd b/project/scripts/player/tree/impl/base/TaskSequence.gd index ea3e1f9..39f847b 100644 --- a/project/scripts/player/tree/impl/base/TaskSequence.gd +++ b/project/scripts/player/tree/impl/base/TaskSequence.gd @@ -3,9 +3,7 @@ extends Task func run(blackboard: Dictionary) -> void: for c in self.get_children(): - if status == RUNNING and not c.status == RUNNING: - continue - c.run(blackboard) + c.internal_run(blackboard) if c.status != SUCCESS: status = c.status return