ngl_server 1.0
基于 Actor 模型的 C++ 服务器框架
ngl::nbt_context< TACTOR > Class Template Reference

前置声明:行为树执行上下文类 More...

#include <nbt.h>

Public Member Functions

 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>
get (const std::string &akey) const
 从黑板获取值。
TACTOR * get_actor ()
 获取绑定的Actor实例指针。
int64_t get_actorid ()
 获取绑定Actor的唯一标识。
BT::Blackboard::Ptr blackboard ()
 获取底层黑板指针。

Detailed Description

template<typename TACTOR>
class ngl::nbt_context< TACTOR >

前置声明:行为树执行上下文类

行为树执行上下文类。

每个Actor实例持有一个上下文对象,桥接行为树和所属Actor。 管理行为树的生命周期(加载、tick、停止),并通过黑板(Blackboard) 在行为树节点间共享数据。

Template Parameters
TACTORActor类型
Note
生命周期:init() → load_tree()/load_tree_xml() → tick()(每帧调用)→ halt()(停止)
黑板数据通过 set()/get() 模板方法读写,支持任意类型。
禁止拷贝构造和赋值操作。
// 使用示例
ctx.load_tree("patrol_tree");
// 每帧调用
auto status = ctx.tick();

Constructor & Destructor Documentation

◆ ~nbt_context()

template<typename TACTOR>
ngl::nbt_context< TACTOR >::~nbt_context ( )
inline

析构函数。

自动调用halt()停止行为树并释放资源。

Member Function Documentation

◆ blackboard()

template<typename TACTOR>
BT::Blackboard::Ptr ngl::nbt_context< TACTOR >::blackboard ( )
inline

获取底层黑板指针。

Returns
BT::Blackboard::Ptr 黑板智能指针

◆ empty()

template<typename TACTOR>
bool ngl::nbt_context< TACTOR >::empty ( ) const
inline

检查是否已加载行为树。

Returns
true 未加载行为树
false 已加载行为树

◆ get()

template<typename TACTOR>
template<typename T>
T ngl::nbt_context< TACTOR >::get ( const std::string & akey) const
inline

从黑板获取值。

Template Parameters
T值类型
Parameters
akey键名
Returns
T 存储的值
Exceptions
std::runtime_error键不存在时抛出异常

◆ get_actor()

template<typename TACTOR>
TACTOR * ngl::nbt_context< TACTOR >::get_actor ( )
inline

获取绑定的Actor实例指针。

Returns
TACTOR* Actor指针,可能为nullptr

◆ get_actorid()

template<typename TACTOR>
int64_t ngl::nbt_context< TACTOR >::get_actorid ( )
inline

获取绑定Actor的唯一标识。

Returns
int64_t Actor的GUID,未绑定时返回无效GUID

◆ halt()

template<typename TACTOR>
void ngl::nbt_context< TACTOR >::halt ( )
inline

停止并重置当前行为树。

终止所有正在运行的节点,释放行为树实例。

◆ init()

template<typename TACTOR>
void ngl::nbt_context< TACTOR >::init ( TACTOR * aactor,
nbt_factory< TACTOR > * afactory )
inline

初始化上下文,绑定Actor和工厂。

创建黑板并将自身指针存入黑板,以便节点回调能获取上下文。

Parameters
aactorActor实例指针
afactory行为树工厂指针
Note
黑板中以TACTOR类型名为key存储上下文指针。

◆ load_tree()

template<typename TACTOR>
bool ngl::nbt_context< TACTOR >::load_tree ( const std::string & atreeid)
inline

通过已注册的行为树ID加载行为树。

Parameters
atreeid已注册的行为树ID
Returns
true 加载成功
false 加载失败(工厂未初始化或ID不存在)
Note
加载前会自动停止当前已有的行为树。

◆ load_tree_xml()

template<typename TACTOR>
bool ngl::nbt_context< TACTOR >::load_tree_xml ( const std::string & axml)
inline

从XML字符串加载行为树。

Parameters
axml行为树XML定义字符串
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
T值类型
Parameters
akey键名
aval
Note
黑板数据在所有节点间共享。

◆ tick()

template<typename TACTOR>
nbt_status ngl::nbt_context< TACTOR >::tick ( )
inline

执行一次行为树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: