2023-08-07 12:36:19 +00:00
|
|
|
/**
|
|
|
|
* bt_set_var.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.
|
|
|
|
* =============================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "bt_set_var.h"
|
|
|
|
|
2024-01-06 23:47:46 +00:00
|
|
|
String BTSetVar::_generate_name() {
|
2023-08-07 12:36:19 +00:00
|
|
|
if (variable.is_empty()) {
|
|
|
|
return "SetVar ???";
|
|
|
|
}
|
2023-11-15 13:45:10 +00:00
|
|
|
return vformat("Set %s %s= %s",
|
|
|
|
LimboUtility::get_singleton()->decorate_var(variable),
|
|
|
|
LimboUtility::get_singleton()->get_operation_string(operation),
|
2023-08-07 12:36:19 +00:00
|
|
|
value.is_valid() ? Variant(value) : Variant("???"));
|
|
|
|
}
|
|
|
|
|
2023-09-19 11:43:26 +00:00
|
|
|
BT::Status BTSetVar::_tick(double p_delta) {
|
2023-09-02 12:16:52 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(variable.is_empty(), FAILURE, "BTSetVar: `variable` is not set.");
|
|
|
|
ERR_FAIL_COND_V_MSG(!value.is_valid(), FAILURE, "BTSetVar: `value` is not set.");
|
2023-11-15 13:45:10 +00:00
|
|
|
Variant result;
|
2024-01-09 12:42:54 +00:00
|
|
|
Variant error_result = LW_NAME(error_value);
|
2023-11-15 13:45:10 +00:00
|
|
|
Variant right_value = value->get_value(get_agent(), get_blackboard(), error_result);
|
|
|
|
ERR_FAIL_COND_V_MSG(right_value == error_result, FAILURE, "BTSetVar: Failed to get parameter value. Returning FAILURE.");
|
2023-11-15 14:40:07 +00:00
|
|
|
if (operation == LimboUtility::OPERATION_NONE) {
|
2023-11-15 13:45:10 +00:00
|
|
|
result = right_value;
|
2023-11-15 14:40:07 +00:00
|
|
|
} else if (operation != LimboUtility::OPERATION_NONE) {
|
2023-11-15 13:45:10 +00:00
|
|
|
Variant left_value = get_blackboard()->get_var(variable, error_result);
|
|
|
|
ERR_FAIL_COND_V_MSG(left_value == error_result, FAILURE, vformat("BTSetVar: Failed to get \"%s\" blackboard variable. Returning FAILURE.", variable));
|
|
|
|
result = LimboUtility::get_singleton()->perform_operation(operation, left_value, right_value);
|
2023-11-15 15:35:56 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(result == Variant(), FAILURE, "BTSetVar: Operation not valid. Returning FAILURE.");
|
2023-11-15 13:45:10 +00:00
|
|
|
}
|
2023-09-02 12:16:52 +00:00
|
|
|
get_blackboard()->set_var(variable, result);
|
2023-08-07 12:36:19 +00:00
|
|
|
return SUCCESS;
|
|
|
|
};
|
|
|
|
|
|
|
|
void BTSetVar::set_variable(const String &p_variable) {
|
|
|
|
variable = p_variable;
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTSetVar::set_value(Ref<BBVariant> p_value) {
|
|
|
|
value = p_value;
|
|
|
|
emit_changed();
|
|
|
|
if (Engine::get_singleton()->is_editor_hint() && value.is_valid()) {
|
2024-01-09 12:42:54 +00:00
|
|
|
value->connect(LW_NAME(changed), Callable(this, LW_NAME(emit_changed)));
|
2023-08-07 12:36:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-15 13:45:10 +00:00
|
|
|
void BTSetVar::set_operation(LimboUtility::Operation p_operation) {
|
|
|
|
operation = p_operation;
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2024-01-06 23:47:46 +00:00
|
|
|
PackedStringArray BTSetVar::get_configuration_warnings() {
|
2023-08-15 15:05:30 +00:00
|
|
|
PackedStringArray warnings = BTAction::get_configuration_warnings();
|
2023-08-07 12:36:19 +00:00
|
|
|
if (variable.is_empty()) {
|
2023-08-15 15:05:30 +00:00
|
|
|
warnings.append("`variable` should be assigned.");
|
2023-08-07 12:36:19 +00:00
|
|
|
}
|
|
|
|
if (!value.is_valid()) {
|
2023-08-15 15:05:30 +00:00
|
|
|
warnings.append("`value` should be assigned.");
|
2023-08-07 12:36:19 +00:00
|
|
|
}
|
2023-08-15 15:05:30 +00:00
|
|
|
return warnings;
|
2023-08-07 12:36:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BTSetVar::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_variable", "p_variable"), &BTSetVar::set_variable);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_variable"), &BTSetVar::get_variable);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_value", "p_value"), &BTSetVar::set_value);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_value"), &BTSetVar::get_value);
|
2023-11-15 13:45:10 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_operation"), &BTSetVar::get_operation);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_operation", "p_operation"), &BTSetVar::set_operation);
|
2023-08-07 12:36:19 +00:00
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "variable"), "set_variable", "get_variable");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "value", PROPERTY_HINT_RESOURCE_TYPE, "BBVariant"), "set_value", "get_value");
|
2023-11-15 14:40:07 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "operation", PROPERTY_HINT_ENUM, "None,Addition,Subtraction,Multiplication,Division,Modulo,Power,Bitwise Shift Left,Bitwise Shift Right,Bitwise AND,Bitwise OR,Bitwise XOR"), "set_operation", "get_operation");
|
2023-08-07 12:36:19 +00:00
|
|
|
}
|