Add Blackboard class with support for scopes

This commit is contained in:
Serhii Snitsaruk 2022-09-08 15:56:51 +02:00
parent 3f04ff2eb4
commit e4f92893a1
9 changed files with 121 additions and 34 deletions

31
blackboard.cpp Normal file
View File

@ -0,0 +1,31 @@
/* blackboard.cpp */
#include "blackboard.h"
Variant Blackboard::get_var(const Variant &p_key, const Variant &p_default) const {
if (data.has(p_key)) {
return data.get_valid(p_key);
} else if (parent.is_valid()) {
return parent->get_var(p_key, p_default);
} else {
return p_default;
}
}
void Blackboard::set_var(const Variant &p_key, const Variant &p_value) {
data[p_key] = p_value;
}
bool Blackboard::has_var(const Variant &p_key) const {
return data.has(p_key) || (parent.is_valid() && parent->has_var(p_key));
}
void Blackboard::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_data"), &Blackboard::get_data);
ClassDB::bind_method(D_METHOD("set_data", "p_data"), &Blackboard::set_data);
ClassDB::bind_method(D_METHOD("get_var", "p_key", "p_default"), &Blackboard::get_var, Variant());
ClassDB::bind_method(D_METHOD("set_var", "p_key", "p_value"), &Blackboard::set_var);
ClassDB::bind_method(D_METHOD("has_var", "p_key"), &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);
}

33
blackboard.h Normal file
View File

@ -0,0 +1,33 @@
/* blackboard.h */
#ifndef BLACKBOARD_H
#define BLACKBOARD_H
#include "core/dictionary.h"
#include "core/object.h"
#include "core/reference.h"
#include "core/variant.h"
class Blackboard : public Reference {
GDCLASS(Blackboard, Reference);
private:
Dictionary data;
Ref<Blackboard> parent;
protected:
static void _bind_methods();
public:
void set_data(const Dictionary &p_value) { data = p_value; }
Dictionary get_data() const { return data; }
void set_parent_scope(const Ref<Blackboard> &p_blackboard) { parent = p_blackboard; }
Ref<Blackboard> get_parent_scope() const { return parent; }
Variant get_var(const Variant &p_key, const Variant &p_default) const;
void set_var(const Variant &p_key, const Variant &p_value);
bool has_var(const Variant &p_key) const;
};
#endif // BLACKBOARD_H

View File

@ -23,30 +23,30 @@ int BTConsolePrint::_tick(float p_delta) {
print_line(text);
} break;
case 1: {
print_line(vformat(text, get_blackboard().get(format_var_args[0], "")));
print_line(vformat(text, get_blackboard()->get_var(format_var_args[0], "")));
} break;
case 2: {
print_line(vformat(text, get_blackboard().get(format_var_args[0], ""),
get_blackboard().get(format_var_args[1], "")));
print_line(vformat(text, get_blackboard()->get_var(format_var_args[0], ""),
get_blackboard()->get_var(format_var_args[1], "")));
} break;
case 3: {
print_line(vformat(text, get_blackboard().get(format_var_args[0], ""),
get_blackboard().get(format_var_args[1], ""),
get_blackboard().get(format_var_args[2], "")));
print_line(vformat(text, get_blackboard()->get_var(format_var_args[0], ""),
get_blackboard()->get_var(format_var_args[1], ""),
get_blackboard()->get_var(format_var_args[2], "")));
} break;
case 4: {
print_line(vformat(text, get_blackboard().get(format_var_args[0], ""),
get_blackboard().get(format_var_args[1], ""),
get_blackboard().get(format_var_args[2], ""),
get_blackboard().get(format_var_args[3], "")));
print_line(vformat(text, get_blackboard()->get_var(format_var_args[0], ""),
get_blackboard()->get_var(format_var_args[1], ""),
get_blackboard()->get_var(format_var_args[2], ""),
get_blackboard()->get_var(format_var_args[3], "")));
} break;
case 5:
default: {
print_line(vformat(text, get_blackboard().get(format_var_args[0], ""),
get_blackboard().get(format_var_args[1], ""),
get_blackboard().get(format_var_args[2], ""),
get_blackboard().get(format_var_args[3], ""),
get_blackboard().get(format_var_args[4], "")));
print_line(vformat(text, get_blackboard()->get_var(format_var_args[0], ""),
get_blackboard()->get_var(format_var_args[1], ""),
get_blackboard()->get_var(format_var_args[2], ""),
get_blackboard()->get_var(format_var_args[3], ""),
get_blackboard()->get_var(format_var_args[4], "")));
} break;
}
return SUCCESS;

