From 8b1b0dd7548982b634f62dbfe8bcbcc45aabd4b8 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Wed, 21 Sep 2022 12:37:19 +0200 Subject: [PATCH] Fix decorator having no children crash --- bt/decorators/bt_always_fail.cpp | 2 +- bt/decorators/bt_always_succeed.cpp | 2 +- bt/decorators/bt_delay.cpp | 2 ++ bt/decorators/bt_invert.cpp | 1 + bt/decorators/bt_new_scope.cpp | 2 +- bt/decorators/bt_probability.cpp | 1 + bt/decorators/bt_repeat.cpp | 1 + bt/decorators/bt_repeat_until_failure.cpp | 1 + bt/decorators/bt_repeat_until_success.cpp | 1 + bt/decorators/bt_run_limit.cpp | 1 + bt/decorators/bt_subtree.cpp | 1 + bt/decorators/bt_time_limit.cpp | 1 + 12 files changed, 13 insertions(+), 3 deletions(-) diff --git a/bt/decorators/bt_always_fail.cpp b/bt/decorators/bt_always_fail.cpp index 6dc112c..cfa42a4 100644 --- a/bt/decorators/bt_always_fail.cpp +++ b/bt/decorators/bt_always_fail.cpp @@ -3,7 +3,7 @@ #include "bt_always_fail.h" int BTAlwaysFail::_tick(float p_delta) { - if (get_child(0)->execute(p_delta) == RUNNING) { + if (get_child_count() > 0 && get_child(0)->execute(p_delta) == RUNNING) { return RUNNING; } return FAILURE; diff --git a/bt/decorators/bt_always_succeed.cpp b/bt/decorators/bt_always_succeed.cpp index a7beb03..a868a0b 100644 --- a/bt/decorators/bt_always_succeed.cpp +++ b/bt/decorators/bt_always_succeed.cpp @@ -3,7 +3,7 @@ #include "bt_always_succeed.h" int BTAlwaysSucceed::_tick(float p_delta) { - if (get_child(0)->execute(p_delta) == RUNNING) { + if (get_child_count() > 0 && get_child(0)->execute(p_delta) == RUNNING) { return RUNNING; } return SUCCESS; diff --git a/bt/decorators/bt_delay.cpp b/bt/decorators/bt_delay.cpp index 53153f1..4118a26 100644 --- a/bt/decorators/bt_delay.cpp +++ b/bt/decorators/bt_delay.cpp @@ -3,6 +3,7 @@ #include "bt_delay.h" #include "core/array.h" #include "core/class_db.h" +#include "core/error_macros.h" #include "core/object.h" #include "core/variant.h" @@ -15,6 +16,7 @@ void BTDelay::_enter() { } int BTDelay::_tick(float p_delta) { + ERR_FAIL_COND_V_MSG(get_child_count() == 0, FAILURE, "BT decorator has no child."); time_passed += p_delta; if (time_passed <= seconds) { return RUNNING; diff --git a/bt/decorators/bt_invert.cpp b/bt/decorators/bt_invert.cpp index 7bf90ef..a5fcd24 100644 --- a/bt/decorators/bt_invert.cpp +++ b/bt/decorators/bt_invert.cpp @@ -3,6 +3,7 @@ #include "bt_invert.h" int BTInvert::_tick(float p_delta) { + ERR_FAIL_COND_V_MSG(get_child_count() == 0, FAILURE, "BT decorator has no child."); int status = get_child(0)->execute(p_delta); if (status == SUCCESS) { status = FAILURE; diff --git a/bt/decorators/bt_new_scope.cpp b/bt/decorators/bt_new_scope.cpp index 70adf3c..423bde9 100644 --- a/bt/decorators/bt_new_scope.cpp +++ b/bt/decorators/bt_new_scope.cpp @@ -14,7 +14,7 @@ void BTNewScope::initialize(Object *p_agent, const Ref &p_blackboard } int BTNewScope::_tick(float p_delta) { - ERR_FAIL_COND_V_MSG(get_child_count() == 0, FAILURE, "BT decorator doesn't have a child."); + ERR_FAIL_COND_V_MSG(get_child_count() == 0, FAILURE, "BT decorator has no child."); return get_child(0)->execute(p_delta); } diff --git a/bt/decorators/bt_probability.cpp b/bt/decorators/bt_probability.cpp index e70c2fc..c656c17 100644 --- a/bt/decorators/bt_probability.cpp +++ b/bt/decorators/bt_probability.cpp @@ -8,6 +8,7 @@ String BTProbability::_generate_name() const { } int BTProbability::_tick(float p_delta) { + ERR_FAIL_COND_V_MSG(get_child_count() == 0, FAILURE, "BT decorator has no child."); if (get_child(0)->get_status() == RUNNING or Math::randf() <= run_chance) { return get_child(0)->execute(p_delta); } diff --git a/bt/decorators/bt_repeat.cpp b/bt/decorators/bt_repeat.cpp index fb61449..607a4e1 100644 --- a/bt/decorators/bt_repeat.cpp +++ b/bt/decorators/bt_repeat.cpp @@ -13,6 +13,7 @@ void BTRepeat::_enter() { } int BTRepeat::_tick(float p_delta) { + ERR_FAIL_COND_V_MSG(get_child_count() == 0, FAILURE, "BT decorator has no child."); int status = get_child(0)->execute(p_delta); if (status == RUNNING) { return RUNNING; diff --git a/bt/decorators/bt_repeat_until_failure.cpp b/bt/decorators/bt_repeat_until_failure.cpp index bd911f0..21e6b73 100644 --- a/bt/decorators/bt_repeat_until_failure.cpp +++ b/bt/decorators/bt_repeat_until_failure.cpp @@ -3,6 +3,7 @@ #include "bt_repeat_until_failure.h" int BTRepeatUntilFailure::_tick(float p_delta) { + ERR_FAIL_COND_V_MSG(get_child_count() == 0, FAILURE, "BT decorator has no child."); if (get_child(0)->execute(p_delta) == FAILURE) { return SUCCESS; } diff --git a/bt/decorators/bt_repeat_until_success.cpp b/bt/decorators/bt_repeat_until_success.cpp index aa2ad59..d9247d1 100644 --- a/bt/decorators/bt_repeat_until_success.cpp +++ b/bt/decorators/bt_repeat_until_success.cpp @@ -3,6 +3,7 @@ #include "bt_repeat_until_success.h" int BTRepeatUntilSuccess::_tick(float p_delta) { + ERR_FAIL_COND_V_MSG(get_child_count() == 0, FAILURE, "BT decorator has no child."); if (get_child(0)->execute(p_delta) == SUCCESS) { return SUCCESS; } diff --git a/bt/decorators/bt_run_limit.cpp b/bt/decorators/bt_run_limit.cpp index 4b616a9..643321c 100644 --- a/bt/decorators/bt_run_limit.cpp +++ b/bt/decorators/bt_run_limit.cpp @@ -7,6 +7,7 @@ String BTRunLimit::_generate_name() const { } int BTRunLimit::_tick(float p_delta) { + ERR_FAIL_COND_V_MSG(get_child_count() == 0, FAILURE, "BT decorator has no child."); if (get_child(0)->get_status() != RUNNING) { if (_num_runs >= run_limit) { return FAILURE; diff --git a/bt/decorators/bt_subtree.cpp b/bt/decorators/bt_subtree.cpp index 50bd01e..e154fb1 100644 --- a/bt/decorators/bt_subtree.cpp +++ b/bt/decorators/bt_subtree.cpp @@ -29,6 +29,7 @@ Ref BTSubtree::clone() const { if (!Engine::get_singleton()->is_editor_hint()) { ERR_FAIL_COND_V_MSG(!subtree.is_valid(), copy, "Subtree is not assigned."); ERR_FAIL_COND_V_MSG(!subtree->get_root_task().is_valid(), copy, "Subtree root task is not valid."); + ERR_FAIL_COND_V_MSG(get_child_count() != 0, copy, "Subtree prototype shouldn't have children."); copy->add_child(subtree->get_root_task()->clone()); } diff --git a/bt/decorators/bt_time_limit.cpp b/bt/decorators/bt_time_limit.cpp index 68612cc..2ab6d2f 100644 --- a/bt/decorators/bt_time_limit.cpp +++ b/bt/decorators/bt_time_limit.cpp @@ -11,6 +11,7 @@ void BTTimeLimit::_enter() { } int BTTimeLimit::_tick(float p_delta) { + ERR_FAIL_COND_V_MSG(get_child_count() == 0, FAILURE, "BT decorator has no child."); _time_passed += p_delta; int status = get_child(0)->execute(p_delta); if (status == RUNNING and _time_passed >= time_limit) {