CallMethod: Store result on the blackboard

This commit is contained in:
Serhii Snitsaruk 2024-01-15 17:03:52 +01:00
parent aca9784c95
commit 184361d460
4 changed files with 28 additions and 6 deletions

View File

@ -12,6 +12,7 @@
#include "bt_call_method.h"
#include "../../../util/limbo_compat.h"
#include "../../../util/limbo_utility.h"
//**** Setters / Getters
@ -33,11 +34,16 @@ void BTCallMethod::set_include_delta(bool p_include_delta) {
emit_changed();
}
void BTCallMethod::set_args(Array p_args) {
void BTCallMethod::set_args(TypedArray<BBVariant> p_args) {
args = p_args;
emit_changed();
}
void BTCallMethod::set_result_var(const String &p_result_var) {
result_var = p_result_var;
emit_changed();
}
//**** Task Implementation
PackedStringArray BTCallMethod::get_configuration_warnings() {
@ -63,10 +69,11 @@ String BTCallMethod::_generate_name() {
}
args_str += vformat("%s", args).trim_prefix("[").trim_suffix("]");
}
return vformat("CallMethod %s(%s) node: %s",
(method != StringName() ? method : "???"),
return vformat("CallMethod %s(%s) node: %s %s",
method != StringName() ? method : "???",
args_str,
(node_param.is_valid() && !node_param->to_string().is_empty() ? node_param->to_string() : "???"));
node_param.is_valid() && !node_param->to_string().is_empty() ? node_param->to_string() : "???",
result_var.is_empty() ? "" : LW_NAME(output_var_prefix) + LimboUtility::get_singleton()->decorate_var(result_var));
}
BT::Status BTCallMethod::_tick(double p_delta) {
@ -75,6 +82,8 @@ BT::Status BTCallMethod::_tick(double p_delta) {
Object *obj = node_param->get_value(get_agent(), get_blackboard());
ERR_FAIL_COND_V_MSG(obj == nullptr, FAILURE, "BTCallMethod: Failed to get object: " + node_param->to_string());
Variant result;
#ifdef LIMBOAI_MODULE
const Variant delta = include_delta ? Variant(p_delta) : Variant();
const Variant **argptrs = nullptr;
@ -91,7 +100,7 @@ BT::Status BTCallMethod::_tick(double p_delta) {
}
Callable::CallError ce;
obj->callp(method, argptrs, argument_count, ce);
result = obj->callp(method, argptrs, argument_count, ce);
if (ce.error != Callable::CallError::CALL_OK) {
ERR_FAIL_V_MSG(FAILURE, "BTCallMethod: Error calling method: " + Variant::get_call_error_text(obj, method, argptrs, argument_count, ce) + ".");
}
@ -105,9 +114,13 @@ BT::Status BTCallMethod::_tick(double p_delta) {
}
// TODO: Unsure how to detect call error, so we return SUCCESS for now...
obj->callv(method, call_args);
result = obj->callv(method, call_args);
#endif // LIMBOAI_MODULE & LIMBOAI_GDEXTENSION
if (!result_var.is_empty()) {
get_blackboard()->set_var(result_var, result);
}
return SUCCESS;
}
@ -122,9 +135,12 @@ void BTCallMethod::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_args"), &BTCallMethod::get_args);
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);
ClassDB::bind_method(D_METHOD("set_result_var", "p_result_var"), &BTCallMethod::set_result_var);
ClassDB::bind_method(D_METHOD("get_result_var"), &BTCallMethod::get_result_var);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "BBNode"), "set_node_param", "get_node_param");
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "method"), "set_method", "get_method");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "result_var"), "set_result_var", "get_result_var");
ADD_GROUP("Arguments", "args_");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "args_include_delta"), "set_include_delta", "is_delta_included");
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "args"), "set_args", "get_args");

View File

@ -25,6 +25,7 @@ private:
Ref<BBNode> node_param;
Array args;
bool include_delta = false;
String result_var;
protected:
static void _bind_methods();
@ -45,6 +46,9 @@ public:
void set_include_delta(bool p_include_delta);
bool is_delta_included() const { return include_delta; }
void set_result_var(const String &p_result_var);
String get_result_var() const { return result_var; }
virtual PackedStringArray get_configuration_warnings() override;
BTCallMethod();

View File

@ -136,4 +136,5 @@ LimboStringNames::LimboStringNames() {
EVENT_FINISHED = "finished";
repeat_forever.parse_utf8("Repeat ∞");
output_var_prefix.parse_utf8("");
}

View File

@ -151,6 +151,7 @@ public:
String EVENT_FINISHED;
String repeat_forever;
String output_var_prefix;
};
#define LW_NAME(m_arg) LimboStringNames::get_singleton()->m_arg