@@ -5,40 +5,11 @@ extends Node
55# You don't usually need to instance this node directly.
66# To define your behaviors, use and extend BTLeaf instead.
77
8- class BTNodeState :
9- var success : bool = true setget set_success
10- var failure : bool = false setget set_failure
11- var running : bool = false setget set_running
12-
13-
14- func set_success (value : bool = true ):
15- if value == false :
16- print_debug ("Ignoring manual change of BTNodeState. Use setters." )
17- return
18- success = true
19- failure = false
20- running = false
21-
22-
23- func set_failure (value : bool = true ):
24- if value == false :
25- print_debug ("Ignoring manual change of BTNodeState. Use setters." )
26- return
27- success = false
28- failure = true
29- running = false
30-
31-
32- func set_running (value : bool = true ):
33- if value == false :
34- print_debug ("Ignoring manual change of BTNodeState. Use setters." )
35- return
36- success = false
37- failure = false
38- running = true
39-
40-
41-
8+ enum BTNodeState {
9+ FAILURE ,
10+ SUCCESS ,
11+ RUNNING ,
12+ }
4213
4314# (Optional) Emitted after a tick() call. True is success, false is failure.
4415signal tick (result )
@@ -55,7 +26,7 @@ export(bool) var debug: bool = false
5526# Turn this on to abort the tree after completion.
5627export (bool ) var abort_tree : bool
5728
58- var state : BTNodeState = BTNodeState . new ()
29+ var state : int setget set_state
5930
6031
6132
@@ -89,49 +60,51 @@ func _post_tick(agent: Node, blackboard: Blackboard, result: bool) -> void:
8960
9061## # BEGIN: RETURN VALUES ###
9162
92- # Your _tick() must return on of the two following funcs .
63+ # Your _tick() must return one of the following functions .
9364
9465# Return this to set the state to success.
9566func succeed () -> bool :
96- state . set_success ()
67+ state = BTNodeState . SUCCESS
9768 return true
9869
9970
10071# Return this to set the state to failure.
10172func fail () -> bool :
102- state . set_failure ()
73+ state = BTNodeState . FAILURE
10374 return false
10475
10576
10677# Return this to match the state to another state.
107- func set_state (rhs : BTNodeState ) -> bool :
108- if rhs .success :
109- return succeed ()
110- else :
111- return fail ()
78+ func set_state (rhs : int ) -> bool :
79+ match rhs :
80+ BTNodeState .SUCCESS :
81+ return succeed ()
82+ BTNodeState .FAILURE :
83+ return fail ()
84+
85+ assert (false , "Invalid BTNodeState assignment. Can only set to success or failure." )
86+ return false
11287
11388## # END: RETURN VALUES ###
11489
11590
11691
117- # You are not supposed to use this.
118- # It's called automatically.
119- # It won't do what you think.
92+ # Don't call this.
12093func run ():
121- state . set_running ()
94+ state = BTNodeState . RUNNING
12295
12396
12497# You can use the following to recover the state of the node
12598func succeeded () -> bool :
126- return state . success
99+ return state == BTNodeState . SUCCESS
127100
128101
129102func failed () -> bool :
130- return state . failure
103+ return state == BTNodeState . FAILURE
131104
132105
133106func running () -> bool :
134- return state . running
107+ return state == BTNodeState . RUNNING
135108
136109
137110# Or this, as a string.
@@ -163,21 +136,18 @@ func tick(agent: Node, blackboard: Blackboard) -> bool:
163136 var result = _tick (agent , blackboard )
164137
165138 if result is GDScriptFunctionState :
166- # If you yield, the node must be running.
167- # If you crash here it means you changed the state before yield.
168- assert (running ())
139+ assert (running (), "BTNode execution was suspended but it's not running. Did you succeed() or fail() before yield?" )
169140 result = yield (result , "completed" )
170141
171- # If you complete execution (or don't yield anymore), the node can't be running.
172- # If you crash here it means you forgot to return succeed() or fail().
173- assert (not running ())
142+ assert (not running (), "BTNode execution was completed but it's still running. Did you forget to return succeed() or fail()?" )
174143
175144 # Do stuff after core behavior depending on the result
176145 _post_tick (agent , blackboard , result )
177146
178147 # Notify completion and new state (i.e. the result of the execution)
179148 emit_signal ("tick" , result )
180149
150+ # Queue tree abortion at the end of current tick
181151 if abort_tree :
182152 emit_signal ("abort_tree" )
183153
0 commit comments