Skip to content

Commit a1290bb

Browse files
author
Gabriele Torini
committed
Optimisations
1 parent da13774 commit a1290bb

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

bt_node.gd

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,11 @@ func _ready():
7171
# You can use these as setters and getters for the BTNodeState of a BTNode
7272
func succeed() -> bool:
7373
state.set_success()
74-
emit_signal("tick", true)
75-
return true
74+
return true
7675

7776

7877
func fail() -> bool:
79-
state.set_failure()
80-
emit_signal("tick", false)
78+
state.set_failure()
8179
return false
8280

8381

@@ -106,13 +104,11 @@ func get_state() -> String:
106104
return "running"
107105

108106

109-
func set_state(rhs: BTNode):
107+
func set_state(rhs: BTNode) -> bool:
110108
if rhs.succeeded():
111109
return succeed()
112110
elif rhs.failed():
113-
return fail()
114-
else:
115-
run()
111+
return fail()
116112

117113

118114
# DO NOT override this
@@ -130,17 +126,13 @@ func tick(agent: Node, blackboard: Blackboard) -> bool:
130126

131127
var result = _tick(agent, blackboard)
132128

133-
if result is GDScriptFunctionState:
134-
if not running():
135-
push_error(name + " exited execution, but it's not running().")
136-
assert(false)
137-
129+
if result is GDScriptFunctionState:
130+
assert(running())
138131
result = yield(result, "completed")
139132

140-
if running():
141-
push_error(name + " completed but it is still running(). Must either succeed() or fail().")
142-
assert(false)
143-
133+
assert(not running())
134+
135+
emit_signal("tick", result)
144136
return result
145137

146138

bt_random_selector.gd

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ extends BTComposite
88
func _tick(agent: Node, blackboard: Blackboard) -> bool:
99
randomize()
1010
var result
11-
12-
for i in get_child_count():
13-
var rand_idx = randi() % get_child_count()
11+
var n_children = get_child_count()
12+
13+
for i in n_children:
14+
var rand_idx = randi() % n_children
1415
bt_child = get_child(rand_idx)
1516

1617
result = bt_child.tick(agent, blackboard)

0 commit comments

Comments
 (0)