diff --git a/bt/tasks/composites/bt_probability_selector.cpp b/bt/tasks/composites/bt_probability_selector.cpp index 575633b..096c5b9 100644 --- a/bt/tasks/composites/bt_probability_selector.cpp +++ b/bt/tasks/composites/bt_probability_selector.cpp @@ -16,15 +16,20 @@ #include "core/error/error_macros.h" double BTProbabilitySelector::get_weight(int p_index) const { + ERR_FAIL_INDEX_V(p_index, get_child_count(), 0.0); + ERR_FAIL_COND_V(get_child(p_index)->is_class_ptr(BTComment::get_class_ptr_static()), 0.0); return _get_weight(p_index); } void BTProbabilitySelector::set_weight(int p_index, double p_weight) { + ERR_FAIL_INDEX(p_index, get_child_count()); + ERR_FAIL_COND(get_child(p_index)->is_class_ptr(BTComment::get_class_ptr_static())); _set_weight(p_index, p_weight); } double BTProbabilitySelector::get_probability(int p_index) const { ERR_FAIL_INDEX_V(p_index, get_child_count(), 0.0); + ERR_FAIL_COND_V(get_child(p_index)->is_class_ptr(BTComment::get_class_ptr_static()), 0.0); double total = _get_total_weight(); return total == 0.0 ? 0.0 : _get_weight(p_index) / total; } @@ -33,6 +38,7 @@ void BTProbabilitySelector::set_probability(int p_index, double p_probability) { ERR_FAIL_INDEX(p_index, get_child_count()); ERR_FAIL_COND(p_probability < 0.0); ERR_FAIL_COND(p_probability >= 1.0); + ERR_FAIL_COND(get_child(p_index)->is_class_ptr(BTComment::get_class_ptr_static())); double others_total = _get_total_weight() - _get_weight(p_index); double others_probability = 1.0 - p_probability; @@ -44,6 +50,11 @@ void BTProbabilitySelector::set_probability(int p_index, double p_probability) { } } +bool BTProbabilitySelector::has_probability(int p_index) const { + ERR_FAIL_INDEX_V(p_index, get_child_count(), false); + return !get_child(p_index)->is_class_ptr(BTComment::get_class_ptr_static()); +} + void BTProbabilitySelector::set_abort_on_failure(bool p_abort_on_failure) { abort_on_failure = p_abort_on_failure; emit_changed(); @@ -115,6 +126,7 @@ void BTProbabilitySelector::_bind_methods() { ClassDB::bind_method(D_METHOD("get_total_weight"), &BTProbabilitySelector::get_total_weight); ClassDB::bind_method(D_METHOD("get_probability", "p_index"), &BTProbabilitySelector::get_probability); ClassDB::bind_method(D_METHOD("set_probability", "p_index", "p_probability"), &BTProbabilitySelector::set_probability); + ClassDB::bind_method(D_METHOD("has_probability", "p_index"), &BTProbabilitySelector::has_probability); ClassDB::bind_method(D_METHOD("get_abort_on_failure"), &BTProbabilitySelector::get_abort_on_failure); ClassDB::bind_method(D_METHOD("set_abort_on_failure", "p_value"), &BTProbabilitySelector::set_abort_on_failure); diff --git a/bt/tasks/composites/bt_probability_selector.h b/bt/tasks/composites/bt_probability_selector.h index 4e893ff..12b1c25 100644 --- a/bt/tasks/composites/bt_probability_selector.h +++ b/bt/tasks/composites/bt_probability_selector.h @@ -12,6 +12,7 @@ #ifndef BT_PROBABILITY_SELECTOR_H #define BT_PROBABILITY_SELECTOR_H +#include "modules/limboai/bt/tasks/bt_comment.h" #include "modules/limboai/bt/tasks/bt_composite.h" #include "core/core_string_names.h" @@ -37,7 +38,9 @@ private: _FORCE_INLINE_ double _get_total_weight() const { double total = 0.0; for (int i = 0; i < get_child_count(); i++) { - total += _get_weight(i); + if (!get_child(i)->is_class_ptr(BTComment::get_class_ptr_static())) { + total += _get_weight(i); + } } return total; } @@ -57,6 +60,8 @@ public: double get_probability(int p_index) const; void set_probability(int p_index, double p_probability); + bool has_probability(int p_index) const; + void set_abort_on_failure(bool p_abort_on_failure); bool get_abort_on_failure() const; }; diff --git a/doc_classes/BTProbabilitySelector.xml b/doc_classes/BTProbabilitySelector.xml index 3eb00c9..2cb45d1 100644 --- a/doc_classes/BTProbabilitySelector.xml +++ b/doc_classes/BTProbabilitySelector.xml @@ -34,6 +34,13 @@ Returns the child task's weight within the weighted probability selection algorithm. + + + + + Returns whether the child task at index [param p_index] participates within the weighted probability selection algorithm and has a probability assigned to it. Returns [code]false[/code] for [BTComment] tasks. + + diff --git a/editor/task_tree.cpp b/editor/task_tree.cpp index 9480705..f5ed104 100644 --- a/editor/task_tree.cpp +++ b/editor/task_tree.cpp @@ -38,7 +38,7 @@ void TaskTree::_update_item(TreeItem *p_item) { if (p_item->get_parent()) { Ref sel = p_item->get_parent()->get_metadata(0); - if (sel.is_valid()) { + if (sel.is_valid() && sel->has_probability(p_item->get_index())) { p_item->set_custom_draw(0, this, SNAME("_draw_probability")); p_item->set_cell_mode(0, TreeItem::CELL_MODE_CUSTOM); } @@ -236,7 +236,7 @@ double TaskTree::get_selected_probability_percent() const { bool TaskTree::selected_has_probability() const { bool result = false; Ref selected = get_selected(); - if (selected.is_valid()) { + if (selected.is_valid() && !selected->is_class_ptr(BTComment::get_class_ptr_static())) { Ref probability_selector = selected->get_parent(); result = probability_selector.is_valid(); }