diff --git a/bt/behavior_tree.cpp b/bt/behavior_tree.cpp index 421a57b..52dea44 100644 --- a/bt/behavior_tree.cpp +++ b/bt/behavior_tree.cpp @@ -7,21 +7,21 @@ #include "core/templates/list.h" #include "core/variant/variant.h" -void BehaviorTree::init() { - List 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 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::clone() const { Ref copy = duplicate(false); @@ -32,7 +32,7 @@ Ref BehaviorTree::clone() const { return copy; } -Ref BehaviorTree::instance(Object *p_agent, const Ref &p_blackboard) const { +Ref BehaviorTree::instantiate(Node *p_agent, const Ref &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 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"); } \ No newline at end of file diff --git a/bt/behavior_tree.h b/bt/behavior_tree.h index 1bba891..68ac7b7 100644 --- a/bt/behavior_tree.h +++ b/bt/behavior_tree.h @@ -31,9 +31,9 @@ public: } Ref get_root_task() const { return root_task; } - void init(); + // void init(); Ref clone() const; - Ref instance(Object *p_agent, const Ref &p_blackboard) const; + Ref instantiate(Node *p_agent, const Ref &p_blackboard) const; }; #endif // BEHAVIOR_TREE_H \ No newline at end of file diff --git a/bt/bt_player.cpp b/bt/bt_player.cpp index ee55626..64bda5a 100644 --- a/bt/bt_player.cpp +++ b/bt/bt_player.cpp @@ -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 &p_tree) { diff --git a/bt/bt_state.cpp b/bt/bt_state.cpp index 746c8d0..9870413 100644 --- a/bt/bt_state.cpp +++ b/bt/bt_state.cpp @@ -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() { diff --git a/test/ai/trees/test.tres b/test/ai/trees/test.tres index e09cfd4..67bad09 100644 --- a/test/ai/trees/test.tres +++ b/test/ai/trees/test.tres @@ -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")