Refactoring and clean up

This commit is contained in:
Serhii Snitsaruk 2024-01-25 14:27:14 +01:00
parent fb1ab16682
commit 8155c2764a
14 changed files with 36 additions and 50 deletions

View File

@ -28,8 +28,8 @@ using namespace godot;
Ref<Blackboard> Blackboard::top() const {
Ref<Blackboard> bb(this);
while (bb->get_parent_scope().is_valid()) {
bb = bb->get_parent_scope();
while (bb->get_parent().is_valid()) {
bb = bb->get_parent();
}
return bb;
}
@ -45,11 +45,11 @@ Variant Blackboard::get_var(const String &p_name, const Variant &p_default) cons
}
void Blackboard::set_var(const String &p_name, const Variant &p_value) {
// TODO: Check if p_value can be converted into required type!
if (data.has(p_name)) {
// Not checking type - allowing duck-typing.
data[p_name].set_value(p_value);
} else {
BBVariable var;
BBVariable var(p_value.get_type());
var.set_value(p_value);
data.insert(p_name, var);
}
@ -85,11 +85,9 @@ void Blackboard::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_var", "p_name", "p_default"), &Blackboard::get_var, Variant());
ClassDB::bind_method(D_METHOD("set_var", "p_name", "p_value"), &Blackboard::set_var);
ClassDB::bind_method(D_METHOD("has_var", "p_name"), &Blackboard::has_var);
ClassDB::bind_method(D_METHOD("set_parent_scope", "p_blackboard"), &Blackboard::set_parent_scope);
ClassDB::bind_method(D_METHOD("get_parent_scope"), &Blackboard::get_parent_scope);
ClassDB::bind_method(D_METHOD("set_parent_scope", "p_blackboard"), &Blackboard::set_parent);
ClassDB::bind_method(D_METHOD("get_parent_scope"), &Blackboard::get_parent);
ClassDB::bind_method(D_METHOD("erase_var", "p_name"), &Blackboard::erase_var);
ClassDB::bind_method(D_METHOD("prefetch_nodepath_vars", "p_node"), &Blackboard::prefetch_nodepath_vars);
ClassDB::bind_method(D_METHOD("top"), &Blackboard::top);
// ClassDB::bind_method(D_METHOD("get_data"), &Blackboard::get_data);
// ClassDB::bind_method(D_METHOD("set_data", "p_data"), &Blackboard::set_data);
}

View File

@ -42,8 +42,8 @@ protected:
static void _bind_methods();
public:
void set_parent_scope(const Ref<Blackboard> &p_blackboard) { parent = p_blackboard; }
Ref<Blackboard> get_parent_scope() const { return parent; }
void set_parent(const Ref<Blackboard> &p_blackboard) { parent = p_blackboard; }
Ref<Blackboard> get_parent() const { return parent; }
Ref<Blackboard> top() const;
@ -56,9 +56,7 @@ public:
void prefetch_nodepath_vars(Node *p_node);
// TODO: Rework serialization API.
// void set_data(const Dictionary &p_value);
// Dictionary get_data() const;
// TODO: Add serialization API.
};
#endif // BLACKBOARD_H

View File

