2023-07-21 09:50:06 +00:00
|
|
|
/**
|
|
|
|
* blackboard.cpp
|
|
|
|
* =============================================================================
|
|
|
|
* Copyright 2021-2023 Serhii Snitsaruk
|
|
|
|
*
|
|
|
|
* 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/error/error_macros.h"
|
|
|
|
#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-01-23 11:05:54 +00:00
|
|
|
Variant Blackboard::get_var(const String &p_name, const Variant &p_default) const {
|
|
|
|
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-01-23 11:05:54 +00:00
|
|
|
return parent->get_var(p_name, p_default);
|
2022-09-08 13:56:51 +00:00
|
|
|
} else {
|
|
|
|
return p_default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-23 11:05:54 +00:00
|
|
|
void Blackboard::set_var(const String &p_name, const Variant &p_value) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Blackboard::has_var(const String &p_name) const {
|
|
|
|
return data.has(p_name) || (parent.is_valid() && parent->has_var(p_name));
|
2022-09-08 13:56:51 +00:00
|
|
|
}
|
|
|
|
|
2024-01-23 11:05:54 +00:00
|
|
|
void Blackboard::erase_var(const String &p_name) {
|
|
|
|
data.erase(p_name);
|
2022-09-08 13:56:51 +00:00
|
|
|
}
|
|
|
|
|
2024-01-23 11:05:54 +00:00
|
|
|
void Blackboard::add_var(const String &p_name, const BBVariable &p_var) {
|
|
|
|
ERR_FAIL_COND(data.has(p_name));
|
|
|
|
data.insert(p_name, p_var);
|
2022-12-16 11:13:03 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 14:01:16 +00:00
|
|
|
void Blackboard::prefetch_nodepath_vars(Node *p_node) {
|
|
|
|
ERR_FAIL_COND(p_node == nullptr);
|
2024-01-23 15:22:10 +00:00
|
|
|
for (const KeyValue<String, 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());
|
2022-10-19 14:01:16 +00:00
|
|
|
if (fetched_node != nullptr) {
|
2024-01-23 15:22:10 +00:00
|
|
|
var.set_value(fetched_node);
|
2022-10-19 14:01:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-13 16:10:42 +00:00
|
|
|
}
|
2022-10-19 14:01:16 +00:00
|
|
|
|
2022-09-08 13:56:51 +00:00
|
|
|
void Blackboard::_bind_methods() {
|
2024-01-23 11:05:54 +00:00
|
|
|
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);
|
2024-01-25 13:27:14 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_parent_scope", "p_blackboard"), &Blackboard::set_parent);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_parent_scope"), &Blackboard::get_parent);
|
2024-01-23 11:05:54 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("erase_var", "p_name"), &Blackboard::erase_var);
|
2022-10-19 21:54:42 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("prefetch_nodepath_vars", "p_node"), &Blackboard::prefetch_nodepath_vars);
|
2022-11-23 17:02:46 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("top"), &Blackboard::top);
|
2024-01-13 16:10:42 +00:00
|
|
|
}
|