Skip to content

Commit 323a087

Browse files
committed
Fix #35 : behavior trees and blackboard can be reset
1 parent d8dc136 commit 323a087

File tree

10 files changed

+55
-2
lines changed

10 files changed

+55
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Data in blackboard can be isolated in so-called *namespaces*. A data key can exi
4545
Some well-known properties are already fed by the root tree during execution :
4646
- `delta` : the float value of delta from *process* or *physics process* (depending on root tree process mode).
4747

48+
A blackboard can also be reset using `reset` method. This method will erase all blackboard content, except well-known properties. The initial data is also restored after resetting the blackboard.
49+
4850
![image](documentation/assets/nodes/btblackboard.png)
4951

5052
🔑 Properties list:
@@ -63,6 +65,8 @@ Its the entry point of your behavior tree. It can only have a unique child of ty
6365
- `blackboard` : path to the blackboard node. This allows to share a same blackboard between several trees, for example to code a group of enemies acting together, or to specify some default entries using the editor. If empty, a default empty blackboard will be used during tree execution. Default is *empty*,
6466
- `enable_monitor` (*debug option*) : indicates if, in debug mode, a custom monitor should be created for this tree. Custom monitor allows to track performances in Debugger Monitors view.
6567

68+
`BTRoot` can be reset, allowinf to resuse the node for another usage. Resetting the tree ensures that all its nodes will be reset too (recursively). Resetting the tree does *not* reset the associated blackboard, since blackboards can be shared among multiple trees. To reset the blackboard, use its reset function.
69+
6670
### ![icon](addons/yet_another_behavior_tree/src/Assets/Icons/btselector.png) BTSelector
6771

6872
The selector node is a *composite node* that executes its children from the first one to the last one, in order, until one of them returns *SUCCESS*. If a selector child succeeds, the selector succeed too. If all selector children failed, the selector fails too.

addons/yet_another_behavior_tree/src/Blackboard/BTBlackboard.gd

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ var _default_namespace_data:Dictionary
5050

5151
func _ready() -> void:
5252
# On copie le dico défini par l'utilisateur dans le dico privé
53-
_default_namespace_data = _get_namespace_board(DEFAULT_NAMESPACE)
54-
_default_namespace_data.merge(data)
53+
_init_default_namespace()
5554

5655
#------------------------------------------
5756
# Fonctions publiques
@@ -81,10 +80,24 @@ func delete_data(key:Variant, board_namespace:String = DEFAULT_NAMESPACE) -> Var
8180
namespace_dico.erase(key)
8281
return old_value.get_ref() if old_value is WeakRef else old_value
8382

83+
## Reset the whole blackboard, erasing all data from it.
84+
## Data set in node configuration through [data] field is restored
85+
func reset() -> void:
86+
# To prevent tree from just stop working, we preserve delta value into the blackboard
87+
# It's an internal state, shoumd never be erased by user
88+
var delta:float = get_delta()
89+
_execution_data.clear()
90+
_init_default_namespace()
91+
_unsafe_set_delta(delta)
92+
8493
#------------------------------------------
8594
# Fonctions privées
8695
#------------------------------------------
8796

97+
func _init_default_namespace() -> void:
98+
_default_namespace_data = _get_namespace_board(DEFAULT_NAMESPACE)
99+
_default_namespace_data.merge(data)
100+
88101
func _get_namespace_board(board_namespace:String) -> Dictionary:
89102
if not _execution_data.has(board_namespace):
90103
_execution_data[board_namespace] = {}

addons/yet_another_behavior_tree/src/Nodes/BTNode.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ func stop(blackboard:BTBlackboard) -> void:
6969
func exit(blackboard:BTBlackboard) -> void:
7070
pass
7171

72+
## Reset node state, as it is was just instantiated
73+
func reset() -> void:
74+
pass
75+
7276
#------------------------------------------
7377
# Fonctions privées
7478
#------------------------------------------

addons/yet_another_behavior_tree/src/Nodes/BTRoot.gd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@ func _get_configuration_warnings() -> PackedStringArray:
127127
# Fonctions publiques
128128
#------------------------------------------
129129

130+
## Reset behavior tree state, and children states too
131+
## This method should never be called when tree is used. It is intended to recycle the
132+
## tree for other usage, so it should be called outside tree execution
133+
## Tree associated blackboard is NOT reset. To do it, call reset function on blackboard itself.
134+
func reset() -> void:
135+
_internal_state.clear()
136+
_children[0].reset()
137+
130138
#------------------------------------------
131139
# Fonctions privées
132140
#------------------------------------------

addons/yet_another_behavior_tree/src/Nodes/Composite/BTComposite.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ func _get_configuration_warnings() -> PackedStringArray:
4242
# Fonctions publiques
4343
#------------------------------------------
4444

45+
func reset() -> void:
46+
for child in _children:
47+
child.reset()
48+
4549
#------------------------------------------
4650
# Fonctions privées
4751
#------------------------------------------

addons/yet_another_behavior_tree/src/Nodes/Composite/BTSelector.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ func tick(actor:Node, blackboard:BTBlackboard) -> int:
5353
# Fonctions publiques
5454
#------------------------------------------
5555

56+
func reset() -> void:
57+
_running_child_index = -1
58+
super.reset()
59+
5660
#------------------------------------------
5761
# Fonctions privées
5862
#------------------------------------------

addons/yet_another_behavior_tree/src/Nodes/Composite/BTSequence.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ func tick(actor:Node, blackboard:BTBlackboard) -> int:
5353
# Fonctions publiques
5454
#------------------------------------------
5555

56+
func reset() -> void:
57+
_running_child_index = -1
58+
super.reset()
59+
5660
#------------------------------------------
5761
# Fonctions privées
5862
#------------------------------------------

addons/yet_another_behavior_tree/src/Nodes/Decorators/BTDecorator.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ func _get_configuration_warnings() -> PackedStringArray:
4141
# Fonctions publiques
4242
#------------------------------------------
4343

44+
func reset() -> void:
45+
for child in _children:
46+
child.reset()
47+
4448
#------------------------------------------
4549
# Fonctions privées
4650
#------------------------------------------

addons/yet_another_behavior_tree/src/Nodes/Decorators/BTLimiter.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ func tick(actor:Node, blackboard:BTBlackboard) -> int:
5555
_invocation_count += 1
5656
return result
5757

58+
func reset() -> void:
59+
_invocation_count = 0
60+
super.reset()
61+
5862
#------------------------------------------
5963
# Fonctions privées
6064
#------------------------------------------

addons/yet_another_behavior_tree/src/Nodes/Leaves/BTActionWait.gd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ func start(blackboard:BTBlackboard) -> void:
5555
if random_deviation_ms != 0:
5656
_time_to_reach_ms += randi_range(0, random_deviation_ms)
5757

58+
func reset() -> void:
59+
_current_time_ms = 0
60+
_time_to_reach_ms = 0
61+
5862
#------------------------------------------
5963
# Fonctions privées
6064
#------------------------------------------

0 commit comments

Comments
 (0)