Move prefetching code into `BlackboardPlan` and add checkbox to the plan editor

This commit is contained in:
Serhii Snitsaruk 2024-03-06 20:17:23 +01:00
parent f452552ecd
commit 2658060b1c
14 changed files with 91 additions and 47 deletions

View File

@ -79,19 +79,6 @@ void Blackboard::add_var(const StringName &p_name, const BBVariable &p_var) {
data.insert(p_name, p_var);
}
void Blackboard::prefetch_nodepath_vars(Node *p_node) {
ERR_FAIL_COND(p_node == nullptr);
for (const KeyValue<StringName, BBVariable> &kv : data) {
BBVariable var = kv.value;
if (var.get_value().get_type() == Variant::NODE_PATH) {
Node *fetched_node = p_node->get_node_or_null(var.get_value());
if (fetched_node != nullptr) {
var.set_value(fetched_node);
}
}
}
}
void Blackboard::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_var", "var_name", "default", "complain"), &Blackboard::get_var, Variant(), true);
ClassDB::bind_method(D_METHOD("set_var", "var_name", "value"), &Blackboard::set_var);
@ -99,7 +86,6 @@ void Blackboard::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_parent", "blackboard"), &Blackboard::set_parent);
ClassDB::bind_method(D_METHOD("get_parent"), &Blackboard::get_parent);
ClassDB::bind_method(D_METHOD("erase_var", "var_name"), &Blackboard::erase_var);
ClassDB::bind_method(D_METHOD("prefetch_nodepath_vars", "node"), &Blackboard::prefetch_nodepath_vars);
ClassDB::bind_method(D_METHOD("top"), &Blackboard::top);
ClassDB::bind_method(D_METHOD("bind_var_to_property", "var_name", "object", "property"), &Blackboard::bind_var_to_property);
ClassDB::bind_method(D_METHOD("unbind_var", "var_name"), &Blackboard::unbind_var);

View File

@ -56,8 +56,6 @@ public:
void add_var(const StringName &p_name, const BBVariable &p_var);
void prefetch_nodepath_vars(Node *p_node);
// TODO: Add serialization API.
};

View File