View File

@ -8,7 +8,9 @@
#include "core/engine.h"
#include "core/io/resource_loader.h"
#include "core/object.h"
#include "core/os/memory.h"
#include "core/variant.h"
#include "modules/limboai/blackboard.h"
#include <cstddef>
VARIANT_ENUM_CAST(BTPlayer::UpdateMode);
@ -100,6 +102,9 @@ void BTPlayer::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_blackboard", "p_blackboard"), &BTPlayer::set_blackboard);
ClassDB::bind_method(D_METHOD("get_blackboard"), &BTPlayer::get_blackboard);
ClassDB::bind_method(D_METHOD("_set_blackboard_data", "p_blackboard"), &BTPlayer::_set_blackboard_data);
ClassDB::bind_method(D_METHOD("_get_blackboard_data"), &BTPlayer::_get_blackboard_data);
ClassDB::bind_method(D_METHOD("update", "p_delta"), &BTPlayer::update);
ClassDB::bind_method(D_METHOD("restart"), &BTPlayer::restart);
@ -107,11 +112,19 @@ void BTPlayer::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::INT, "update_mode", PROPERTY_HINT_ENUM, "Idle,Physics,Manual"), "set_update_mode", "get_update_mode");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "active"), "set_active", "get_active");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_restart"), "set_auto_restart", "get_auto_restart");
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "blackboard"), "set_blackboard", "get_blackboard");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "blackboard", PROPERTY_HINT_NONE, "Blackboard", 0), "", "get_blackboard");
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "_blackboard_data"), "_set_blackboard_data", "_get_blackboard_data");
BIND_ENUM_CONSTANT(IDLE);
BIND_ENUM_CONSTANT(PHYSICS);
BIND_ENUM_CONSTANT(MANUAL);
ADD_SIGNAL(MethodInfo("behavior_tree_finished", PropertyInfo(Variant::INT, "p_status")));
}
BTPlayer::BTPlayer() {
blackboard = Ref<Blackboard>(memnew(Blackboard));
}
BTPlayer::~BTPlayer() {
}

View File

@ -3,11 +3,11 @@
#ifndef BT_PLAYER_H
#define BT_PLAYER_H
#include "core/object.h"
#include "scene/main/node.h"
#include "behavior_tree.h"
#include "bt_task.h"
#include "core/object.h"
#include "modules/limboai/blackboard.h"
#include "scene/main/node.h"
#include <pulse/proplist.h>
class BTPlayer : public Node {
@ -25,7 +25,7 @@ private:
UpdateMode update_mode = UpdateMode::IDLE;
bool active = false;
bool auto_restart = false;
Dictionary blackboard;
Ref<Blackboard> blackboard;
Ref<BehaviorTree> _loaded_tree;
Ref<BTTask> _root_task;
@ -35,6 +35,9 @@ private:
protected:
static void _bind_methods();
void _set_blackboard_data(Dictionary p_value) { blackboard->set_data(p_value); }
Dictionary _get_blackboard_data() const { return blackboard->get_data(); }
void _notification(int p_notification);
public:
@ -50,11 +53,14 @@ public:
void set_auto_restart(bool p_value) { auto_restart = p_value; }
bool get_auto_restart() const { return auto_restart; }
void set_blackboard(Dictionary p_value) { blackboard = p_value; }
Dictionary get_blackboard() const { return blackboard; }
Ref<Blackboard> get_blackboard() const { return blackboard; }
void set_blackboard(const Ref<Blackboard> &p_blackboard) { blackboard = p_blackboard; }
void update(float p_delta);
void restart();
BTPlayer();
~BTPlayer();
};
#endif // BT_PLAYER_H

View File

