2023-07-21 09:50:06 +00:00
|
|
|
/**
|
|
|
|
* blackboard.cpp
|
|
|
|
* =============================================================================
|
2024-03-03 22:38:52 +00:00
|
|
|
* Copyright 2021-2024 Serhii Snitsaruk
|
2023-07-21 09:50:06 +00:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style
|
|
|
|
* license that can be found in the LICENSE file or at
|
|
|
|
* https://opensource.org/licenses/MIT.
|
|
|
|
* =============================================================================
|
|
|
|
*/
|
2022-09-08 13:56:51 +00:00
|
|
|
|
|
|
|
#include "blackboard.h"
|
2023-07-20 16:35:36 +00:00
|
|
|
|
2024-01-06 20:04:34 +00:00
|
|
|
#ifdef LIMBOAI_MODULE
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/variant/variant.h"
|
2022-10-19 14:01:16 +00:00
|
|
|
#include "scene/main/node.h"
|
2024-01-06 20:04:34 +00:00
|
|
|
#endif // LIMBOAI_MODULE
|
|
|
|
|
|
|
|
#ifdef LIMBOAI_GDEXTENSION
|
|
|
|
#include <godot_cpp/classes/node.hpp>
|
|
|
|
#include <godot_cpp/classes/ref.hpp>
|
|
|
|
#include <godot_cpp/classes/ref_counted.hpp>
|
|
|
|
#include <godot_cpp/core/object.hpp>
|
|
|
|
using namespace godot;
|
|
|
|
#endif
|
2022-09-08 13:56:51 +00:00
|
|
|
|
2022-11-23 17:02:46 +00:00
|
|
|
Ref<Blackboard> Blackboard::top() const {
|
|
|
|
Ref<Blackboard> bb(this);
|
2024-01-25 13:27:14 +00:00
|
|
|
while (bb->get_parent().is_valid()) {
|
|
|
|
bb = bb->get_parent();
|
2022-11-23 17:02:46 +00:00
|
|
|
}
|
|
|
|
return bb;
|
|
|
|
}
|
|
|
|
|
2024-03-03 22:38:52 +00:00
|
|
|
Variant Blackboard::get_var(const StringName &p_name, const Variant &p_default, bool p_complain) const {
|
2024-01-23 11:05:54 +00:00
|
|
|
if (data.has(p_name)) {
|
|
|
|
return data.get(p_name).get_value();
|
2022-09-08 13:56:51 +00:00
|
|
|
} else if (parent.is_valid()) {
|
2024-03-11 22:04:34 +00:00
|
|
|
return parent->get_var(p_name, p_default, p_complain);
|
2022-09-08 13:56:51 +00:00
|
|
|
} else {
|
2024-02-13 16:07:53 +00:00
|
|
|
if (p_complain) {
|
|
|
|
ERR_PRINT(vformat("Blackboard: Variable \"%s\" not found.", p_name));
|
|
|
|
}
|
2022-09-08 13:56:51 +00:00
|
|
|
return p_default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-03 22:38:52 +00:00
|
|
|
void Blackboard::set_var(const StringName &p_name, const Variant &p_value) {
|
2024-01-23 11:05:54 +00:00
|
|
|
if (data.has(p_name)) {
|
2024-01-25 13:27:14 +00:00
|
|
|
// Not checking type - allowing duck-typing.
|
2024-01-23 11:05:54 +00:00
|
|
|
data[p_name].set_value(p_value);
|
|
|
|
} else {
|
2024-01-25 13:27:14 +00:00
|
|
|
BBVariable var(p_value.get_type());
|
2024-01-23 11:05:54 +00:00
|
|
|
var.set_value(p_value);
|
|
|
|
data.insert(p_name, var);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-03 22:38:52 +00:00
|
|
|
bool Blackboard::has_var(const StringName &p_name) const {
|
2024-01-23 11:05:54 +00:00
|
|
|
return data.has(p_name) || (parent.is_valid() && parent->has_var(p_name));
|
2022-09-08 13:56:51 +00:00
|
|
|
}
|
|
|
|
|
2024-03-03 22:38:52 +00:00
|
|
|
void Blackboard::erase_var(const StringName &p_name) {
|
2024-01-23 11:05:54 +00:00
|
|
|
data.erase(p_name);
|
2022-09-08 13:56:51 +00:00
|
|
|
}
|
|
|
|
|
2024-05-31 07:56:45 +00:00
|
|
|
TypedArray<StringName> Blackboard::list_vars() const {
|
|
|
|
TypedArray<StringName> var_names;
|
|
|
|
var_names.resize(data.size());
|
|
|
|
int idx = 0;
|
|
|
|
for (const KeyValue<StringName, BBVariable> &kv : data) {
|
|
|
|
var_names[idx] = kv.key;
|
|
|
|
idx += 1;
|
|
|
|
}
|
|
|
|
return var_names;
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary Blackboard::get_vars_as_dict() const {
|
|
|
|
Dictionary dict;
|
|
|
|
for (const KeyValue<StringName, BBVariable> &kv : data) {
|
|
|
|
dict[kv.key] = kv.value.get_value();
|
|
|
|
}
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Blackboard::populate_from_dict(const Dictionary &p_dictionary) {
|
|
|
|
Array keys = p_dictionary.keys();
|
|
|
|
for (int i = 0; i < keys.size(); i++) {
|
|
|
|
if (keys[i].get_type() == Variant::STRING_NAME || keys[i].get_type() == Variant::STRING) {
|
|
|
|
set_var(keys[i], p_dictionary[keys[i]]);
|
|
|
|
} else {
|
|
|
|
ERR_PRINT("Blackboard: Invalid key type in dictionary to populate blackboard. Must be StringName or String.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-09 09:34:22 +00:00
|
|
|
void Blackboard::bind_var_to_property(const StringName &p_name, Object *p_object, const StringName &p_property, bool p_create) {
|
|
|
|
if (!data.has(p_name)) {
|
|
|
|
if (p_create) {
|
|
|
|
data.insert(p_name, BBVariable());
|
|
|
|
} else {
|
|
|
|
ERR_FAIL_MSG("Blackboard: Can't bind variable that doesn't exist (var: " + p_name + ").");
|
|
|
|
}
|
|
|
|
}
|
2024-01-27 15:38:58 +00:00
|
|
|
data[p_name].bind(p_object, p_property);
|
|
|
|
}
|
|
|
|
|
2024-03-03 22:38:52 +00:00
|
|
|
void Blackboard::unbind_var(const StringName &p_name) {
|
2024-04-09 09:34:22 +00:00
|
|
|
ERR_FAIL_COND_MSG(data.has(p_name), "Blackboard: Can't unbind variable that doesn't exist (var: " + p_name + ").");
|
2024-01-27 15:38:58 +00:00
|
|
|
data[p_name].unbind();
|
|
|
|
}
|
|
|
|
|
2024-03-11 23:30:38 +00:00
|
|
|
void Blackboard::assign_var(const StringName &p_name, const BBVariable &p_var) {
|
2024-01-23 11:05:54 +00:00
|
|
|
data.insert(p_name, p_var);
|
2022-12-16 11:13:03 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 09:34:22 +00:00
|
|
|
void Blackboard::link_var(const StringName &p_name, const Ref<Blackboard> &p_target_blackboard, const StringName &p_target_var, bool p_create) {
|
|
|
|
if (!data.has(p_name)) {
|
|
|
|
if (p_create) {
|
|
|
|
data.insert(p_name, BBVariable());
|
|
|
|
} else {
|
|
|
|
ERR_FAIL_MSG("Blackboard: Can't link variable that doesn't exist (var: " + p_name + ").");
|
|
|
|
}
|
|
|
|
}
|
2024-03-12 15:17:34 +00:00
|
|
|
ERR_FAIL_COND_MSG(p_target_blackboard.is_null(), "Blackboard: Can't link variable to target blackboard that is null (var: " + p_name + ").");
|
|
|
|
ERR_FAIL_COND_MSG(!p_target_blackboard->data.has(p_target_var), "Blackboard: Can't link variable to non-existent target (var: " + p_name + ", target: " + p_target_var + ").");
|
|
|
|
data[p_name] = p_target_blackboard->data[p_target_var];
|
|
|
|
}
|
|
|
|
|
2022-09-08 13:56:51 +00:00
|
|
|
void Blackboard::_bind_methods() {
|
2024-03-04 20:36:16 +00:00
|
|
|
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);
|
|
|
|
ClassDB::bind_method(D_METHOD("has_var", "var_name"), &Blackboard::has_var);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_parent", "blackboard"), &Blackboard::set_parent);
|
2024-03-02 14:43:55 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_parent"), &Blackboard::get_parent);
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("erase_var", "var_name"), &Blackboard::erase_var);
|
2024-05-31 07:56:45 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("clear"), &Blackboard::clear);
|
|
|
|
ClassDB::bind_method(D_METHOD("list_vars"), &Blackboard::list_vars);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_vars_as_dict"), &Blackboard::get_vars_as_dict);
|
|
|
|
ClassDB::bind_method(D_METHOD("populate_from_dict", "dictionary"), &Blackboard::populate_from_dict);
|
2022-11-23 17:02:46 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("top"), &Blackboard::top);
|
2024-04-09 09:34:22 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("bind_var_to_property", "var_name", "object", "property", "create"), &Blackboard::bind_var_to_property, DEFVAL(false));
|
2024-03-04 20:36:16 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("unbind_var", "var_name"), &Blackboard::unbind_var);
|
2024-04-09 09:34:22 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("link_var", "var_name", "target_blackboard", "target_var", "create"), &Blackboard::link_var, DEFVAL(false));
|
2024-01-13 16:10:42 +00:00
|
|
|
}
|