Skip to content

Commit 0bc9f89

Browse files
authored
Merge pull request #33 from Shargon-Pendragon/clean
Cleaned up scripts and fixed the "Resumed function '_tick()' after yield, but script is gone." error for bt_wait.gd
2 parents b6081ae + 0429298 commit 0bc9f89

21 files changed

+287
-138
lines changed

.DS_Store

6 KB
Binary file not shown.

addons/.DS_Store

6 KB
Binary file not shown.

addons/behavior_tree/.DS_Store

6 KB
Binary file not shown.
Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
class_name BehaviorTree, "res://addons/behavior_tree/icons/bt.svg"
1+
class_name BehaviorTree, "res://addons/behavior_tree/icons/bt.svg"
22
extends Node
33

44
# This is your main node. Put one of these at the root of the scene and start adding BTNodes.
5-
# A Behavior Tree only accepts ONE entry point (so one child), for example a BTSequence or a BTSelector.
5+
# A Behavior Tree only accepts ONE entry point (so one child).
66

77
export(bool) var is_active: bool = false
88
export(NodePath) var _blackboard
@@ -16,51 +16,46 @@ onready var agent = get_node(_agent) as Node
1616
onready var blackboard = get_node(_blackboard) as Blackboard
1717
onready var bt_root = get_child(0) as BTNode
1818

19-
20-
2119
func _ready() -> void:
2220
assert(get_child_count() == 1, "A Behavior Tree can only have one entry point.")
2321
bt_root.propagate_call("connect", ["abort_tree", self, "abort"])
2422
start()
2523

26-
27-
func _process(delta: float) -> void:
24+
func _process(_delta: float) -> void:
2825
if not is_active:
2926
set_process(false)
3027
return
31-
28+
3229
if debug:
3330
print()
34-
31+
3532
tick_result = bt_root.tick(agent, blackboard)
36-
33+
3734
if tick_result is GDScriptFunctionState:
3835
set_process(false)
3936
yield(tick_result, "completed")
4037
set_process(true)
41-
42-
4338

44-
func _physics_process(delta: float) -> void:
39+
func _physics_process(_delta: float) -> void:
4540
if not is_active:
4641
set_physics_process(false)
4742
return
48-
43+
4944
if debug:
5045
print()
51-
46+
5247
tick_result = bt_root.tick(agent, blackboard)
53-
48+
5449
if tick_result is GDScriptFunctionState:
5550
set_physics_process(false)
5651
yield(tick_result, "completed")
5752
set_physics_process(true)
5853

59-
54+
# Internal: Set up if we are using process or physics_process for the behavior tree
6055
func start() -> void:
6156
if not is_active:
6257
return
63-
58+
6459
match sync_mode:
6560
0:
6661
set_physics_process(false)
@@ -69,6 +64,6 @@ func start() -> void:
6964
set_process(false)
7065
set_physics_process(true)
7166

72-
67+
# Public: Set the tree to inactive when a abort_tree signal is sent from bt_root
7368
func abort() -> void:
7469
is_active = false
Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
1-
class_name Blackboard, "res://addons/behavior_tree/icons/blackboard.svg"
1+
class_name Blackboard, "res://addons/behavior_tree/icons/blackboard.svg"
22
extends Node
33

4-
# This is the database where all your variables go. Here you keep track of
4+
# This is the database where all your variables go. Here you keep track of
55
# whether the player is nearby, your health is low, there is a cover available,
66
# or whatever.
77
#
8-
# You just store the data here and use it for condition checks in BTCondition scripts,
8+
# You just store the data here and use it for condition checks in BTCondition scripts,
99
# or as arguments for your function calls in BTAction.
1010
#
11-
# This is a good way to separate data from behavior, which is essential
11+
# This is a good way to separate data from behavior, which is essential
1212
# to avoid nasty bugs.
1313

1414
export(Dictionary) var data: Dictionary
1515

16-
17-
1816
func _enter_tree() -> void:
1917
data = data.duplicate()
2018

21-
2219
func _ready() -> void :
2320
for key in data.keys():
2421
assert(key is String, "Blackboard keys must be stored as strings.")
2522

26-
23+
# Public: Updates or adds the blackboard with the provided key and value
24+
#
25+
# key = Contains the name the data will be stored upder in the blackboard
26+
# value = Contains the data that will be associated with the key. (Variant)
27+
# Example
28+
# set_data("health", 15)
2729
func set_data(key: String, value) -> void:
2830
data[key] = value
2931

