doc: Add custom task example

This commit is contained in:
Serhii Snitsaruk 2023-10-31 14:50:37 +01:00
parent d688953fd8
commit 793dba1e07
1 changed files with 40 additions and 0 deletions

View File

@ -25,3 +25,43 @@ Behavior Trees handle conditional logic using condition tasks. These tasks check
>**🛈 Note:** To create your own conditions, extend the `BTCondition` class. >**🛈 Note:** To create your own conditions, extend the `BTCondition` class.
Check out the `BTTask` class documentation in the editor, which provides the foundation for various building blocks of Behavior Trees. Check out the `BTTask` class documentation in the editor, which provides the foundation for various building blocks of Behavior Trees.
## Custom task example
```gdscript
@tool
extends BTCondition
## InRange checks if agent is within a range of target.
## Returns SUCCESS if agent is within a range of target;
## otherwise, returns FAILURE.
@export var distance_min: float
@export var distance_max: float
@export var target_var := "target"
var _min_distance_squared: float
var _max_distance_squared: float
func _generate_name() -> String:
return "InRange (%d, %d) of %s" % [distance_min, distance_max,
LimboUtility.decorate_var(target_var)]
func _setup() -> void:
_min_distance_squared = distance_min * distance_min
_max_distance_squared = distance_max * distance_max
func _tick(_delta: float) -> int:
var target: Node2D = blackboard.get_var(target_var, null)
if not is_instance_valid(target):
return FAILURE
var dist_sq: float = agent.global_position.distance_squared_to(target.global_position)
if dist_sq >= _min_distance_squared and dist_sq <= _max_distance_squared:
return SUCCESS
else:
return FAILURE
```