Clean and improve BehaviorTree resource class

This commit is contained in:
Serhii Snitsaruk 2022-12-16 14:29:14 +01:00
parent 39f504f42c
commit 0bb03c2b4c
5 changed files with 39 additions and 24 deletions

View File

@ -7,21 +7,21 @@
#include "core/templates/list.h"
#include "core/variant/variant.h"
void BehaviorTree::init() {
List<BTTask *> stack;
BTTask *task = root_task.ptr();
while (task != nullptr) {
for (int i = 0; i < task->get_child_count(); i++) {
task->get_child(i)->parent = task;
stack.push_back(task->get_child(i).ptr());
}
task = nullptr;
if (!stack.is_empty()) {
task = stack.front()->get();
stack.pop_front();
}
}
}
// void BehaviorTree::init() {
// List<BTTask *> stack;
// BTTask *task = root_task.ptr();
// while (task != nullptr) {
// for (int i = 0; i < task->get_child_count(); i++) {
// task->get_child(i)->parent = task;
// stack.push_back(task->get_child(i).ptr());
// }
// task = nullptr;
// if (!stack.is_empty()) {
// task = stack.front()->get();
// stack.pop_front();
// }
// }
// }
Ref<BehaviorTree> BehaviorTree::clone() const {
Ref<BehaviorTree> copy = duplicate(false);
@ -32,7 +32,7 @@ Ref<BehaviorTree> BehaviorTree::clone() const {
return copy;
}
Ref<BTTask> BehaviorTree::instance(Object *p_agent, const Ref<Blackboard> &p_blackboard) const {
Ref<BTTask> BehaviorTree::instantiate(Node *p_agent, const Ref<Blackboard> &p_blackboard) const {
ERR_FAIL_COND_V_MSG(root_task == nullptr, memnew(BTTask), "Trying to instance a behavior tree with no valid root task.");
Ref<BTTask> inst = root_task->clone();
inst->initialize(p_agent, p_blackboard);
@ -44,10 +44,10 @@ void BehaviorTree::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_description"), &BehaviorTree::get_description);
ClassDB::bind_method(D_METHOD("set_root_task", "p_value"), &BehaviorTree::set_root_task);
ClassDB::bind_method(D_METHOD("get_root_task"), &BehaviorTree::get_root_task);
ClassDB::bind_method(D_METHOD("init"), &BehaviorTree::init);
// ClassDB::bind_method(D_METHOD("init"), &BehaviorTree::init);
ClassDB::bind_method(D_METHOD("clone"), &BehaviorTree::clone);
ClassDB::bind_method(D_METHOD("instance", "p_agent", "p_blackboard"), &BehaviorTree::instance);
ClassDB::bind_method(D_METHOD("instantiate", "p_agent", "p_blackboard"), &BehaviorTree::instantiate);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "description", PROPERTY_HINT_MULTILINE_TEXT), "set_description", "get_description");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "root_task", PROPERTY_HINT_RESOURCE_TYPE, "BTTask"), "set_root_task", "get_root_task");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "root_task", PROPERTY_HINT_RESOURCE_TYPE, "BTTask", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_root_task", "get_root_task");
}

View File

@ -31,9 +31,9 @@ public:
}
Ref<BTTask> get_root_task() const { return root_task; }
void init();
// void init();
Ref<BehaviorTree> clone() const;
Ref<BTTask> instance(Object *p_agent, const Ref<Blackboard> &p_blackboard) const;
Ref<BTTask> instantiate(Node *p_agent, const Ref<Blackboard> &p_blackboard) const;
};
#endif // BEHAVIOR_TREE_H

View File

@ -24,7 +24,7 @@ void BTPlayer::_load_tree() {
// blackboard->prefetch_nodepath_vars(get_owner());
blackboard->prefetch_nodepath_vars(this);
}
_root_task = _loaded_tree->instance(get_owner(), blackboard);
_root_task = _loaded_tree->instantiate(get_owner(), blackboard);
}
void BTPlayer::set_behavior_tree(const Ref<BehaviorTree> &p_tree) {

View File

@ -7,7 +7,7 @@
#include "modules/limboai/limbo_state.h"
void BTState::_setup() {
root_task = behavior_tree->instance(get_agent(), get_blackboard());
root_task = behavior_tree->instantiate(get_agent(), get_blackboard());
}
void BTState::_exit() {

View File

@ -1,6 +1,21 @@
[gd_resource type="BehaviorTree" load_steps=2 format=3 uid="uid://6l0b3cyvwy56"]
[gd_resource type="BehaviorTree" load_steps=6 format=3 uid="uid://6l0b3cyvwy56"]
[sub_resource type="BTSequence" id="BTSequence_hlsn5"]
custom_name = "3"
[sub_resource type="BTSequence" id="BTSequence_g3hm1"]
custom_name = "2"
children = [SubResource("BTSequence_hlsn5")]
[sub_resource type="BTSequence" id="BTSequence_b0qql"]
custom_name = "4"
[sub_resource type="BTSequence" id="BTSequence_78a5u"]
custom_name = "5"
[sub_resource type="BTSequence" id="1"]
custom_name = "1"
children = [SubResource("BTSequence_g3hm1"), SubResource("BTSequence_b0qql"), SubResource("BTSequence_78a5u")]
[resource]
root_task = SubResource("1")