Add BTSubtree action

This commit is contained in:
Serhii Snitsaruk 2022-09-03 20:42:38 +02:00
parent 1770c23553
commit ec9eec4120
4 changed files with 70 additions and 0 deletions

35
bt/actions/bt_subtree.cpp Normal file
View File

@ -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<BTTask> 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");
}

32
bt/actions/bt_subtree.h Normal file
View File

@ -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<BehaviorTree> subtree;
protected:
static void _bind_methods();
virtual String _generate_name() const;
public:
void set_subtree(const Ref<BehaviorTree> &p_value) {
subtree = p_value;
emit_changed();
}
Ref<BehaviorTree> get_subtree() const { return subtree; }
virtual Ref<BTTask> clone() const;
virtual String get_configuration_warning() const;
};
#endif // BT_SUBTREE_H

View File

@ -0,0 +1 @@
<svg enable-background="new 0 0 16 16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="m16 10.03v-4h-6v1.5h-6.5v-3.53h2.5v-4h-6v4h2.5v10.5h.17.83 6.5v1.5h6v-4h-6v1.5h-6.5v-4.97h6.5v1.5z" fill="#cea4f1"/></svg>

After

Width:  |  Height:  |  Size: 225 B

View File

@ -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<BTWait>();
ClassDB::register_class<BTRandomWait>();
ClassDB::register_class<BTWaitTicks>();
ClassDB::register_class<BTSubtree>();
ClassDB::register_class<BTCondition>();