limboai/bt/actions/bt_set_agent_property.cpp

69 lines
2.4 KiB
C++
Raw Normal View History

2023-08-11 10:33:39 +00:00
/**
* bt_set_agent_property.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_agent_property.h"
2023-08-11 20:16:03 +00:00
void BTSetAgentProperty::set_property(StringName p_prop) {
property = p_prop;
2023-08-11 10:33:39 +00:00
emit_changed();
}
void BTSetAgentProperty::set_value(Ref<BBVariant> p_value) {
value = p_value;
emit_changed();
if (Engine::get_singleton()->is_editor_hint() && value.is_valid()) {
value->connect(SNAME("changed"), Callable(this, SNAME("emit_changed")));
}
}
String BTSetAgentProperty::get_configuration_warning() const {
String warning = BTAction::get_configuration_warning();
if (!warning.is_empty()) {
warning += "\n";
}
2023-08-11 20:16:03 +00:00
if (property == StringName()) {
warning += "`property` should be assigned.\n";
2023-08-11 10:33:39 +00:00
}
if (!value.is_valid()) {
warning += "`value` should be assigned.\n";
}
return warning;
}
String BTSetAgentProperty::_generate_name() const {
2023-08-11 20:16:03 +00:00
if (property == StringName()) {
2023-08-11 10:33:39 +00:00
return "SetAgentProperty ???";
}
2023-08-11 20:16:03 +00:00
return vformat("Set agent.%s = %s", property,
2023-08-11 10:33:39 +00:00
value.is_valid() ? Variant(value) : Variant("???"));
}
int BTSetAgentProperty::_tick(double p_delta) {
2023-08-11 20:16:03 +00:00
ERR_FAIL_COND_V_MSG(property == StringName(), FAILURE, "BTSetAgentProperty: `property` is not set.");
2023-08-11 10:33:39 +00:00
ERR_FAIL_COND_V_MSG(!value.is_valid(), FAILURE, "BTSetAgentProperty: `value` is not set.");
bool r_valid;
2023-08-11 20:16:03 +00:00
get_agent()->set(property, value->get_value(get_agent(), get_blackboard()), &r_valid);
ERR_FAIL_COND_V_MSG(!r_valid, FAILURE, vformat("BTSetAgentProperty: Agent doesn't have property named \"%s\"", property));
2023-08-11 10:33:39 +00:00
return SUCCESS;
}
void BTSetAgentProperty::_bind_methods() {
2023-08-11 20:16:03 +00:00
ClassDB::bind_method(D_METHOD("set_property", "p_property"), &BTSetAgentProperty::set_property);
ClassDB::bind_method(D_METHOD("get_property"), &BTSetAgentProperty::get_property);
2023-08-11 10:33:39 +00:00
ClassDB::bind_method(D_METHOD("set_value", "p_value"), &BTSetAgentProperty::set_value);
ClassDB::bind_method(D_METHOD("get_value"), &BTSetAgentProperty::get_value);
2023-08-11 20:16:03 +00:00
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "property"), "set_property", "get_property");
2023-08-11 10:33:39 +00:00
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "value", PROPERTY_HINT_RESOURCE_TYPE, "BBVariant"), "set_value", "get_value");
}