--- name: bt-xml-fundamentals description: 'Write Nav2 behavior tree XML files. Use when creating BT XML, understanding tick flow, or using BehaviorTree.CPP v4 syntax.' --- # BehaviorTree.CPP v4 XML Fundamentals ## XML Root Structure Every BT.CPP v4 XML file begins with the `` element declaring the format version: ```xml ``` The `BTCPP_format="4"` attribute is **mandatory** in v4. Omitting it or using `"3"` triggers legacy parsing with different port semantics. When multiple `` blocks exist, specify which tree to execute: ```xml ... ... ``` If `main_tree_to_execute` is omitted and only one tree exists, it is used by default. With multiple trees and no attribute, the factory throws an error. ## Node Type Categories BT.CPP v4 defines four fundamental node categories: | Category | Children | Return Values | Purpose | |-------------|----------|----------------------------|--------------------------------| | Action | 0 (leaf) | SUCCESS, FAILURE, RUNNING | Execute work | | Condition | 0 (leaf) | SUCCESS, FAILURE | Check state (must NOT return RUNNING) | | Control | 1+ | SUCCESS, FAILURE, RUNNING | Route tick to children | | Decorator | 1 | SUCCESS, FAILURE, RUNNING | Modify child behavior | Condition nodes **must never** return RUNNING. This is a hard contract—violating it causes undefined behavior in reactive control nodes. ## Port System (v4 Key Change) In v4, ports use **named attributes** instead of positional arguments. Every port has a direction: ```xml ``` Port declarations in C++: ```cpp static BT::PortsList providedPorts() { return { BT::InputPort("goal", "Target pose"), BT::OutputPort("path", "Computed path"), BT::BidirectionalPort("counter", "Visit counter") }; } ``` ### Blackboard Variable Syntax Curly braces `{variable_name}` denote blackboard remapping. The variable is resolved at runtime from the shared blackboard: ```xml ``` Literal values (no braces) are parsed directly: ```xml ``` ## Script System (v4 Feature) BT.CPP v4 introduces inline scripting for blackboard manipulation: ```xml ``` The `