Add BTConsolePrint task

This commit is contained in:
Serhii Snitsaruk 2022-09-06 13:08:52 +02:00
parent afbf095d1b
commit 5a8442d5a0
4 changed files with 116 additions and 0 deletions

View File

@ -0,0 +1,74 @@
/* bt_console_print.cpp */
#include "bt_console_print.h"
#include "core/object.h"
#include "core/print_string.h"
#include "modules/limboai/bt/actions/bt_action.h"
String BTConsolePrint::_generate_name() const {
String tx = text;
if (text.length() > 30) {
tx = text.substr(0, 30) + "...";
}
tx = tx.replace("\"", "\\\"");
if (format_var_args.size() > 0) {
return vformat("ConsolePrint text: \"%s\" format_args: %s", tx, format_var_args);
}
return vformat("ConsolePrint \"%s\"", tx);
}
int BTConsolePrint::_tick(float p_delta) {
switch (format_var_args.size()) {
case 0: {
print_line(text);
} break;
case 1: {
print_line(vformat(text, get_blackboard().get(format_var_args[0], "")));
} break;
case 2: {
print_line(vformat(text, get_blackboard().get(format_var_args[0], ""),
get_blackboard().get(format_var_args[1], "")));
} break;
case 3: {
print_line(vformat(text, get_blackboard().get(format_var_args[0], ""),
get_blackboard().get(format_var_args[1], ""),
get_blackboard().get(format_var_args[2], "")));
} break;
case 4: {
print_line(vformat(text, get_blackboard().get(format_var_args[0], ""),
get_blackboard().get(format_var_args[1], ""),
get_blackboard().get(format_var_args[2], ""),
get_blackboard().get(format_var_args[3], "")));
} break;
case 5:
default: {
print_line(vformat(text, get_blackboard().get(format_var_args[0], ""),
get_blackboard().get(format_var_args[1], ""),
get_blackboard().get(format_var_args[2], ""),
get_blackboard().get(format_var_args[3], ""),
get_blackboard().get(format_var_args[4], "")));
} break;
}
return SUCCESS;
}
String BTConsolePrint::get_configuration_warning() const {
String warning = BTAction::get_configuration_warning();
if (!warning.empty()) {
warning += "\n";
}
if (format_var_args.size() > 5) {
warning += "ConsolePrint supports up to 5 format arguments.\n";
}
return warning;
}
void BTConsolePrint::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_text", "p_text"), &BTConsolePrint::set_text);
ClassDB::bind_method(D_METHOD("get_text"), &BTConsolePrint::get_text);
ClassDB::bind_method(D_METHOD("set_format_var_args", "p_variables"), &BTConsolePrint::set_format_var_args);
ClassDB::bind_method(D_METHOD("get_format_var_args"), &BTConsolePrint::get_format_var_args);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
ADD_PROPERTY(PropertyInfo(Variant::POOL_STRING_ARRAY, "format_var_args"), "set_format_var_args", "get_format_var_args");
}

View File

@ -0,0 +1,39 @@
/* bt_console_print.h */
#ifndef BT_CONSOLE_PRINT_H
#define BT_CONSOLE_PRINT_H
#include "bt_action.h"
#include "core/object.h"
#include "core/variant.h"
class BTConsolePrint : public BTAction {
GDCLASS(BTConsolePrint, BTAction);
private:
String text;
PoolStringArray format_var_args;
protected:
static void _bind_methods();
virtual String _generate_name() const;
virtual int _tick(float p_delta);
public:
void set_text(String p_value) {
text = p_value;
emit_changed();
}
String get_text() const { return text; }
void set_format_var_args(const PoolStringArray &p_value) {
format_var_args = p_value;
emit_changed();
}
PoolStringArray get_format_var_args() const { return format_var_args; }
virtual String get_configuration_warning() const;
};
#endif // BT_CONSOLE_PRINT_H

View File

@ -0,0 +1 @@
<svg enable-background="new 0 0 16 16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><g fill="#e0e0e0"><path d="m12.47 1.5h-8.94c-1.12 0-2.03.91-2.03 2.03v.77 1.25 1.86 2.59 1.16 1.31c0 1.12.91 2.03 2.03 2.03h8.94c1.12 0 2.03-.91 2.03-2.03v-1.04-.99-1.91-2.53-1.43-1.04c0-1.12-.91-2.03-2.03-2.03zm.83 4.5v2.56 1.88.99 1.04c0 .45-.38.83-.83.83h-8.94c-.45 0-.83-.38-.83-.83v-1.31-1.16-2.5-1.93-1.27-.77c0-.45.38-.83.83-.83h8.94c.45 0 .83.38.83.83v1.04z"/><path d="m10.65 7.35-4.8-2.85-.85 1.18 3.83 2.27-3.79 2.38.88 1.17 4.75-2.98c.2-.13.33-.35.33-.58v-.01c0-.23-.14-.45-.35-.58z"/></g></svg>

After

Width:  |  Height:  |  Size: 600 B

View File

@ -5,6 +5,7 @@
#include "core/class_db.h"
#include "bt/actions/bt_action.h"
#include "bt/actions/bt_console_print.h"
#include "bt/actions/bt_fail.h"
#include "bt/actions/bt_random_wait.h"
#include "bt/actions/bt_subtree.h"
@ -75,6 +76,7 @@ void register_limboai_types() {
ClassDB::register_class<BTRandomWait>();
ClassDB::register_class<BTWaitTicks>();
ClassDB::register_class<BTSubtree>();
ClassDB::register_class<BTConsolePrint>();
ClassDB::register_class<BTCondition>();