From ec9eec41206bbf487b67c1e4bc8dcb9a6ed2f053 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Sat, 3 Sep 2022 20:42:38 +0200 Subject: [PATCH] Add BTSubtree action --- bt/actions/bt_subtree.cpp | 35 +++++++++++++++++++++++++++++++++++ bt/actions/bt_subtree.h | 32 ++++++++++++++++++++++++++++++++ icons/icon_b_t_subtree.svg | 1 + register_types.cpp | 2 ++ 4 files changed, 70 insertions(+) create mode 100644 bt/actions/bt_subtree.cpp create mode 100644 bt/actions/bt_subtree.h create mode 100644 icons/icon_b_t_subtree.svg diff --git a/bt/actions/bt_subtree.cpp b/bt/actions/bt_subtree.cpp new file mode 100644 index 0000000..c289f94 --- /dev/null +++ b/bt/actions/bt_subtree.cpp @@ -0,0 +1,35 @@ +/* bt_subtree.cpp */ + +#include "bt_subtree.h" +#include "core/error_macros.h" +#include "core/object.h" +#include "core/variant.h" +#include "modules/limboai/bt/actions/bt_action.h" + +String BTSubtree::_generate_name() const { + return vformat("Subtree '%s'", subtree.is_null() ? "?" : subtree->get_path()); +} + +Ref BTSubtree::clone() const { + ERR_FAIL_COND_V_MSG(!subtree.is_valid(), nullptr, vformat("Subtree is not valid (%s)", get_agent())); + ERR_FAIL_COND_V_MSG(!subtree->get_root_task().is_valid(), nullptr, vformat("Subtree root task is not valid (%s)", get_agent())); + return subtree->get_root_task()->clone(); +} + +String BTSubtree::get_configuration_warning() const { + String warning = BTAction::get_configuration_warning(); + if (!warning.empty()) { + warning += "\n"; + } + if (subtree.is_null()) { + warning += "Subtree needs to be assigned.\n"; + } + return warning; +} + +void BTSubtree::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_subtree", "p_value"), &BTSubtree::set_subtree); + ClassDB::bind_method(D_METHOD("get_subtree"), &BTSubtree::get_subtree); + + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "subtree", PROPERTY_HINT_RESOURCE_TYPE, "BehaviorTree"), "set_subtree", "get_subtree"); +} \ No newline at end of file diff --git a/bt/actions/bt_subtree.h b/bt/actions/bt_subtree.h new file mode 100644 index 0000000..e8a271f --- /dev/null +++ b/bt/actions/bt_subtree.h @@ -0,0 +1,32 @@ +/* bt_subtree.h */ + +#ifndef BT_SUBTREE_H +#define BT_SUBTREE_H + +#include "bt_action.h" +#include "core/object.h" +#include "modules/limboai/bt/behavior_tree.h" + +class BTSubtree : public BTAction { + GDCLASS(BTSubtree, BTAction); + +private: + Ref subtree; + +protected: + static void _bind_methods(); + + virtual String _generate_name() const; + +public: + void set_subtree(const Ref &p_value) { + subtree = p_value; + emit_changed(); + } + Ref get_subtree() const { return subtree; } + + virtual Ref clone() const; + virtual String get_configuration_warning() const; +}; + +#endif // BT_SUBTREE_H \ No newline at end of file diff --git a/icons/icon_b_t_subtree.svg b/icons/icon_b_t_subtree.svg new file mode 100644 index 0000000..f8cd473 --- /dev/null +++ b/icons/icon_b_t_subtree.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/register_types.cpp b/register_types.cpp index 19cc263..41e108e 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -7,6 +7,7 @@ #include "bt/actions/bt_action.h" #include "bt/actions/bt_fail.h" #include "bt/actions/bt_random_wait.h" +#include "bt/actions/bt_subtree.h" #include "bt/actions/bt_wait.h" #include "bt/actions/bt_wait_ticks.h" #include "bt/behavior_tree.h" @@ -73,6 +74,7 @@ void register_limboai_types() { ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); + ClassDB::register_class(); ClassDB::register_class();