30-
32+
# Public: Returns the blackboard data for the provided key
33+
#
34+
# key = Contains the name the data will be stored upder in the blackboard
35+
#
36+
# Example
37+
# get("health")
38+
# => 15
39+
#
40+
# Returns Variant
3141
func get_data(key: String):
3242
if data.has(key):
3343
var value = data[key]
@@ -42,6 +52,12 @@ func get_data(key: String):
4252
else:
4353
return null
4454

45-
55+
# Public: Returns true if the key is present in the blackboard
56+
#
57+
# key = Contains the name the data will be stored upder in the blackboard
58+
#
59+
# Example
60+
# had_data("health")
61+
# => true
4662
func has_data(key: String) -> bool:
4763
return data.has(key)

addons/behavior_tree/src/bt_node.gd

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
1-
class_name BTNode, "res://addons/behavior_tree/icons/btnode.svg"
1+
class_name BTNode, "res://addons/behavior_tree/icons/btnode.svg"
22
extends Node
33

4-
# Base class from which every node in the behavior tree inherits.
4+
# Base class from which every node in the behavior tree inherits.
55
# You don't usually need to instance this node directly.
66
# To define your behaviors, use and extend BTLeaf instead.
77

8+
# (Optional) Emitted after a tick() call. True is success, false is failure.
9+
signal tick(result)
10+
11+
# Emitted if abort_tree is set to true
12+
signal abort_tree()
13+
14+
815
enum BTNodeState {
916
FAILURE,
1017
SUCCESS,
1118
RUNNING,
1219
}
1320

14-
# (Optional) Emitted after a tick() call. True is success, false is failure.
15-
signal tick(result)
16-
17-
# Emitted if abort_tree is set to true
18-
signal abort_tree()
19-
2021
# Turn this off to make the node fail at each tick.
21-
export(bool) var is_active: bool = true
22+
export(bool) var is_active: bool = true
2223

2324
# Turn this on to print the name of the node at each tick.
24-
export(bool) var debug: bool = false
25+
export(bool) var debug: bool = false
2526

2627
# Turn this on to abort the tree after completion.
2728
export(bool) var abort_tree: bool
2829

2930
var state: int setget set_state
3031

31-
32-
3332
func _ready():
3433
if is_active:
3534
succeed()
@@ -41,17 +40,18 @@ func _ready():
4140
### OVERRIDE THE FOLLOWING FUNCTIONS ###
4241
# You just need to implement them. DON'T CALL THEM MANUALLY.
4342

44-
func _pre_tick(agent: Node, blackboard: Blackboard) -> void:
43+
# Public: Called before the tick happens used for preperation for action or conditional
44+
func _pre_tick(_agent: Node, _blackboard: Blackboard) -> void:
4545
pass
4646

4747

4848
# This is where the core behavior goes and where the node state is changed.
4949
# You must return either succeed() or fail() (check below), not just set the state.
50-
func _tick(agent: Node, blackboard: Blackboard) -> bool:
50+
func _tick(_agent: Node, _blackboard: Blackboard) -> bool:
5151
return succeed()
5252

53-
54-
func _post_tick(agent: Node, blackboard: Blackboard, result: bool) -> void:
53+
# Public: Called after the tick for anything that needs to happen after the action
54+
func _post_tick(_agent: Node, _blackboard: Blackboard, _result: bool) -> void:
5555
pass
5656

5757
### DO NOT OVERRIDE ANYTHING FROM HERE ON ###
@@ -81,7 +81,7 @@ func set_state(rhs: int) -> bool:
8181
return succeed()
8282
BTNodeState.FAILURE:
8383
return fail()
84-
84+
8585
assert(false, "Invalid BTNodeState assignment. Can only set to success or failure.")
8686
return false
8787

@@ -117,38 +117,45 @@ func get_state() -> String:
117117
return "running"
118118

119119

