From caf1ff86b045adfb24f3c66ab5f7ee007a268656 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Mon, 20 Nov 2023 15:02:18 +0100 Subject: [PATCH] SetAgentProperty: Operation feature --- bt/tasks/scene/bt_set_agent_property.cpp | 27 +++++++++++++++++++----- bt/tasks/scene/bt_set_agent_property.h | 5 +++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/bt/tasks/scene/bt_set_agent_property.cpp b/bt/tasks/scene/bt_set_agent_property.cpp index 16f93df..dc3c87a 100644 --- a/bt/tasks/scene/bt_set_agent_property.cpp +++ b/bt/tasks/scene/bt_set_agent_property.cpp @@ -24,6 +24,11 @@ void BTSetAgentProperty::set_value(Ref p_value) { } } +void BTSetAgentProperty::set_operation(LimboUtility::Operation p_operation) { + operation = p_operation; + emit_changed(); +} + PackedStringArray BTSetAgentProperty::get_configuration_warnings() const { PackedStringArray warnings = BTAction::get_configuration_warnings(); if (property == StringName()) { @@ -48,13 +53,22 @@ BT::Status BTSetAgentProperty::_tick(double p_delta) { ERR_FAIL_COND_V_MSG(property == StringName(), FAILURE, "BTSetAgentProperty: `property` is not set."); ERR_FAIL_COND_V_MSG(!value.is_valid(), FAILURE, "BTSetAgentProperty: `value` is not set."); + Variant result; StringName error_value = SNAME("ErrorGettingValue"); - Variant v = value->get_value(get_agent(), get_blackboard(), error_value); - ERR_FAIL_COND_V_MSG(v == Variant(error_value), FAILURE, "BTSetAgentProperty: Couldn't get value of value-parameter."); - + Variant right_value = value->get_value(get_agent(), get_blackboard(), error_value); + ERR_FAIL_COND_V_MSG(right_value == Variant(error_value), FAILURE, "BTSetAgentProperty: Couldn't get value of value-parameter."); bool r_valid; - get_agent()->set(property, v, &r_valid); - ERR_FAIL_COND_V_MSG(!r_valid, FAILURE, vformat("BTSetAgentProperty: Couldn't set property \"%s\" with value \"%s\"", property, v)); + if (operation == LimboUtility::OPERATION_NONE) { + result = right_value; + } else { + Variant left_value = get_agent()->get(property, &r_valid); + ERR_FAIL_COND_V_MSG(!r_valid, FAILURE, vformat("BTSetAgentProperty: Failed to get agent's \"%s\" property. Returning FAILURE.", property)); + result = LimboUtility::get_singleton()->perform_operation(operation, left_value, right_value); + ERR_FAIL_COND_V_MSG(result == Variant(), FAILURE, "BTSetAgentProperty: Operation not valid. Returning FAILURE."); + } + + get_agent()->set(property, result, &r_valid); + ERR_FAIL_COND_V_MSG(!r_valid, FAILURE, vformat("BTSetAgentProperty: Couldn't set property \"%s\" with value \"%s\"", property, result)); return SUCCESS; } @@ -63,7 +77,10 @@ void BTSetAgentProperty::_bind_methods() { ClassDB::bind_method(D_METHOD("get_property"), &BTSetAgentProperty::get_property); ClassDB::bind_method(D_METHOD("set_value", "p_value"), &BTSetAgentProperty::set_value); ClassDB::bind_method(D_METHOD("get_value"), &BTSetAgentProperty::get_value); + ClassDB::bind_method(D_METHOD("set_operation", "p_operation"), &BTSetAgentProperty::set_operation); + ClassDB::bind_method(D_METHOD("get_operation"), &BTSetAgentProperty::get_operation); ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "property"), "set_property", "get_property"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "value", PROPERTY_HINT_RESOURCE_TYPE, "BBVariant"), "set_value", "get_value"); + 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"); } diff --git a/bt/tasks/scene/bt_set_agent_property.h b/bt/tasks/scene/bt_set_agent_property.h index 65fff0e..e5a4b12 100644 --- a/bt/tasks/scene/bt_set_agent_property.h +++ b/bt/tasks/scene/bt_set_agent_property.h @@ -15,6 +15,7 @@ #include "../bt_action.h" #include "modules/limboai/blackboard/bb_param/bb_variant.h" +#include "modules/limboai/util/limbo_utility.h" class BTSetAgentProperty : public BTAction { GDCLASS(BTSetAgentProperty, BTAction); @@ -23,6 +24,7 @@ class BTSetAgentProperty : public BTAction { private: StringName property; Ref value; + LimboUtility::Operation operation = LimboUtility::OPERATION_NONE; protected: static void _bind_methods(); @@ -38,6 +40,9 @@ public: void set_value(Ref p_value); Ref get_value() const { return value; } + + void set_operation(LimboUtility::Operation p_operation); + LimboUtility::Operation get_operation() const { return operation; } }; #endif // BT_SET_AGENT_PROPERTY \ No newline at end of file