2023-08-15 11:09:18 +00:00
|
|
|
/**
|
|
|
|
* bt_call_method.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_call_method.h"
|
|
|
|
|
|
|
|
//**** Setters / Getters
|
|
|
|
|
2023-08-28 17:24:55 +00:00
|
|
|
void BTCallMethod::set_method(StringName p_method_name) {
|
|
|
|
method = p_method_name;
|
2023-08-15 11:09:18 +00:00
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BTCallMethod::set_node_param(Ref<BBNode> p_object) {
|
|
|
|
node_param = p_object;
|
|
|
|
emit_changed();
|
|
|
|
if (Engine::get_singleton()->is_editor_hint() && node_param.is_valid()) {
|
|
|
|
node_param->connect(SNAME("changed"), Callable(this, SNAME("emit_changed")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-27 18:43:54 +00:00
|
|
|
void BTCallMethod::set_include_delta(bool p_include_delta) {
|
|
|
|
include_delta = p_include_delta;
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
2023-08-15 11:09:18 +00:00
|
|
|
void BTCallMethod::set_args(Array p_args) {
|
|
|
|
args = p_args;
|
|
|
|
emit_changed();
|
|
|
|
}
|
|
|
|
|
|
|
|
//**** Task Implementation
|
|
|
|
|
2023-08-15 15:05:30 +00:00
|
|
|
PackedStringArray BTCallMethod::get_configuration_warnings() const {
|
|
|
|
PackedStringArray warnings = BTAction::get_configuration_warnings();
|
2023-08-28 17:24:55 +00:00
|
|
|
if (method == StringName()) {
|
2023-08-15 15:05:30 +00:00
|
|
|
warnings.append("Method Name is not set.");
|
2023-08-15 11:09:18 +00:00
|
|
|
}
|
|
|
|
if (node_param.is_null()) {
|
2023-08-15 15:05:30 +00:00
|
|
|
warnings.append("Node parameter is not set.");
|
2023-08-15 11:09:18 +00:00
|
|
|
} else if (node_param->get_value_source() == BBParam::SAVED_VALUE && node_param->get_saved_value().is_zero()) {
|
2023-08-15 15:05:30 +00:00
|
|
|
warnings.append("Path to node is not set.");
|
2023-08-15 11:09:18 +00:00
|
|
|
} else if (node_param->get_value_source() == BBParam::BLACKBOARD_VAR && node_param->get_variable() == StringName()) {
|
2023-08-15 15:05:30 +00:00
|
|
|
warnings.append("Node blackboard variable is not set.");
|
2023-08-15 11:09:18 +00:00
|
|
|
}
|
|
|
|
return warnings;
|
|
|
|
}
|
|
|
|
|
|
|
|
String BTCallMethod::_generate_name() const {
|
2023-12-28 13:26:39 +00:00
|
|
|
String args_str = include_delta ? "delta" : "";
|
|
|
|
if (args.size() > 0) {
|
|
|
|
if (!args_str.is_empty()) {
|
|
|
|
args_str += ", ";
|
|
|
|
}
|
|
|
|
args_str += Variant(args).get_construct_string().trim_prefix("[").trim_suffix("]");
|
2023-12-27 18:43:54 +00:00
|
|
|
}
|
2023-08-15 11:09:18 +00:00
|
|
|
return vformat("CallMethod %s(%s) node: %s",
|
2023-08-28 17:24:55 +00:00
|
|
|
(method != StringName() ? method : "???"),
|
2023-12-28 13:26:39 +00:00
|
|
|
args_str,
|
2023-08-15 11:09:18 +00:00
|
|
|
(node_param.is_valid() && !node_param->to_string().is_empty() ? node_param->to_string() : "???"));
|
|
|
|
}
|
|
|
|
|
2023-09-19 11:43:26 +00:00
|
|
|
BT::Status BTCallMethod::_tick(double p_delta) {
|
2023-08-28 17:24:55 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(method == StringName(), FAILURE, "BTCallMethod: Method Name is not set.");
|
2023-08-15 11:09:18 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(node_param.is_null(), FAILURE, "BTCallMethod: Node parameter is not set.");
|
|
|
|
Object *obj = node_param->get_value(get_agent(), get_blackboard());
|
2023-09-05 08:55:17 +00:00
|
|
|
ERR_FAIL_COND_V_MSG(obj == nullptr, FAILURE, "BTCallMethod: Failed to get object: " + node_param->to_string());
|
2023-08-15 11:09:18 +00:00
|
|
|
|
2023-12-27 18:43:54 +00:00
|
|
|
const Variant delta = include_delta ? Variant(p_delta) : Variant();
|
2023-08-15 11:09:18 +00:00
|
|
|
const Variant **argptrs = nullptr;
|
|
|
|
|
2023-12-27 18:43:54 +00:00
|
|
|
int argument_count = include_delta ? args.size() + 1 : args.size();
|
|
|
|
if (argument_count > 0) {
|
|
|
|
argptrs = (const Variant **)alloca(sizeof(Variant *) * argument_count);
|
|
|
|
if (include_delta) {
|
|
|
|
argptrs[0] = δ
|
|
|
|
}
|
2023-08-15 11:09:18 +00:00
|
|
|
for (int i = 0; i < args.size(); i++) {
|
2023-12-27 18:43:54 +00:00
|
|
|
argptrs[i + int(include_delta)] = &args[i];
|
2023-08-15 11:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Callable::CallError ce;
|
2023-12-27 18:43:54 +00:00
|
|
|
obj->callp(method, argptrs, argument_count, ce);
|
2023-08-15 11:09:18 +00:00
|
|
|
if (ce.error != Callable::CallError::CALL_OK) {
|
2023-12-27 18:43:54 +00:00
|
|
|
ERR_FAIL_V_MSG(FAILURE, "BTCallMethod: Error calling method: " + Variant::get_call_error_text(obj, method, argptrs, argument_count, ce) + ".");
|
2023-08-15 11:09:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
//**** Godot
|
|
|
|
|
|
|
|
void BTCallMethod::_bind_methods() {
|
2023-08-28 17:24:55 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_method", "p_method"), &BTCallMethod::set_method);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_method"), &BTCallMethod::get_method);
|
2023-08-15 11:09:18 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_node_param", "p_param"), &BTCallMethod::set_node_param);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_node_param"), &BTCallMethod::get_node_param);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_args", "p_args"), &BTCallMethod::set_args);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_args"), &BTCallMethod::get_args);
|
2023-12-27 18:43:54 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_include_delta", "p_include_delta"), &BTCallMethod::set_include_delta);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_delta_included"), &BTCallMethod::is_delta_included);
|
2023-08-15 11:09:18 +00:00
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "BBNode"), "set_node_param", "get_node_param");
|
2023-12-28 13:26:39 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "method"), "set_method", "get_method");
|
|
|
|
ADD_GROUP("Arguments", "args_");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "args_include_delta"), "set_include_delta", "is_delta_included");
|
2023-08-15 11:09:18 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "args"), "set_args", "get_args");
|
2023-12-28 13:26:39 +00:00
|
|
|
|
|
|
|
ADD_PROPERTY_DEFAULT("args_include_delta", false);
|
|
|
|
}
|
|
|
|
|
|
|
|
BTCallMethod::BTCallMethod() {
|
2023-08-15 11:09:18 +00:00
|
|
|
}
|