CallMethod: Use array of BBVariant to specify arguments

This allows specifying variables on the blackboard as arguments in additions to raw values.

WARNING: Breaks compatibility if you are using CallMethod tasks. CallMethod arguments in existing trees will need to be reassigned!
This commit is contained in:
Serhii Snitsaruk 2024-01-15 21:51:17 +01:00
parent 184361d460
commit c63785b5bc
3 changed files with 18 additions and 9 deletions

View File

@ -14,6 +14,10 @@
#include "../../../util/limbo_compat.h" #include "../../../util/limbo_compat.h"
#include "../../../util/limbo_utility.h" #include "../../../util/limbo_utility.h"
#ifdef LIMBOAI_GDEXTENSION
#include "godot_cpp/classes/global_constants.hpp"
#endif // LIMBOAI_GDEXTENSION
//**** Setters / Getters //**** Setters / Getters
void BTCallMethod::set_method(StringName p_method_name) { void BTCallMethod::set_method(StringName p_method_name) {
@ -83,6 +87,7 @@ BT::Status BTCallMethod::_tick(double p_delta) {
ERR_FAIL_COND_V_MSG(obj == nullptr, FAILURE, "BTCallMethod: Failed to get object: " + node_param->to_string()); ERR_FAIL_COND_V_MSG(obj == nullptr, FAILURE, "BTCallMethod: Failed to get object: " + node_param->to_string());
Variant result; Variant result;
Array call_args;
#ifdef LIMBOAI_MODULE #ifdef LIMBOAI_MODULE
const Variant delta = include_delta ? Variant(p_delta) : Variant(); const Variant delta = include_delta ? Variant(p_delta) : Variant();
@ -95,7 +100,9 @@ BT::Status BTCallMethod::_tick(double p_delta) {
argptrs[0] = δ argptrs[0] = δ
} }
for (int i = 0; i < args.size(); i++) { for (int i = 0; i < args.size(); i++) {
argptrs[i + int(include_delta)] = &args[i]; Ref<BBVariant> param = args[i];
call_args.push_back(param->get_value(get_agent(), get_blackboard()));
argptrs[i + int(include_delta)] = &call_args[i];
} }
} }
@ -105,12 +112,12 @@ BT::Status BTCallMethod::_tick(double p_delta) {
ERR_FAIL_V_MSG(FAILURE, "BTCallMethod: Error calling method: " + Variant::get_call_error_text(obj, method, argptrs, argument_count, ce) + "."); ERR_FAIL_V_MSG(FAILURE, "BTCallMethod: Error calling method: " + Variant::get_call_error_text(obj, method, argptrs, argument_count, ce) + ".");
} }
#elif LIMBOAI_GDEXTENSION #elif LIMBOAI_GDEXTENSION
Array call_args;
if (include_delta) { if (include_delta) {
call_args.push_back(Variant(p_delta)); call_args.push_back(Variant(p_delta));
call_args.append_array(args); }
} else { for (int i = 0; i < args.size(); i++) {
call_args = args; Ref<BBVariant> param = args[i];
call_args.push_back(param->get_value(get_agent(), get_blackboard()));
} }
// TODO: Unsure how to detect call error, so we return SUCCESS for now... // TODO: Unsure how to detect call error, so we return SUCCESS for now...
@ -143,7 +150,7 @@ void BTCallMethod::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::STRING, "result_var"), "set_result_var", "get_result_var"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "result_var"), "set_result_var", "get_result_var");
ADD_GROUP("Arguments", "args_"); ADD_GROUP("Arguments", "args_");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "args_include_delta"), "set_include_delta", "is_delta_included"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "args_include_delta"), "set_include_delta", "is_delta_included");
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "args"), "set_args", "get_args"); ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "args", PROPERTY_HINT_ARRAY_TYPE, RESOURCE_TYPE_HINT("BBVariant")), "set_args", "get_args");
// ADD_PROPERTY_DEFAULT("args_include_delta", false); // ADD_PROPERTY_DEFAULT("args_include_delta", false);
} }

View File

@ -15,6 +15,7 @@
#include "../bt_action.h" #include "../bt_action.h"
#include "../../../blackboard/bb_param/bb_node.h" #include "../../../blackboard/bb_param/bb_node.h"
#include "../../../blackboard/bb_param/bb_variant.h"
class BTCallMethod : public BTAction { class BTCallMethod : public BTAction {
GDCLASS(BTCallMethod, BTAction); GDCLASS(BTCallMethod, BTAction);
@ -23,7 +24,7 @@ class BTCallMethod : public BTAction {
private: private:
StringName method; StringName method;
Ref<BBNode> node_param; Ref<BBNode> node_param;
Array args; TypedArray<BBVariant> args;
bool include_delta = false; bool include_delta = false;
String result_var; String result_var;
@ -40,8 +41,8 @@ public:
void set_node_param(Ref<BBNode> p_object); void set_node_param(Ref<BBNode> p_object);
Ref<BBNode> get_node_param() const { return node_param; } Ref<BBNode> get_node_param() const { return node_param; }
void set_args(Array p_args); void set_args(TypedArray<BBVariant> p_args);
Array get_args() const { return args; } TypedArray<BBVariant> get_args() const { return args; }
void set_include_delta(bool p_include_delta); void set_include_delta(bool p_include_delta);
bool is_delta_included() const { return include_delta; } bool is_delta_included() const { return include_delta; }

View File

@ -170,6 +170,7 @@ inline void VARIANT_DELETE_IF_OBJECT(Variant m_variant) {
#define PROJECT_CONFIG_FILE() GET_PROJECT_SETTINGS_DIR().path_join("limbo_ai.cfg") #define PROJECT_CONFIG_FILE() GET_PROJECT_SETTINGS_DIR().path_join("limbo_ai.cfg")
#define IS_RESOURCE_FILE(m_path) (m_path.begins_with("res://") && m_path.find("::") == -1) #define IS_RESOURCE_FILE(m_path) (m_path.begins_with("res://") && m_path.find("::") == -1)
#define RESOURCE_TYPE_HINT(m_type) vformat("%s/%s:%s", Variant::OBJECT, PROPERTY_HINT_RESOURCE_TYPE, m_type)
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED