limboai/blackboard/bb_param/bb_param.cpp

120 lines
3.6 KiB
C++
Raw Normal View History

/**
* bb_param.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.
* =============================================================================
*/
2022-10-20 16:26:46 +00:00
#include "bb_param.h"
2023-07-20 16:35:36 +00:00
2024-01-13 16:10:42 +00:00
#include "../../util/limbo_utility.h"
2023-07-20 16:35:36 +00:00
2024-01-13 16:10:42 +00:00
#ifdef LIMBOAI_MODULE
2024-01-06 20:05:08 +00:00
#endif // LIMBOAI_MODULE
#ifdef LIMBOAI_GDEXTENSION
using namespace godot;
#endif // LIMBOAI_GDEXTENSION
2022-10-20 16:26:46 +00:00
VARIANT_ENUM_CAST(BBParam::ValueSource);
2022-10-20 20:53:09 +00:00
void BBParam::set_value_source(ValueSource p_value) {
value_source = p_value;
notify_property_list_changed();
2022-10-20 20:53:09 +00:00
_update_name();
emit_changed();
}
2022-10-25 20:01:00 +00:00
Variant BBParam::get_saved_value() {
return saved_value;
}
2022-10-20 20:53:09 +00:00
void BBParam::set_saved_value(Variant p_value) {
2023-11-08 15:21:46 +00:00
if (p_value.get_type() == Variant::NIL) {
_assign_default_value();
} else {
2023-11-08 15:21:46 +00:00
saved_value = p_value;
}
2022-10-20 20:53:09 +00:00
_update_name();
emit_changed();
}
void BBParam::set_variable(const StringName &p_variable) {
variable = p_variable;
2022-10-20 20:53:09 +00:00
_update_name();
emit_changed();
}
2024-01-06 20:05:08 +00:00
#ifdef LIMBOAI_MODULE
2022-10-25 21:07:27 +00:00
String BBParam::to_string() {
2024-01-13 16:10:42 +00:00
#elif LIMBOAI_GDEXTENSION
2024-01-09 21:23:38 +00:00
String BBParam::_to_string() {
2024-01-13 16:10:42 +00:00
#endif
2022-10-25 21:07:27 +00:00
if (value_source == SAVED_VALUE) {
2023-08-07 12:29:58 +00:00
String s = saved_value.stringify();
switch (get_type()) {
case Variant::STRING: {
2024-01-09 21:23:38 +00:00
s = "\"" + s.c_escape() + "\"";
2023-08-07 12:29:58 +00:00
} break;
case Variant::STRING_NAME: {
2024-01-09 21:23:38 +00:00
s = "&\"" + s.c_escape() + "\"";
2023-08-07 12:29:58 +00:00
} break;
case Variant::NODE_PATH: {
2024-01-09 21:23:38 +00:00
s = "^\"" + s.c_escape() + "\"";
2023-08-07 12:29:58 +00:00
} break;
default: {
} break;
}
return s;
2022-10-25 21:07:27 +00:00
} else {
return LimboUtility::get_singleton()->decorate_var(variable);
}
}
2022-10-20 16:26:46 +00:00
Variant BBParam::get_value(Object *p_agent, const Ref<Blackboard> &p_blackboard, const Variant &p_default) {
2022-10-25 21:07:27 +00:00
ERR_FAIL_COND_V(!p_blackboard.is_valid(), p_default);
2022-10-20 16:26:46 +00:00
if (value_source == SAVED_VALUE) {
return saved_value;
} else {
2023-09-03 08:47:37 +00:00
ERR_FAIL_COND_V_MSG(!p_blackboard->has_var(variable), p_default, vformat("BBParam: Blackboard variable \"%s\" doesn't exist.", variable));
2022-10-20 16:26:46 +00:00
return p_blackboard->get_var(variable, p_default);
}
}
void BBParam::_get_property_list(List<PropertyInfo> *p_list) const {
if (value_source == ValueSource::SAVED_VALUE) {
p_list->push_back(PropertyInfo(get_type(), "saved_value"));
} else {
p_list->push_back(PropertyInfo(Variant::STRING_NAME, "variable"));
2022-10-20 16:26:46 +00:00
}
}
void BBParam::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_value_source", "value_source"), &BBParam::set_value_source);
2022-10-20 16:26:46 +00:00
ClassDB::bind_method(D_METHOD("get_value_source"), &BBParam::get_value_source);
ClassDB::bind_method(D_METHOD("set_saved_value", "value"), &BBParam::set_saved_value);
2022-10-20 16:26:46 +00:00
ClassDB::bind_method(D_METHOD("get_saved_value"), &BBParam::get_saved_value);
ClassDB::bind_method(D_METHOD("set_variable", "variable_name"), &BBParam::set_variable);
2022-10-20 16:26:46 +00:00
ClassDB::bind_method(D_METHOD("get_variable"), &BBParam::get_variable);
ClassDB::bind_method(D_METHOD("get_type"), &BBParam::get_type);
ClassDB::bind_method(D_METHOD("get_value", "agent", "blackboard", "default"), &BBParam::get_value, Variant());
2022-10-20 16:26:46 +00:00
2023-08-08 13:07:02 +00:00
ADD_PROPERTY(PropertyInfo(Variant::INT, "value_source", PROPERTY_HINT_ENUM, "Saved Value,Blackboard Var"), "set_value_source", "get_value_source");
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "variable", PROPERTY_HINT_NONE, "", 0), "set_variable", "get_variable");
2022-10-20 16:26:46 +00:00
ADD_PROPERTY(PropertyInfo(Variant::NIL, "saved_value", PROPERTY_HINT_NONE, "", 0), "set_saved_value", "get_saved_value");
BIND_ENUM_CONSTANT(SAVED_VALUE);
BIND_ENUM_CONSTANT(BLACKBOARD_VAR);
}
BBParam::BBParam() {
value_source = SAVED_VALUE;
2023-08-07 12:29:58 +00:00
_assign_default_value();
2022-10-20 16:26:46 +00:00
}