Skip to content

中文 | English

API: wingman.bt ​

Behavior tree engine, providing a basic AI decision system.

[The three basic functions create/tick/remove are implemented, plus node construction (sequence/selector/parallel/inverter/repeat/wait/condition action) and assembly (addChild/setRoot), so a behavior tree can be fully built and ticked on the script side (see Node Construction).]

Module Overview ​

The bt module provides basic execution management for behavior trees:

  • Execution management - create, execute, and remove behavior trees

Node States ​

Each node returns one of the following states after execution:

StateDescription
SUCCESSSuccess
FAILUREFailure
RUNNINGRunning

Create Behavior Tree ​

create(name) / create(name) ​

Description: Creates a new behavior tree.

Function signature:

python
create(name: str) -> bool
lua
create(name: string) -> boolean

Parameters:

  • name - behavior tree name

Returns:

  • Whether creation succeeded
python
from wingman import bt

# 创建一个名为 "combat_tree" 的行为树
ok = bt.create("combat_tree")
if not ok:
    print("创建失败")

Execute Behavior Tree ​

tick(tree_name) / tick(treeName) ​

Description: Executes the behavior tree once and returns the execution status.

Function signature:

python
tick(tree_name: str) -> str
lua
tick(treeName: string) -> string

Parameters:

  • tree_name - behavior tree name

Returns:

  • Execution status: "SUCCESS" / "FAILURE" / "RUNNING"
python
from wingman import bt

status = bt.tick("combat_tree")
print(f"状态: {status}")  # SUCCESS/FAILURE/RUNNING

Remove Behavior Tree ​

remove(tree_name) / remove(treeName) ​

Description: Removes the specified behavior tree and releases its resources.

Function signature:

python
remove(tree_name: str) -> None
lua
remove(treeName: string) -> nil

Parameters:

  • tree_name - behavior tree name

Returns:

  • None
python
from wingman import bt

# 移除行为树
bt.remove("combat_tree")

Available Interfaces ​

Python functionLua functionDescriptionParameters
create(name)create(name)Create behavior treename: behavior tree name
Return: whether successful
tick(tree_name)tick(treeName)Execute behavior treetree_name: behavior tree name
Return: execution status
remove(tree_name)remove(treeName)Remove behavior treetree_name: behavior tree name

Node Construction ​

Behavior trees are composed of nodes. Construction functions return a node handle (int, ≥1 means valid, 0 means invalid), which are assembled with addChild and attached to a tree with setRoot.

Composite Nodes ​

Python functionLua functionDescriptionParameters
sequence(name?)sequence(name?)Sequence: succeeds only when all child nodes succeedname: node name (optional) → handle
selector(name?)selector(name?)Selector: succeeds when any child node succeedsname → handle
parallel(name?, policy?)parallel(name?, policy?)Parallelpolicy: SUCCEED_ON_ALL/SUCCEED_ON_ONE/FAIL_ON_ALL/FAIL_ON_ONE (default SUCCEED_ON_ALL) → handle

Decorator Nodes ​

Python functionLua functionDescriptionParameters
inverter(child)inverter(child)Inverts the child node's resultchildHandle → handle
repeat(child, count?)repeat(child, count?)Repeatedly executes the child nodechildHandle, count (default -1=infinite) → handle

Leaf Nodes ​

Python functionLua functionDescriptionParameters
wait(ms)wait(ms)Wait for the given number of millisecondsmilliseconds → handle
condition(name, callback)condition(name, callback)Condition: callback returns boolname, callback(): bool → handle
action(name, callback)action(name, callback)Action: callback returns a statusname, callback(): "SUCCESS"/"FAILURE"/"RUNNING" → handle

Assembly ​

Python functionLua functionDescriptionParameters
add_child(parent, child)addChild(parent, child)Adds a child node to a composite nodeparentHandle, childHandle → bool
set_root(tree, node)setRoot(tree, node)Sets the root node of a behavior treetreeName, nodeHandle → bool

The condition/action callbacks are invoked when the behavior tree is ticked (no arguments). Lua callbacks must tick on the same thread that created the script (the Lua state is not thread-safe); Python callbacks are thread-safe (GIL).

Example ​

python
from wingman import bt

bt.create("combat")
cond = bt.condition("enemy_near", lambda: True)
act = bt.action("attack", lambda: "SUCCESS")
seq = bt.sequence()
bt.add_child(seq, cond)
bt.add_child(seq, act)
bt.set_root("combat", seq)
print(bt.tick("combat"))  # SUCCESS