前置声明:行为树执行上下文类
More...
#include <nbt.h>
|
|
| nbt_context ()=default |
| | 默认构造函数
|
| | ~nbt_context () |
| | 析构函数。
|
|
| nbt_context (const nbt_context &)=delete |
| | 禁止拷贝构造
|
| void | init (TACTOR *aactor, nbt_factory< TACTOR > *afactory) |
| | 初始化上下文,绑定Actor和工厂。
|
| bool | load_tree (const std::string &atreeid) |
| | 通过已注册的行为树ID加载行为树。
|
| bool | load_tree_xml (const std::string &axml) |
| | 从XML字符串加载行为树。
|
| nbt_status | tick () |
| | 执行一次行为树tick。
|
| void | halt () |
| | 停止并重置当前行为树。
|
| bool | empty () const |
| | 检查是否已加载行为树。
|
| template<typename T> |
| void | set (const std::string &akey, const T &aval) |
| | 在黑板上设置值。
|
| template<typename T> |
| T | get (const std::string &akey) const |
| | 从黑板获取值。
|
| TACTOR * | get_actor () |
| | 获取绑定的Actor实例指针。
|
| int64_t | get_actorid () |
| | 获取绑定Actor的唯一标识。
|
| BT::Blackboard::Ptr | blackboard () |
| | 获取底层黑板指针。
|
template<typename TACTOR>
class ngl::nbt_context< TACTOR >
前置声明:行为树执行上下文类
行为树执行上下文类。
每个Actor实例持有一个上下文对象,桥接行为树和所属Actor。 管理行为树的生命周期(加载、tick、停止),并通过黑板(Blackboard) 在行为树节点间共享数据。
- Template Parameters
-
- Note
- 生命周期:init() → load_tree()/load_tree_xml() → tick()(每帧调用)→ halt()(停止)
-
黑板数据通过 set()/get() 模板方法读写,支持任意类型。
-
禁止拷贝构造和赋值操作。
ctx.load_tree("patrol_tree");
auto status = ctx.tick();
◆ ~nbt_context()
template<typename TACTOR>
析构函数。
自动调用halt()停止行为树并释放资源。
◆ blackboard()
template<typename TACTOR>
获取底层黑板指针。
- Returns
- BT::Blackboard::Ptr 黑板智能指针
◆ empty()
template<typename TACTOR>
检查是否已加载行为树。
- Returns
- true 未加载行为树
-
false 已加载行为树
◆ get()
template<typename TACTOR>
template<typename T>
从黑板获取值。
- Template Parameters
-
- Parameters
-
- Returns
- T 存储的值
- Exceptions
-
| std::runtime_error | 键不存在时抛出异常 |
◆ get_actor()
template<typename TACTOR>
获取绑定的Actor实例指针。
- Returns
- TACTOR* Actor指针,可能为nullptr
◆ get_actorid()
template<typename TACTOR>
获取绑定Actor的唯一标识。
- Returns
- int64_t Actor的GUID,未绑定时返回无效GUID
◆ halt()
template<typename TACTOR>
停止并重置当前行为树。
终止所有正在运行的节点,释放行为树实例。
◆ init()
template<typename TACTOR>
初始化上下文,绑定Actor和工厂。
创建黑板并将自身指针存入黑板,以便节点回调能获取上下文。
- Parameters
-
| aactor | Actor实例指针 |
| afactory | 行为树工厂指针 |
- Note
- 黑板中以TACTOR类型名为key存储上下文指针。
◆ load_tree()
template<typename TACTOR>
通过已注册的行为树ID加载行为树。
- Parameters
-
- Returns
- true 加载成功
-
false 加载失败(工厂未初始化或ID不存在)
- Note
- 加载前会自动停止当前已有的行为树。
◆ load_tree_xml()
template<typename TACTOR>
从XML字符串加载行为树。
- Parameters
-
- Returns
- true 加载成功
-
false 加载失败(工厂未初始化或XML格式错误)
- Note
- 加载前会自动停止当前已有的行为树。
◆ set()
template<typename TACTOR>
template<typename T>
| void ngl::nbt_context< TACTOR >::set |
( |
const std::string & | akey, |
|
|
const T & | aval ) |
|
inline |
在黑板上设置值。
- Template Parameters
-
- Parameters
-
- Note
- 黑板数据在所有节点间共享。
◆ tick()
template<typename TACTOR>
执行一次行为树tick。
- Returns
- 行为树根节点的执行状态
- Return values
-
| BT::NodeStatus::SUCCESS | 行为树执行成功 |
| BT::NodeStatus::FAILURE | 行为树执行失败 |
| BT::NodeStatus::RUNNING | 行为树仍在执行中 |
| BT::NodeStatus::IDLE | 行为树未加载 |
- Note
- 每帧应调用一次此方法以驱动行为树执行。
The documentation for this class was generated from the following file:
- E:/GitHub/ngl_server/public/cpp/tools/ai/nbt.h