Better error handling in BTState, BTPlayer & BehaviorTree

This commit is contained in:
Serhii Snitsaruk 2024-05-02 14:10:29 +02:00
parent 803da63fa8
commit e36ea6d3e6
No known key found for this signature in database
GPG Key ID: A965EF8799FFEC2D
3 changed files with 6 additions and 2 deletions

View File

@ -73,6 +73,8 @@ void BehaviorTree::copy_other(const Ref<BehaviorTree> &p_other) {
Ref<BTTask> BehaviorTree::instantiate(Node *p_agent, const Ref<Blackboard> &p_blackboard, Node *p_scene_root) const { Ref<BTTask> BehaviorTree::instantiate(Node *p_agent, const Ref<Blackboard> &p_blackboard, Node *p_scene_root) const {
ERR_FAIL_COND_V_MSG(root_task == nullptr, memnew(BTTask), "Trying to instance a behavior tree with no valid root task."); ERR_FAIL_COND_V_MSG(root_task == nullptr, memnew(BTTask), "Trying to instance a behavior tree with no valid root task.");
ERR_FAIL_NULL_V_MSG(p_agent, memnew(BTTask), "Trying to instance a behavior tree with no valid agent.");
ERR_FAIL_NULL_V_MSG(p_scene_root, memnew(BTTask), "Trying to instance a behavior tree with no valid scene root.");
Ref<BTTask> inst = root_task->clone(); Ref<BTTask> inst = root_task->clone();
inst->initialize(p_agent, p_blackboard, p_scene_root); inst->initialize(p_agent, p_blackboard, p_scene_root);
return inst; return inst;

View File

@ -55,7 +55,7 @@ void BTPlayer::_load_tree() {
Node *agent = GET_NODE(this, agent_node); Node *agent = GET_NODE(this, agent_node);
ERR_FAIL_NULL_MSG(agent, vformat("BTPlayer: Initialization failed - can't get agent with path '%s'.", agent_node)); ERR_FAIL_NULL_MSG(agent, vformat("BTPlayer: Initialization failed - can't get agent with path '%s'.", agent_node));
Node *scene_root = get_owner(); Node *scene_root = get_owner();
ERR_FAIL_NULL_MSG(scene_root, "BTPlayer: Initialization failed - can't get scene root (make sure the BTPlayer.owner is set)."); ERR_FAIL_NULL_MSG(scene_root, "BTPlayer: Initialization failed - can't get scene root (make sure the BTPlayer's owner property is set).");
tree_instance = behavior_tree->instantiate(agent, blackboard, scene_root); tree_instance = behavior_tree->instantiate(agent, blackboard, scene_root);
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
if (IS_DEBUGGER_ACTIVE()) { if (IS_DEBUGGER_ACTIVE()) {

View File

@ -52,7 +52,9 @@ void BTState::_update_blackboard_plan() {
void BTState::_setup() { void BTState::_setup() {
LimboState::_setup(); LimboState::_setup();
ERR_FAIL_COND_MSG(behavior_tree.is_null(), "BTState: BehaviorTree is not assigned."); ERR_FAIL_COND_MSG(behavior_tree.is_null(), "BTState: BehaviorTree is not assigned.");
tree_instance = behavior_tree->instantiate(get_agent(), get_blackboard(), get_owner()); Node *scene_root = get_owner();
ERR_FAIL_NULL_MSG(scene_root, "BTState: Initialization failed - can't get scene root (make sure the BTState's owner property is set).");
tree_instance = behavior_tree->instantiate(get_agent(), get_blackboard(), scene_root);
#ifdef DEBUG_ENABLED #ifdef DEBUG_ENABLED
if (tree_instance.is_valid() && IS_DEBUGGER_ACTIVE()) { if (tree_instance.is_valid() && IS_DEBUGGER_ACTIVE()) {