limboai/bt/tasks/utility/bt_console_print.cpp

83 lines
3.1 KiB
C++
Raw Permalink Normal View History

/**
* bt_console_print.cpp
* =============================================================================
* Copyright 2021-2024 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.
* =============================================================================
*/
2022-09-06 11:08:52 +00:00
#include "bt_console_print.h"
2023-07-20 16:35:36 +00:00
2024-01-10 21:45:42 +00:00
#include "../../../util/limbo_compat.h"
String BTConsolePrint::_generate_name() {
2022-09-06 11:08:52 +00:00
String tx = text;
if (text.length() > 30) {
tx = text.substr(0, 30) + "...";
}
tx = tx.replace("\"", "\\\"");
tx = tx.replace("\r", "\\r");
tx = tx.replace("\t", "\\t");
tx = tx.replace("\n", "\\n");
2022-12-17 10:26:48 +00:00
if (bb_format_parameters.size() > 0) {
return vformat("ConsolePrint text: \"%s\" params: %s", tx, bb_format_parameters);
2022-09-06 11:08:52 +00:00
}
return vformat("ConsolePrint text: \"%s\"", tx);
2022-09-06 11:08:52 +00:00
}
BT::Status BTConsolePrint::_tick(double p_delta) {
2022-12-17 10:26:48 +00:00
switch (bb_format_parameters.size()) {
2022-09-06 11:08:52 +00:00
case 0: {
PRINT_LINE(text);
2022-09-06 11:08:52 +00:00
} break;
case 1: {
PRINT_LINE(vformat(text, get_blackboard()->get_var(bb_format_parameters[0], "")));
2022-09-06 11:08:52 +00:00
} break;
case 2: {
PRINT_LINE(vformat(text, get_blackboard()->get_var(bb_format_parameters[0], ""),
2022-12-17 10:26:48 +00:00
get_blackboard()->get_var(bb_format_parameters[1], "")));
2022-09-06 11:08:52 +00:00
} break;
case 3: {
PRINT_LINE(vformat(text, get_blackboard()->get_var(bb_format_parameters[0], ""),
2022-12-17 10:26:48 +00:00
get_blackboard()->get_var(bb_format_parameters[1], ""),
get_blackboard()->get_var(bb_format_parameters[2], "")));
2022-09-06 11:08:52 +00:00
} break;
case 4: {
PRINT_LINE(vformat(text, get_blackboard()->get_var(bb_format_parameters[0], ""),
2022-12-17 10:26:48 +00:00
get_blackboard()->get_var(bb_format_parameters[1], ""),
get_blackboard()->get_var(bb_format_parameters[2], ""),
get_blackboard()->get_var(bb_format_parameters[3], "")));
2022-09-06 11:08:52 +00:00
} break;
case 5:
default: {
PRINT_LINE(vformat(text, get_blackboard()->get_var(bb_format_parameters[0], ""),
2022-12-17 10:26:48 +00:00
get_blackboard()->get_var(bb_format_parameters[1], ""),
get_blackboard()->get_var(bb_format_parameters[2], ""),
get_blackboard()->get_var(bb_format_parameters[3], ""),
get_blackboard()->get_var(bb_format_parameters[4], "")));
2022-09-06 11:08:52 +00:00
} break;
}
return SUCCESS;
}
PackedStringArray BTConsolePrint::get_configuration_warnings() {
PackedStringArray warnings = BTAction::get_configuration_warnings();
2022-12-17 10:26:48 +00:00
if (bb_format_parameters.size() > 5) {
warnings.append("ConsolePrint supports up to 5 format arguments.");
2022-09-06 11:08:52 +00:00
}
return warnings;
2022-09-06 11:08:52 +00:00
}
void BTConsolePrint::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_text", "text"), &BTConsolePrint::set_text);
2022-09-06 11:08:52 +00:00
ClassDB::bind_method(D_METHOD("get_text"), &BTConsolePrint::get_text);
ClassDB::bind_method(D_METHOD("set_bb_format_parameters", "params"), &BTConsolePrint::set_bb_format_parameters);
2022-12-17 10:26:48 +00:00
ClassDB::bind_method(D_METHOD("get_bb_format_parameters"), &BTConsolePrint::get_bb_format_parameters);
2022-09-06 11:08:52 +00:00
ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
2022-12-17 10:26:48 +00:00
ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "bb_format_parameters"), "set_bb_format_parameters", "get_bb_format_parameters");
2024-01-13 16:10:42 +00:00
}