Skip to content

Commit f406e6f

Browse files
committed
Update README to add exemple of custom nodes and a brief description about behavior trees
1 parent fe56f40 commit f406e6f

File tree

1 file changed

+60
-1
lines changed

1 file changed

+60
-1
lines changed

README.md

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ A Behavior Tree implementation for [Godot Engine](https://godotengine.org/)
44

55
![image](documentation/assets/bt_illustration.png)
66

7+
## 📄 Yet Another Behavior Tree in a nutshell
8+
9+
This behavior tree is a Godot node that can be added to your *Scene* tree. The logic inside tree nodes will be run every frame, during *process* or *physics process*, depending on tree process mode.
10+
11+
At each frame, the `tick` function of tree nodes will be run. This function has access to the *actor* (the node the tree is describing behavior for), and a *blackboard* (allowing to share data between nodes). The tick function can either returns:
12+
- *SUCCESS*, indicating that node execution is successful,
13+
- *RUNNING*, indicating that node is doing a long computation/action/whatever you want, that is not finished yet,
14+
- *FAILURE*, indicating that something went wrong during child execution (condition not met, ...).
15+
16+
Depending on your tree structure, node result will produce various behaviors. See node documentation for mor details.
17+
718
## 📄 Features
819

920
➡️ This plugin is an implementation of well-known Behavior Trees, allowing game developers to create AI-Like behaviors for their NPCs. In addition to provide all behavior tree base nodes, this plugin also brings additional helper nodes that avoid to create custom ones.
@@ -184,4 +195,52 @@ The blackboard delete action node is a *leaf* node. It allows to erase a key fro
184195
🔑 Properties list:
185196
- `blackboard_key`: name of the key that must be erased from blackboard.
186197

187-
⚠️ Due to GDScript 2.0 restrictions, only string type keys can be set, since its not possible to export Variant variables.
198+
⚠️ Due to GDScript 2.0 restrictions, only string type keys can be set, since its not possible to export Variant variables.
199+
200+
## 📄 Creating your own nodes
201+
202+
Even if the provided node set is wonderful and all (😁), you will need to create your own nodes to describe your own behavior, your functional actions and conditions, or whatever you want.
203+
204+
*Creating your own composite and decorator nodes is off-topic here, and should be reserved for advanced users.* But creating your own actions and conditions is required, and is quite simple.
205+
206+
Empty-shell nodes **BTAction** and **BTCondition** are your entry-points. You just need to create a GDScript that extends one of this node, depending on what you want to do, and overrick the `tick` function, then code your functional stuff in it !
207+
208+
### Exemple of condition
209+
210+
```gdscript
211+
extends BTCondition
212+
class_name ConditionPlayerIsInRange
213+
@icon("res://addons/yet_another_behavior_tree/src/Assets/Icons/btcondition.png")
214+
215+
@export var player_detection_distance:int = 50
216+
217+
func tick(actor:Node2D, _blackboard:BTBlackboard) -> int:
218+
var player_position:Vector2 = get_tree().get_nodes_in_group("player")[0].global_position
219+
var actor_position:Vector2 = actor.global_position
220+
var player_distance:float = actor_position.distance_to(player_position)
221+
222+
if player_distance <= player_detection_distance:
223+
return BTTickResult.SUCCESS
224+
225+
return BTTickResult.FAILURE
226+
227+
```
228+
229+
### Exemple of action
230+
231+
```gdscript
232+
extends BTAction
233+
class_name ActionWander
234+
@icon("res://addons/yet_another_behavior_tree/src/Assets/Icons/btaction.png")
235+
236+
func tick(actor:Node2D, blackboard:BTBlackboard) -> int:
237+
var current_position:Vector2 = actor.global_position
238+
var target_position:Vector2 = blackboard.get_data("wander_position")
239+
if current_position.distance_to(target_position) < 5:
240+
return BTTickResult.SUCCESS
241+
else:
242+
var direction:Vector2 = (target_position - current_position).normalized()
243+
actor.velocity = direction
244+
return BTTickResult.RUNNING
245+
246+
```

0 commit comments

Comments
 (0)