@ -8,8 +8,10 @@
#include "core/script_language.h"
#include "core/variant.h"
#include "editor/editor_node.h"
#include "modules/limboai/blackboard.h"
#include "modules/limboai/limbo_string_names.h"
#include "modules/limboai/limbo_utility.h"
#include <cstddef>
String BTTask::_generate_name() const {
if (get_script_instance()) {
@ -72,7 +74,9 @@ void BTTask::set_custom_name(const String &p_name) {
}
};
void BTTask::initialize(Object *p_agent, Dictionary p_blackboard) {
void BTTask::initialize(Object *p_agent, const Ref<Blackboard> &p_blackboard) {
ERR_FAIL_COND(p_agent == nullptr);
ERR_FAIL_COND(p_blackboard == nullptr);
agent = p_agent;
blackboard = p_blackboard;
for (int i = 0; i < children.size(); i++) {
@ -239,7 +243,7 @@ void BTTask::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_agent"), &BTTask::get_agent);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "agent", PROPERTY_HINT_NONE, "", 0, "Object"), "", "get_agent");
ClassDB::bind_method(D_METHOD("get_blackboard"), &BTTask::get_blackboard);
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "blackboard", PROPERTY_HINT_NONE, "", 0, "Dictionary"), "", "get_blackboard");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "blackboard", PROPERTY_HINT_NONE, "", 0, "Blackboard"), "", "get_blackboard");
ClassDB::bind_method(D_METHOD("get_parent"), &BTTask::get_parent);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "parent", PROPERTY_HINT_NONE, "", 0, "BTTask"), "", "get_parent");
ClassDB::bind_method(D_METHOD("get_children"), &BTTask::_get_children);
@ -261,8 +265,6 @@ void BTTask::_bind_methods() {
BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::STRING, ""), "_generate_name"));
ClassDB::bind_method(D_METHOD("_get_configuration_warning"), &BTTask::get_configuration_warning);
BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::STRING, ""), "_get_configuration_warning"));
// ClassDB::bind_method(D_METHOD("_get_icon"), &BTTask::get_icon);
// BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "_get_icon"));
// Public Methods.
ClassDB::bind_method(D_METHOD("is_root"), &BTTask::is_root);
@ -293,7 +295,6 @@ BTTask::BTTask() {
custom_name = String();
agent = nullptr;
parent = nullptr;
blackboard = Dictionary();
children = Vector<Ref<BTTask>>();
status = FRESH;
}

View File

@ -10,6 +10,7 @@
#include "core/resource.h"
#include "core/ustring.h"
#include "core/vector.h"
#include "modules/limboai/blackboard.h"
#include "scene/resources/texture.h"
class BTTask : public Resource {
@ -28,7 +29,7 @@ private:
String custom_name;
Object *agent;
Dictionary blackboard;
Ref<Blackboard> blackboard;
BTTask *parent;
Vector<Ref<BTTask>> children;
int status;
@ -47,7 +48,7 @@ protected:
public:
Object *get_agent() const { return agent; }
Dictionary get_blackboard() const { return blackboard; }
Ref<Blackboard> get_blackboard() const { return blackboard; }
Ref<BTTask> get_parent() const { return Ref<BTTask>(parent); }
bool is_root() const { return parent == nullptr; }
Ref<BTTask> get_root() const;
@ -56,7 +57,7 @@ public:
void set_custom_name(const String &p_name);
String get_task_name() const;
void initialize(Object *p_agent, Dictionary p_blackboard);
void initialize(Object *p_agent, const Ref<Blackboard> &p_blackboard);
virtual Ref<BTTask> clone() const;
int execute(float p_delta);
void cancel();

View File

@ -13,14 +13,14 @@ void BTCooldown::_setup() {
if (cooldown_state_var.empty()) {
cooldown_state_var = vformat("cooldown_%d", rand());
}
get_blackboard()[cooldown_state_var] = false;
get_blackboard()->set(cooldown_state_var, false);
if (start_cooled) {
_chill();
}
}
int BTCooldown::_tick(float p_delta) {
if (get_blackboard().get(cooldown_state_var, true)) {
if (get_blackboard()->get_var(cooldown_state_var, true)) {
return FAILURE;
}
int status = get_child(0)->execute(p_delta);
@ -31,7 +31,7 @@ int BTCooldown::_tick(float p_delta) {
}
void BTCooldown::_chill() {
get_blackboard()[cooldown_state_var] = true;
get_blackboard()->set(cooldown_state_var, true);
if (_timer.is_valid()) {
_timer->set_time_left(duration);
} else {
@ -41,7 +41,7 @@ void BTCooldown::_chill() {
}
void BTCooldown::_on_timeout() {
get_blackboard()[cooldown_state_var] = false;
get_blackboard()->set(cooldown_state_var, false);
_timer.unref();
}

View File

@ -4,6 +4,7 @@
#include "core/class_db.h"
#include "blackboard.h"
#include "bt/actions/bt_action.h"
#include "bt/actions/bt_console_print.h"
#include "bt/actions/bt_fail.h"
@ -43,6 +44,7 @@
#endif
void register_limboai_types() {
ClassDB::register_class<Blackboard>();
ClassDB::register_class<BTTask>();
ClassDB::register_class<BehaviorTree>();
ClassDB::register_class<BTPlayer>();