120-
# Again, DO NOT override this.
120+
# Again, DO NOT override this.
121121
func tick(agent: Node, blackboard: Blackboard) -> bool:
122122
if not is_active:
123123
return fail()
124-
124+
125125
if running():
126126
return false
127-
127+
128128
if debug:
129129
print(name)
130-
130+
131131
# Do stuff before core behavior
132132
_pre_tick(agent, blackboard)
133-
134-
run()
135-
133+
134+
run()
135+
136136
var result = _tick(agent, blackboard)
137-
137+
138138
if result is GDScriptFunctionState:
139-
assert(running(), "BTNode execution was suspended but it's not running. Did you succeed() or fail() before yield?")
139+
assert(
140+
running(),
141+
"BTNode execution was suspended. Did you succeed() or fail() before yield?"
142+
)
143+
140144
result = yield(result, "completed")
141-
142-
assert(not running(), "BTNode execution was completed but it's still running. Did you forget to return succeed() or fail()?")
143-
145+
146+
assert(
147+
not running(),
148+
"BTNode executed but it's still running. Did you forget to return succeed() or fail()?"
149+
)
150+
144151
# Do stuff after core behavior depending on the result
145152
_post_tick(agent, blackboard, result)
146-
153+
147154
# Notify completion and new state (i.e. the result of the execution)
148155
emit_signal("tick", result)
149-
156+
150157
# Queue tree abortion at the end of current tick
151158
if abort_tree:
152159
emit_signal("abort_tree")
153-
160+
154161
return result

addons/behavior_tree/src/btnodes/bt_composite.gd

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,29 @@ extends BTNode
88
# to you, but you may have some specific flow in your game. In that case,
99
# you can extend this script and define it yourself.
1010

11-
onready var children: Array = get_children() as Array
12-
1311
var bt_child: BTNode # Used to iterate over children
1412

15-
13+
onready var children: Array = get_children() as Array
1614

1715
func _ready():
1816
assert(get_child_count() > 1, "A BTComposite must have more than one child.")
1917

20-
21-
18+
# Public: Returns true once all the childrens _tick functions are completed
19+
#
20+
# agent = Is the agent instance
21+
# blackboard = Contains data that the agent has stored
22+
#
23+
# Example
24+
# _tick(Node.new(), Blackboard.new())
25+
# => true
2226
func _tick(agent: Node, blackboard: Blackboard) -> bool:
2327
var result
24-
28+
2529
for c in children:
2630
bt_child = c
2731
result = bt_child.tick(agent, blackboard)
28-
32+
2933
if result is GDScriptFunctionState:
3034
result = yield(result, "completed")
31-
35+
3236
return succeed()

addons/behavior_tree/src/btnodes/bt_decorator.gd

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@ extends BTNode
66

77
onready var bt_child: BTNode = get_child(0) as BTNode
88

9-
10-
119
func _ready():
1210
assert(get_child_count() == 1, "A BTDecorator can only have one child.")
1311

14-
12+
# Public: Returns true once it's only child's _tick function has completed
13+
#
14+
# agent = Is the agent instance
15+
# blackboard = Contains data that the agent has stored
16+
#
17+
# Example
18+
# _tick(Node.new(), Blackboard.new())
19+
# => true
1520
func _tick(agent: Node, blackboard: Blackboard) -> bool:
1621
var result = bt_child.tick(agent, blackboard)
17-
22+
1823
if result is GDScriptFunctionState:
1924
result = yield(result, "completed")
20-
21-
return set_state(bt_child.state)
2225

26+
return set_state(bt_child.state)

addons/behavior_tree/src/btnodes/bt_leaf.gd

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
class_name BTLeaf, "res://addons/behavior_tree/icons/btleaf.svg"
1+
class_name BTLeaf, "res://addons/behavior_tree/icons/btleaf.svg"
22
extends BTNode
33

44
# Leaf nodes are used to implement your own behavior logic.
5-
# That can be for example calling functions on the agent, or checking conditions in the blackboard.
5+
# That can be for example calling functions on the agent, or checking conditions in the blackboard.
66
# Good practice is to not make leaf nodes do too much stuff, and to not have flow logic in them.
77
# Instead, just use them to do a single action or condition check, and use a composite node
88
# (BTSequence, BTSelector or BTParallel) to define the flow between multiple leaf nodes.

addons/behavior_tree/src/btnodes/composites/bt_parallel.gd

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@ extends BTComposite
33

44
# Executes each child. doesn't wait for completion, always succeeds.
55

6+
# Public: Returns true once all the childrens _tick functions are ran doesn't check for completion
7+
#
8+
# agent = Is the agent instance
9+
# blackboard = Contains data that the agent has stored
10+
#
11+
# Example
12+
# _tick(Node.new(), Blackboard.new())
13+
# => true
614
func _tick(agent: Node, blackboard: Blackboard) -> bool:
715
for c in children:
816
bt_child = c
917
bt_child.tick(agent, blackboard)
10-
18+
1119
return succeed()
1220

0 commit comments

Comments
 (0)