2024-01-23 09:33:57 +00:00
|
|
|
/**
|
|
|
|
* blackboard_source.cpp
|
|
|
|
* =============================================================================
|
|
|
|
* Copyright 2021-2024 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.
|
|
|
|
* =============================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "blackboard_source.h"
|
|
|
|
|
|
|
|
void BlackboardSource::set_value(const String &p_name, const Variant &p_value) {
|
2024-01-23 11:05:54 +00:00
|
|
|
ERR_FAIL_COND(!data.has(p_name));
|
|
|
|
data.get(p_name).set_value(p_value);
|
2024-01-23 09:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Variant BlackboardSource::get_value(const String &p_name) const {
|
2024-01-23 11:05:54 +00:00
|
|
|
ERR_FAIL_COND_V(!data.has(p_name), Variant());
|
|
|
|
return data.get(p_name).get_value();
|
2024-01-23 09:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlackboardSource::add_var(const String &p_name, const BBVariable &p_var) {
|
2024-01-23 11:05:54 +00:00
|
|
|
ERR_FAIL_COND(data.has(p_name));
|
2024-01-23 09:33:57 +00:00
|
|
|
ERR_FAIL_COND(base.is_valid());
|
2024-01-23 11:05:54 +00:00
|
|
|
data.insert(p_name, p_var);
|
2024-01-23 09:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlackboardSource::remove_var(const String &p_name) {
|
2024-01-23 11:05:54 +00:00
|
|
|
ERR_FAIL_COND(!data.has(p_name));
|
2024-01-23 09:33:57 +00:00
|
|
|
ERR_FAIL_COND(base.is_valid());
|
2024-01-23 11:05:54 +00:00
|
|
|
data.erase(p_name);
|
2024-01-23 09:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BBVariable BlackboardSource::get_var(const String &p_name) {
|
2024-01-23 11:05:54 +00:00
|
|
|
ERR_FAIL_COND_V(!data.has(p_name), BBVariable());
|
|
|
|
return data.get(p_name);
|
2024-01-23 09:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PackedStringArray BlackboardSource::list_vars() const {
|
|
|
|
PackedStringArray ret;
|
2024-01-23 11:05:54 +00:00
|
|
|
for (const KeyValue<String, BBVariable> &kv : data) {
|
2024-01-23 09:33:57 +00:00
|
|
|
ret.append(kv.key);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlackboardSource::sync_base() {
|
2024-01-23 11:05:54 +00:00
|
|
|
for (const KeyValue<String, BBVariable> &kv : base->data) {
|
|
|
|
if (!data.has(kv.key)) {
|
|
|
|
data.insert(kv.key, kv.value.duplicate());
|
2024-01-23 09:33:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-01-23 11:05:54 +00:00
|
|
|
BBVariable var = data.get(kv.key);
|
2024-01-23 09:33:57 +00:00
|
|
|
if (!var.is_same_prop_info(kv.value)) {
|
|
|
|
var.copy_prop_info(kv.value);
|
|
|
|
}
|
|
|
|
if (var.get_value().get_type() != kv.value.get_type()) {
|
|
|
|
var.set_value(kv.value.get_value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-23 11:05:54 +00:00
|
|
|
Ref<Blackboard> BlackboardSource::create_blackboard() {
|
2024-01-23 09:33:57 +00:00
|
|
|
Ref<Blackboard> bb = memnew(Blackboard);
|
2024-01-23 11:05:54 +00:00
|
|
|
for (const KeyValue<String, BBVariable> &kv : data) {
|
|
|
|
bb->add_var(kv.key, kv.value.duplicate());
|
|
|
|
}
|
2024-01-23 09:33:57 +00:00
|
|
|
return bb;
|
|
|
|
}
|
2024-01-23 11:05:54 +00:00
|
|
|
|
|
|
|
void BlackboardSource::populate_blackboard(const Ref<Blackboard> &p_blackboard, bool overwrite) {
|
|
|
|
for (const KeyValue<String, BBVariable> &kv : data) {
|
|
|
|
if (p_blackboard->has_var(kv.key)) {
|
|
|
|
if (overwrite) {
|
|
|
|
p_blackboard->erase_var(kv.key);
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p_blackboard->add_var(kv.key, kv.value.duplicate());
|
|
|
|
}
|
|
|
|
}
|