@ -63,7 +63,10 @@ bool BlackboardPlan::_get(const StringName &p_name, Variant &r_ret) const {
String var_name = prop_name.get_slicec('/', 1);
String what = prop_name.get_slicec('/', 2);
ERR_FAIL_COND_V(!data.has(var_name), false);
if (what == "type") {
if (what == "name") {
r_ret = var_name;
} else if (what == "type") {
r_ret = data[var_name].get_type();
} else if (what == "value") {
r_ret = data[var_name].get_value();
@ -111,17 +114,6 @@ void BlackboardPlan::set_base_plan(const Ref<BlackboardPlan> &p_base) {
sync_with_base_plan();
}
void BlackboardPlan::set_value(const String &p_name, const Variant &p_value) {
ERR_FAIL_COND(!data.has(p_name));
data.get(p_name).set_value(p_value);
emit_changed();
}
Variant BlackboardPlan::get_value(const String &p_name) const {
ERR_FAIL_COND_V(!data.has(p_name), Variant());
return data.get(p_name).get_value();
}
void BlackboardPlan::add_var(const String &p_name, const BBVariable &p_var) {
ERR_FAIL_COND(data.has(p_name));
ERR_FAIL_COND(base.is_valid());

View File

@ -39,9 +39,6 @@ public:
void set_base_plan(const Ref<BlackboardPlan> &p_base);
Ref<BlackboardPlan> get_base_plan() const { return base; }
void set_value(const String &p_name, const Variant &p_value);
Variant get_value(const String &p_name) const;
void add_var(const String &p_name, const BBVariable &p_var);
void remove_var(const String &p_name);
BBVariable get_var(const String &p_name);

View File

@ -22,6 +22,11 @@
#include "godot_cpp/core/error_macros.hpp"
#endif // ! LIMBOAI_GDEXTENSION
void BehaviorTree::set_description(const String &p_value) {
description = p_value;
emit_changed();
}
void BehaviorTree::set_blackboard_plan(const Ref<BlackboardPlan> &p_plan) {
blackboard_plan = p_plan;
if (blackboard_plan.is_null()) {
@ -30,6 +35,11 @@ void BehaviorTree::set_blackboard_plan(const Ref<BlackboardPlan> &p_plan) {
emit_changed();
}
void BehaviorTree::set_root_task(const Ref<BTTask> &p_value) {
root_task = p_value;
emit_changed();
}
Ref<BehaviorTree> BehaviorTree::clone() const {
Ref<BehaviorTree> copy = duplicate(false);
copy->set_path("");
@ -64,8 +74,8 @@ void BehaviorTree::_bind_methods() {
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", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_root_task", "get_root_task");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "blackboard_plan", PROPERTY_HINT_RESOURCE_TYPE, "BlackboardPlan", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT), "set_blackboard_plan", "get_blackboard_plan");
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");
}
BehaviorTree::BehaviorTree() {

View File

@ -40,19 +40,13 @@ public:
virtual bool editor_can_reload_from_file() override { return false; }
#endif
void set_description(const String &p_value);
String get_description() const { return description; }
void set_blackboard_plan(const Ref<BlackboardPlan> &p_plan);
Ref<BlackboardPlan> get_blackboard_plan() const { return blackboard_plan; }
void set_description(String p_value) {
description = p_value;
emit_changed();
}
String get_description() const { return description; }
void set_root_task(const Ref<BTTask> &p_value) {
root_task = p_value;
emit_changed();
}
void set_root_task(const Ref<BTTask> &p_value);
Ref<BTTask> get_root_task() const { return root_task; }
Ref<BehaviorTree> clone() const;

View File

@ -22,7 +22,7 @@ void BTNewScope::initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard)
bb = Ref<Blackboard>(memnew(Blackboard));
}
bb->set_parent_scope(p_blackboard);
bb->set_parent(p_blackboard);
BTDecorator::initialize(p_agent, bb);
}

View File

@ -3,7 +3,7 @@
[ext_resource type="Script" path="res://demo/ai/tasks/arrive_pos.gd" id="1_rhs33"]
[sub_resource type="BlackboardPlan" id="BlackboardPlan_2hcqi"]
var/speed/name = null
var/speed/name = "speed"
var/speed/type = 3
var/speed/value = 200.0
var/speed/hint = 1
@ -43,5 +43,5 @@ duration = 3.0
children = [SubResource("BTCooldown_gen0l")]
[resource]
root_task = SubResource("BTSelector_5dclr")
blackboard_plan = SubResource("BlackboardPlan_2hcqi")
root_task = SubResource("BTSelector_5dclr")

View File

@ -24,5 +24,3 @@ func _ready() -> void:
waypoints.reverse()
for wp in waypoints:
agent2.add_waypoint(wp.global_position)

View File

@ -5,9 +5,9 @@
[ext_resource type="Texture2D" uid="uid://d0mht3ntak7e5" path="res://demo/godot.png" id="3_64ge2"]
[sub_resource type="BlackboardPlan" id="BlackboardPlan_b86q8"]
var/speed/name = null
var/speed/name = "speed"
var/speed/type = 3
var/speed/value = 200.0
var/speed/value = 300.0
var/speed/hint = 1
var/speed/hint_string = "10,1000,10"

View File

@ -34,7 +34,6 @@ private:
} theme_cache;
int last_index = 0;
int drag_mouse_y_delta = 0;
int drag_index = -1;

View File

@ -190,7 +190,7 @@ bool LimboHSM::dispatch(const String &p_event, const Variant &p_cargo) {
void LimboHSM::initialize(Node *p_agent, const Ref<Blackboard> &p_parent_scope) {
ERR_FAIL_COND(p_agent == nullptr);
if (!p_parent_scope.is_null()) {
blackboard->set_parent_scope(p_parent_scope);
blackboard->set_parent(p_parent_scope);
}
_initialize(p_agent, nullptr);

View File

@ -71,7 +71,7 @@ void LimboState::_initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard)
if (!p_blackboard.is_null()) {
if (blackboard_plan.is_valid() && !blackboard_plan->is_empty()) {
blackboard = blackboard_plan->create_blackboard();
blackboard->set_parent_scope(p_blackboard);
blackboard->set_parent(p_blackboard);
} else {
blackboard = p_blackboard;
}

View File

@ -50,7 +50,7 @@ TEST_CASE("[Modules][LimboAI] BTNewScope") {
CHECK(ns->get_blackboard() != parent->get_blackboard());
CHECK(ns->get_blackboard() == child->get_blackboard());
CHECK(parent->get_blackboard() == parent_bb);
CHECK(ns->get_blackboard()->get_parent_scope() == parent_bb);
CHECK(ns->get_blackboard()->get_parent() == parent_bb);
ns->get_blackboard()->set_var("fruit", "pear"); // * override "fruit"