@@ -5,6 +5,7 @@ class_name LimiterDecorator extends Decorator
55## The limiter will execute its `RUNNING` child `x` amount of times. When the number of
66## maximum ticks is reached, it will return a `FAILURE` status code.
77## The count resets the next time that a child is not `RUNNING`
8+ ## or when the node is interrupted (such as when the behavior tree changes branches).
89
910@onready var cache_key = "limiter_%s " % self .get_instance_id ()
1011
@@ -28,11 +29,13 @@ func tick(actor: Node, blackboard: Blackboard) -> int:
2829 blackboard .set_value ("last_condition" , child , str (actor .get_instance_id ()))
2930 blackboard .set_value ("last_condition_status" , response , str (actor .get_instance_id ()))
3031
31- if child is ActionLeaf and response == RUNNING :
32+ if response == RUNNING :
3233 running_child = child
33- blackboard .set_value ("running_action" , child , str (actor .get_instance_id ()))
34-
35- if response != RUNNING :
34+ if child is ActionLeaf :
35+ blackboard .set_value ("running_action" , child , str (actor .get_instance_id ()))
36+ else :
37+ # If the child is no longer running, reset the counter for next time
38+ _reset_counter (actor , blackboard )
3639 child .after_run (actor , blackboard )
3740
3841 return response
@@ -43,11 +46,25 @@ func tick(actor: Node, blackboard: Blackboard) -> int:
4346
4447
4548func before_run (actor : Node , blackboard : Blackboard ) -> void :
46- blackboard .set_value (cache_key , 0 , str (actor .get_instance_id ()))
49+ # Initialize the counter to 0 when we first start running
50+ _reset_counter (actor , blackboard )
4751 if get_child_count () > 0 :
4852 get_child (0 ).before_run (actor , blackboard )
4953
5054
55+ func interrupt (actor : Node , blackboard : Blackboard ) -> void :
56+ # The tree is changing branches, so the count should reset
57+ _reset_counter (actor , blackboard )
58+
59+ # Call super, which may affect our blackboard values
60+ super (actor , blackboard )
61+
62+
63+ # Resets the counter in the blackboard
64+ func _reset_counter (actor : Node , blackboard : Blackboard ) -> void :
65+ blackboard .set_value (cache_key , 0 , str (actor .get_instance_id ()))
66+
67+
5168func get_class_name () -> Array [StringName ]:
5269 var classes := super ()
5370 classes .push_back (& "LimiterDecorator" )
0 commit comments