Improve BTCallMethod

- Fix `include_delta` not initialized for some reason
- Add additional tests
- Group argument properties
- Improve name generation
This commit is contained in:
Serhii Snitsaruk 2023-12-28 14:26:39 +01:00
parent 8af747c599
commit 22bfb9e3ab
4 changed files with 65 additions and 10 deletions

View File

@ -54,15 +54,16 @@ PackedStringArray BTCallMethod::get_configuration_warnings() const {
}
String BTCallMethod::_generate_name() const {
int argument_count = include_delta ? args.size() + 1 : args.size();
Array final_args;
if (include_delta) {
final_args.push_back("delta");
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("]");
}
final_args.append_array(args);
return vformat("CallMethod %s(%s) node: %s",
(method != StringName() ? method : "???"),
(argument_count > 0 ? Variant(final_args).get_construct_string().trim_prefix("[").trim_suffix("]") : ""),
args_str,
(node_param.is_valid() && !node_param->to_string().is_empty() ? node_param->to_string() : "???"));
}
@ -107,9 +108,14 @@ void BTCallMethod::_bind_methods() {
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);
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "method"), "set_method", "get_method");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "node", PROPERTY_HINT_RESOURCE_TYPE, "BBNode"), "set_node_param", "get_node_param");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "include_delta"), "set_include_delta", "is_delta_included");
ADD_PROPERTY_DEFAULT("include_delta", false);
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");
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "args"), "set_args", "get_args");
ADD_PROPERTY_DEFAULT("args_include_delta", false);
}
BTCallMethod::BTCallMethod() {
}

View File

@ -46,6 +46,8 @@ public:
bool is_delta_included() const { return include_delta; }
virtual PackedStringArray get_configuration_warnings() const override;
BTCallMethod();
};
#endif // BT_CALL_METHOD

View File

@ -13,7 +13,7 @@
<member name="args" type="Array" setter="set_args" getter="get_args" default="[]">
The arguments to be passed when calling the method.
</member>
<member name="include_delta" type="bool" setter="set_include_delta" getter="is_delta_included" default="false">
<member name="args_include_delta" type="bool" setter="set_include_delta" getter="is_delta_included" default="false">
Include delta as a first parameter and shift the position of the rest of the arguments if any.
</member>
<member name="method" type="StringName" setter="set_method" getter="get_method" default="&amp;&quot;&quot;">

View File

@ -20,6 +20,7 @@
#include "modules/limboai/bt/tasks/utility/bt_call_method.h"
#include "core/os/memory.h"
#include "core/variant/array.h"
namespace TestCallMethod {
@ -64,6 +65,52 @@ TEST_CASE("[Modules][LimboAI] BTCallMethod") {
CHECK(cm->execute(0.01666) == BTTask::SUCCESS);
CHECK(callback_counter->num_callbacks == 1);
}
SUBCASE("With arguments") {
cm->set_method("callback_delta");
SUBCASE("Should fail with 0 arguments") {
cm->set_include_delta(false);
cm->set_args(Array());
ERR_PRINT_OFF;
CHECK(cm->execute(0.01666) == BTTask::FAILURE);
ERR_PRINT_ON;
CHECK(callback_counter->num_callbacks == 0);
}
SUBCASE("Should fail with too many arguments") {
cm->set_include_delta(true);
Array args;
args.push_back(0.2);
cm->set_args(args);
ERR_PRINT_OFF;
CHECK(cm->execute(0.01666) == BTTask::FAILURE);
ERR_PRINT_ON;
CHECK(callback_counter->num_callbacks == 0);
}
SUBCASE("Should fail with a wrong type arg") {
cm->set_include_delta(false);
Array args;
args.push_back("wrong_data");
cm->set_args(args);
ERR_PRINT_OFF;
CHECK(cm->execute(0.01666) == BTTask::FAILURE);
ERR_PRINT_ON;
CHECK(callback_counter->num_callbacks == 1);
}
SUBCASE("Should succeed with delta included") {
cm->set_include_delta(true);
cm->set_args(Array());
CHECK(cm->execute(0.01666) == BTTask::SUCCESS);
CHECK(callback_counter->num_callbacks == 1);
}
SUBCASE("Should succeed with one float arg") {
cm->set_include_delta(false);
Array args;
args.push_back(0.2);
cm->set_args(args);
CHECK(cm->execute(0.01666) == BTTask::SUCCESS);
CHECK(callback_counter->num_callbacks == 1);
}
}
memdelete(dummy);
}