@ -119,6 +119,19 @@ void BlackboardPlan::set_base_plan(const Ref<BlackboardPlan> &p_base) {
notify_property_list_changed();
}
void BlackboardPlan::set_prefetch_nodepath_vars(bool p_enable) {
prefetch_nodepath_vars = p_enable;
emit_changed();
}
bool BlackboardPlan::is_prefetching_nodepath_vars() const {
if (is_derived()) {
return base->is_prefetching_nodepath_vars();
} else {
return prefetch_nodepath_vars;
}
}
void BlackboardPlan::add_var(const StringName &p_name, const BBVariable &p_var) {
ERR_FAIL_COND(var_map.has(p_name));
var_map.insert(p_name, p_var);
@ -263,15 +276,33 @@ void BlackboardPlan::sync_with_base_plan() {
}
}
Ref<Blackboard> BlackboardPlan::create_blackboard() {
// Add a variable duplicate to the blackboard, optionally with NodePath prefetch.
inline void bb_add_var_dup_with_prefetch(const Ref<Blackboard> &p_blackboard, const StringName &p_name, const BBVariable &p_var, bool p_prefetch, Node *p_node) {
if (unlikely(p_prefetch && p_var.get_type() == Variant::NODE_PATH)) {
Node *n = p_node->get_node_or_null(p_var.get_value());
BBVariable var = p_var.duplicate();
if (n != nullptr) {
var.set_value(n);
} else {
ERR_PRINT(vformat("BlackboardPlan: Prefetch failed for variable $%s with value: %s", p_name, p_var.get_value()));
}
p_blackboard->add_var(p_name, var);
} else {
p_blackboard->add_var(p_name, p_var.duplicate());
}
}
Ref<Blackboard> BlackboardPlan::create_blackboard(Node *p_node) {
ERR_FAIL_COND_V(p_node == nullptr && prefetch_nodepath_vars, memnew(Blackboard));
Ref<Blackboard> bb = memnew(Blackboard);
for (const Pair<StringName, BBVariable> &p : var_list) {
bb->add_var(p.first, p.second.duplicate());
bb_add_var_dup_with_prefetch(bb, p.first, p.second, prefetch_nodepath_vars, p_node);
}
return bb;
}
void BlackboardPlan::populate_blackboard(const Ref<Blackboard> &p_blackboard, bool overwrite) {
void BlackboardPlan::populate_blackboard(const Ref<Blackboard> &p_blackboard, bool overwrite, Node *p_node) {
ERR_FAIL_COND(p_node == nullptr && prefetch_nodepath_vars);
for (const Pair<StringName, BBVariable> &p : var_list) {
if (p_blackboard->has_var(p.first)) {
if (overwrite) {
@ -280,13 +311,18 @@ void BlackboardPlan::populate_blackboard(const Ref<Blackboard> &p_blackboard, bo
continue;
}
}
p_blackboard->add_var(p.first, p.second.duplicate());
bb_add_var_dup_with_prefetch(p_blackboard, p.first, p.second, prefetch_nodepath_vars, p_node);
}
}
void BlackboardPlan::_bind_methods() {
ClassDB::bind_method(D_METHOD("create_blackboard"), &BlackboardPlan::create_blackboard);
ClassDB::bind_method(D_METHOD("populate_blackboard", "blackboard", "overwrite"), &BlackboardPlan::populate_blackboard);
ClassDB::bind_method(D_METHOD("set_prefetch_nodepath_vars", "enable"), &BlackboardPlan::set_prefetch_nodepath_vars);
ClassDB::bind_method(D_METHOD("is_prefetching_nodepath_vars"), &BlackboardPlan::is_prefetching_nodepath_vars);
ClassDB::bind_method(D_METHOD("create_blackboard", "node"), &BlackboardPlan::create_blackboard);
ClassDB::bind_method(D_METHOD("populate_blackboard", "blackboard", "overwrite", "node"), &BlackboardPlan::populate_blackboard);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "prefetch_nodepath_vars", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_prefetch_nodepath_vars", "is_prefetching_nodepath_vars");
}
BlackboardPlan::BlackboardPlan() {

View File

@ -36,6 +36,9 @@ private:
// and only the values can be different in those variables.
Ref<BlackboardPlan> base;
// If true, NodePath variables will be prefetched, so that the vars will contain node pointers instead (upon BB creation/population).
bool prefetch_nodepath_vars = true;
protected:
static void _bind_methods();
@ -49,12 +52,15 @@ public:
void set_base_plan(const Ref<BlackboardPlan> &p_base);
Ref<BlackboardPlan> get_base_plan() const { return base; }
void set_prefetch_nodepath_vars(bool p_enable);
bool is_prefetching_nodepath_vars() const;
void add_var(const StringName &p_name, const BBVariable &p_var);
void remove_var(const StringName &p_name);
BBVariable get_var(const StringName &p_name);
Pair<StringName, BBVariable> get_var_by_index(int p_index);
bool has_var(const StringName &p_name) { return var_map.has(p_name); }
bool is_empty() const { return var_map.is_empty(); }
_FORCE_INLINE_ bool has_var(const StringName &p_name) { return var_map.has(p_name); }
_FORCE_INLINE_ bool is_empty() const { return var_map.is_empty(); }
int get_var_count() const { return var_map.size(); }
PackedStringArray list_vars() const;
@ -66,8 +72,8 @@ public:
void sync_with_base_plan();
bool is_derived() const { return base.is_valid(); }
Ref<Blackboard> create_blackboard();
void populate_blackboard(const Ref<Blackboard> &p_blackboard, bool overwrite);
Ref<Blackboard> create_blackboard(Node *p_agent);
void populate_blackboard(const Ref<Blackboard> &p_blackboard, bool overwrite, Node *p_node);
BlackboardPlan();
};

View File

@ -52,9 +52,6 @@ void BTPlayer::_load_tree() {
tree_instance.unref();
ERR_FAIL_COND_MSG(!behavior_tree.is_valid(), "BTPlayer: Needs a valid behavior tree.");
ERR_FAIL_COND_MSG(!behavior_tree->get_root_task().is_valid(), "BTPlayer: Behavior tree has no valid root task.");
if (prefetch_nodepath_vars == true) {
blackboard->prefetch_nodepath_vars(this);
}
tree_instance = behavior_tree->instantiate(get_owner(), blackboard);
#ifdef DEBUG_ENABLED
if (IS_DEBUGGER_ACTIVE()) {
@ -187,7 +184,7 @@ void BTPlayer::_notification(int p_notification) {
blackboard = Ref<Blackboard>(memnew(Blackboard));
}
if (blackboard_plan.is_valid()) {
blackboard_plan->populate_blackboard(blackboard, false);
blackboard_plan->populate_blackboard(blackboard, false, this);
}
if (behavior_tree.is_valid()) {
_load_tree();
@ -231,8 +228,6 @@ void BTPlayer::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_active"), &BTPlayer::get_active);
ClassDB::bind_method(D_METHOD("set_blackboard", "blackboard"), &BTPlayer::set_blackboard);
ClassDB::bind_method(D_METHOD("get_blackboard"), &BTPlayer::get_blackboard);
ClassDB::bind_method(D_METHOD("set_prefetch_nodepath_vars", "enable"), &BTPlayer::set_prefetch_nodepath_vars);
ClassDB::bind_method(D_METHOD("get_prefetch_nodepath_vars"), &BTPlayer::get_prefetch_nodepath_vars);
ClassDB::bind_method(D_METHOD("set_blackboard_plan", "plan"), &BTPlayer::set_blackboard_plan);
ClassDB::bind_method(D_METHOD("get_blackboard_plan"), &BTPlayer::get_blackboard_plan);
@ -248,7 +243,6 @@ void BTPlayer::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "active"), "set_active", "get_active");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "blackboard", PROPERTY_HINT_NONE, "Blackboard", 0), "set_blackboard", "get_blackboard");
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::BOOL, "prefetch_nodepath_vars"), "set_prefetch_nodepath_vars", "get_prefetch_nodepath_vars");
BIND_ENUM_CONSTANT(IDLE);
BIND_ENUM_CONSTANT(PHYSICS);

View File

@ -41,7 +41,6 @@ private:
UpdateMode update_mode = UpdateMode::PHYSICS;
bool active = true;
Ref<Blackboard> blackboard;
bool prefetch_nodepath_vars = true;
int last_status = -1;
Ref<BTTask> tree_instance;
@ -70,9 +69,6 @@ public:
Ref<Blackboard> get_blackboard() const { return blackboard; }
void set_blackboard(const Ref<Blackboard> &p_blackboard) { blackboard = p_blackboard; }
void set_prefetch_nodepath_vars(bool p_value) { prefetch_nodepath_vars = p_value; }
bool get_prefetch_nodepath_vars() const { return prefetch_nodepath_vars; }
void update(double p_delta);
void restart();
int get_last_status() const { return last_status; }

View File

@ -34,6 +34,7 @@ void BTState::set_behavior_tree(const Ref<BehaviorTree> &p_tree) {
if (p_tree.is_valid()) {
p_tree->connect(LW_NAME(changed), callable_mp(this, &BTState::_update_blackboard_plan));
}
_update_blackboard_plan();
}
behavior_tree = p_tree;
}
@ -48,6 +49,7 @@ void BTState::_update_blackboard_plan() {
void BTState::_setup() {
LimboState::_setup();
ERR_FAIL_COND_MSG(behavior_tree.is_null(), "BTState: BehaviorTree is not assigned.");
// TODO: BBNode relies on agent to be scene owner, so if the user provides anything else, the behavior tree can break.
tree_instance = behavior_tree->instantiate(get_agent(), get_blackboard());
#ifdef DEBUG_ENABLED

View File

@ -33,6 +33,8 @@ protected:
void _notification(int p_notification);
virtual bool _should_use_new_scope() const override { return true; }
virtual void _setup() override;
virtual void _exit() override;
virtual void _update(double p_delta) override;

View File

@ -17,7 +17,7 @@ void BTNewScope::initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard)
Ref<Blackboard> bb;
if (blackboard_plan.is_valid()) {
bb = blackboard_plan->create_blackboard();
bb = blackboard_plan->create_blackboard(p_agent);
} else {
bb = Ref<Blackboard>(memnew(Blackboard));
}

View File

@ -176,6 +176,11 @@ void BlackboardPlanEditor::_add_var_pressed() {
scroll_container->call_deferred(LW_NAME(call_deferred), LW_NAME(set_v_scroll), 888888888);
}
void BlackboardPlanEditor::_prefetching_toggled(bool p_toggle_on) {
ERR_FAIL_COND(plan.is_null());
plan->set_prefetch_nodepath_vars(p_toggle_on);
}
void BlackboardPlanEditor::_drag_button_down(Control *p_row) {
drag_index = p_row->get_index();
drag_start = drag_index;
@ -240,7 +245,7 @@ void BlackboardPlanEditor::_refresh() {
child->queue_free();
}
// TODO: Name validation
nodepath_prefetching->set_pressed(plan->is_prefetching_nodepath_vars());
PackedStringArray names = plan->list_vars();
int idx = 0;
@ -345,6 +350,7 @@ void BlackboardPlanEditor::_notification(int p_what) {
connect(LW_NAME(visibility_changed), callable_mp(this, &BlackboardPlanEditor::_visibility_changed));
type_menu->connect(LW_NAME(id_pressed), callable_mp(this, &BlackboardPlanEditor::_type_chosen));
hint_menu->connect(LW_NAME(id_pressed), callable_mp(this, &BlackboardPlanEditor::_hint_chosen));
nodepath_prefetching->connect(LW_NAME(toggled), callable_mp(this, &BlackboardPlanEditor::_prefetching_toggled));
for (int i = 0; i < PropertyHint::PROPERTY_HINT_MAX; i++) {
hint_menu->add_item(LimboUtility::get_singleton()->get_property_hint_text(PropertyHint(i)), i);
@ -377,6 +383,13 @@ BlackboardPlanEditor::BlackboardPlanEditor() {
add_var_tool->set_focus_mode(Control::FOCUS_NONE);
add_var_tool->set_text(TTR("Add variable"));
nodepath_prefetching = memnew(CheckBox);
toolbar->add_child(nodepath_prefetching);
nodepath_prefetching->set_text(TTR("NodePath Prefetching"));
nodepath_prefetching->set_tooltip_text(TTR("If checked, NodePath variables will be prefetched on Blackboard initialization."));
nodepath_prefetching->set_h_size_flags(Control::SIZE_EXPAND | Control::SIZE_SHRINK_END);
nodepath_prefetching->set_focus_mode(Control::FOCUS_NONE);
{
// * Header
header_row = memnew(PanelContainer);

View File

@ -18,11 +18,13 @@
#ifdef LIMBOAI_MODULE
#include "editor/editor_inspector.h"
#include "scene/gui/check_box.h"
#include "scene/gui/dialogs.h"
#endif // LIMBOAI_MODULE
#ifdef LIMBOAI_GDEXTENSION
#include <godot_cpp/classes/accept_dialog.hpp>
#include <godot_cpp/classes/check_box.hpp>
#include <godot_cpp/classes/editor_inspector_plugin.hpp>
#include <godot_cpp/classes/line_edit.hpp>
#include <godot_cpp/classes/panel_container.hpp>
@ -60,6 +62,7 @@ private:
VBoxContainer *rows_vbox;
Button *add_var_tool;
CheckBox *nodepath_prefetching;
PanelContainer *header_row;
ScrollContainer *scroll_container;
PopupMenu *type_menu;
@ -78,6 +81,7 @@ private:
void _type_chosen(int id);
void _hint_chosen(int id);
void _add_var_pressed();
void _prefetching_toggled(bool p_toggle_on);
void _drag_button_down(Control *p_row);
void _drag_button_up();

View File

@ -195,9 +195,12 @@ bool LimboHSM::_dispatch(const StringName &p_event, const Variant &p_cargo) {
void LimboHSM::initialize(Node *p_agent, const Ref<Blackboard> &p_parent_scope) {
ERR_FAIL_COND(p_agent == nullptr);
ERR_FAIL_COND_MSG(!is_root(), "LimboHSM: initialize() must be called on the root HSM.");
if (!p_parent_scope.is_null()) {
blackboard->set_parent(p_parent_scope);
}
_initialize(p_agent, nullptr);
if (initial_state == nullptr) {

View File

@ -28,7 +28,7 @@
LimboState *LimboState::get_root() const {
const LimboState *state = this;
while (state->get_parent() && state->get_parent()->is_class("LimboState")) {
while (state->get_parent() && IS_CLASS(state->get_parent(), LimboState)) {
state = Object::cast_to<LimboState>(get_parent());
}
return const_cast<LimboState *>(state);
@ -68,13 +68,13 @@ void LimboState::_initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard)
ERR_FAIL_COND(p_agent == nullptr);
agent = p_agent;
if (!p_blackboard.is_null()) {
if (blackboard_plan.is_valid() && !blackboard_plan->is_empty()) {
blackboard = blackboard_plan->create_blackboard();
blackboard->set_parent(p_blackboard);
} else {
blackboard = p_blackboard;
}
if (_should_use_new_scope()) {
blackboard->set_parent(p_blackboard);
} else {
blackboard = p_blackboard;
}
if (blackboard_plan.is_valid() && !blackboard_plan->is_empty()) {
blackboard_plan->populate_blackboard(blackboard, true, this);
}
_setup();

View File

@ -15,6 +15,7 @@
#include "../blackboard/blackboard.h"
#include "../blackboard/blackboard_plan.h"
#include "../util/limbo_compat.h"
#include "../util/limbo_string_names.h"
#ifdef LIMBOAI_MODULE
@ -56,6 +57,8 @@ protected:
virtual void _initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard);
virtual bool _dispatch(const StringName &p_event, const Variant &p_cargo = Variant());
virtual bool _should_use_new_scope() const { return blackboard_plan.is_valid() || is_root(); }
virtual void _setup();
virtual void _enter();
virtual void _exit();
@ -87,6 +90,7 @@ public:
_FORCE_INLINE_ StringName event_finished() const { return LW_NAME(EVENT_FINISHED); }
LimboState *get_root() const;
_FORCE_INLINE_ bool is_root() const { return !(get_parent() && IS_CLASS(get_parent(), LimboState)); }
bool is_active() const { return active; }
void set_guard(const Callable &p_guard_callable);