2022-10-20 16:26:46 +00:00
|
|
|
/* bb_param.cpp */
|
|
|
|
|
|
|
|
#include "bb_param.h"
|
2023-07-20 16:35:36 +00:00
|
|
|
|
|
|
|
#include "modules/limboai/util/limbo_utility.h"
|
|
|
|
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/core_bind.h"
|
|
|
|
#include "core/error/error_macros.h"
|
|
|
|
#include "core/object/class_db.h"
|
|
|
|
#include "core/object/object.h"
|
|
|
|
#include "core/variant/variant.h"
|
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;
|
2022-12-15 07:26:52 +00:00
|
|
|
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() {
|
|
|
|
if (saved_value.get_type() != get_type()) {
|
2022-12-15 07:26:52 +00:00
|
|
|
Callable::CallError err;
|
|
|
|
Variant::construct(get_type(), saved_value, nullptr, 0, err);
|
2022-10-25 20:01:00 +00:00
|
|
|
}
|
|
|
|
return saved_value;
|
|
|
|
}
|
|
|
|
|
2022-10-20 20:53:09 +00:00
|
|
|
void BBParam::set_saved_value(Variant p_value) {
|
|
|
|
saved_value = p_value;
|
|
|
|
_update_name();
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BBParam::set_variable(const String &p_value) {
|
|
|
|
variable = p_value;
|
|
|
|
_update_name();
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2022-10-25 21:07:27 +00:00
|
|
|
String BBParam::to_string() {
|
|
|
|
if (value_source == SAVED_VALUE) {
|
|
|
|
return String(saved_value);
|
|
|
|
} 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 {
|
|
|
|
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, "variable"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BBParam::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_value_source", "p_value_source"), &BBParam::set_value_source);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_value_source"), &BBParam::get_value_source);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_saved_value", "p_value"), &BBParam::set_saved_value);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_saved_value"), &BBParam::get_saved_value);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_variable", "p_variable_name"), &BBParam::set_variable);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_variable"), &BBParam::get_variable);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_type"), &BBParam::get_type);
|
2022-10-20 20:53:09 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_value", "p_agent", "p_blackboard", "p_default"), &BBParam::get_value, Variant());
|
2022-10-20 16:26:46 +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, "variable", PROPERTY_HINT_NONE, "", 0), "set_variable", "get_variable");
|
|
|
|
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;
|
|
|
|
variable = "";
|
|
|
|
saved_value = Variant();
|
|
|
|
}
|