Add BTSetVar action

BTSetVar action can be used to assign any variant-type value to a blackboard variable.
This commit is contained in:
Serhii Snitsaruk 2023-08-07 14:36:19 +02:00
parent b662c10513
commit 34e70d48ce
5 changed files with 124 additions and 6 deletions

68
bt/actions/bt_set_var.cpp Normal file
View File

@ -0,0 +1,68 @@
/**
* bt_set_var.cpp
* =============================================================================
* Copyright 2021-2023 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.
* =============================================================================
*/
#include "bt_set_var.h"
#include "modules/limboai/util/limbo_utility.h"
#include "core/variant/callable.h"
String BTSetVar::_generate_name() const {
if (variable.is_empty()) {
return "SetVar ???";
}
return vformat("SetVar %s = %s", LimboUtility::get_singleton()->decorate_var(variable),
value.is_valid() ? Variant(value) : Variant("???"));
}
int BTSetVar::_tick(double p_delta) {
ERR_FAIL_COND_V_MSG(variable.is_empty(), FAILURE, "BBSetVar: `variable` is not set.");
ERR_FAIL_COND_V_MSG(!value.is_valid(), FAILURE, "BBSetVar: `value` is not set.");
get_blackboard()->set_var(variable, value->get_value(get_agent(), get_blackboard()));
return SUCCESS;
};
void BTSetVar::set_variable(const String &p_variable) {
variable = p_variable;
emit_changed();
}
void BTSetVar::set_value(Ref<BBVariant> p_value) {
value = p_value;
emit_changed();
if (Engine::get_singleton()->is_editor_hint() && value.is_valid()) {
value->connect(SNAME("changed"), Callable(this, SNAME("emit_changed")));
}
}
String BTSetVar::get_configuration_warning() const {
String warning = BTAction::get_configuration_warning();
if (!warning.is_empty()) {
warning += "\n";
}
if (variable.is_empty()) {
warning += "`variable` should be assigned.\n";
}
if (!value.is_valid()) {
warning += "`value` should be assigned.\n";
}
return warning;
}
void BTSetVar::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_variable", "p_variable"), &BTSetVar::set_variable);
ClassDB::bind_method(D_METHOD("get_variable"), &BTSetVar::get_variable);
ClassDB::bind_method(D_METHOD("set_value", "p_value"), &BTSetVar::set_value);
ClassDB::bind_method(D_METHOD("get_value"), &BTSetVar::get_value);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "variable"), "set_variable", "get_variable");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "value", PROPERTY_HINT_RESOURCE_TYPE, "BBVariant"), "set_value", "get_value");
}

46
bt/actions/bt_set_var.h Normal file
View File

@ -0,0 +1,46 @@
/**
* bt_set_var.h
* =============================================================================
* Copyright 2021-2023 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.
* =============================================================================
*/
/* bt_set_var.h */
#ifndef BT_SET_VAR_H
#define BT_SET_VAR_H
#include "../actions/bt_action.h"
#include "../../blackboard/bb_param/bb_variant.h"
#include "core/object/object.h"
#include "core/string/ustring.h"
class BTSetVar : public BTAction {
GDCLASS(BTSetVar, BTAction);
private:
String variable;
Ref<BBVariant> value;
protected:
static void _bind_methods();
virtual String _generate_name() const override;
virtual int _tick(double p_delta) override;
public:
virtual String get_configuration_warning() const override;
void set_variable(const String &p_variable);
String get_variable() const { return variable; }
void set_value(Ref<BBVariant> p_value);
Ref<BBVariant> get_value() const { return value; }
};
#endif // BT_SET_VAR

View File

@ -84,6 +84,7 @@ def get_doc_classes():
"BTRunLimit", "BTRunLimit",
"BTSelector", "BTSelector",
"BTSequence", "BTSequence",
"BTSetVar",
"BTState", "BTState",
"BTSubtree", "BTSubtree",
"BTTask", "BTTask",

1
icons/BTSetVar.svg Normal file
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="m2.91 14.57c-1.37-2.28-1.91-4.36-1.91-6.5s.54-4.23 1.91-6.5l1.09 1.06c-1.26 2.36-1.51 3.87-1.51 5.44s.25 3.08 1.51 5.44z"/><path d="m13.09 1.57c1.37 2.27 1.91 4.35 1.91 6.5 0 2.14-.54 4.23-1.91 6.5l-1.09-1.06c1.26-2.37 1.51-3.87 1.51-5.44s-.25-3.08-1.51-5.44z"/><path d="m10.87 11.5h-1.83l-1.21-2.33-1.93 2.33h-1.9l2.96-3.53-1.84-3.47h1.89l1.18 2.23 1.86-2.23h1.95l-2.98 3.49z"/></g></svg>

After

Width:  |  Height:  |  Size: 510 B

View File

@ -48,6 +48,7 @@
#include "bt/actions/bt_console_print.h" #include "bt/actions/bt_console_print.h"
#include "bt/actions/bt_fail.h" #include "bt/actions/bt_fail.h"
#include "bt/actions/bt_random_wait.h" #include "bt/actions/bt_random_wait.h"
#include "bt/actions/bt_set_var.h"
#include "bt/actions/bt_wait.h" #include "bt/actions/bt_wait.h"
#include "bt/actions/bt_wait_ticks.h" #include "bt/actions/bt_wait_ticks.h"
#include "bt/behavior_tree.h" #include "bt/behavior_tree.h"
@ -133,13 +134,14 @@ void initialize_limboai_module(ModuleInitializationLevel p_level) {
GDREGISTER_CLASS(BTForEach); GDREGISTER_CLASS(BTForEach);
GDREGISTER_CLASS(BTAction); GDREGISTER_CLASS(BTAction);
GDREGISTER_CLASS(BTFail);
GDREGISTER_CLASS(BTWait);
GDREGISTER_CLASS(BTRandomWait);
GDREGISTER_CLASS(BTWaitTicks);
GDREGISTER_CLASS(BTNewScope);
GDREGISTER_CLASS(BTSubtree);
GDREGISTER_CLASS(BTConsolePrint); GDREGISTER_CLASS(BTConsolePrint);
GDREGISTER_CLASS(BTFail);
GDREGISTER_CLASS(BTNewScope);
GDREGISTER_CLASS(BTRandomWait);
GDREGISTER_CLASS(BTSetVar);
GDREGISTER_CLASS(BTSubtree);
GDREGISTER_CLASS(BTWait);
GDREGISTER_CLASS(BTWaitTicks);
GDREGISTER_CLASS(BTCondition); GDREGISTER_CLASS(BTCondition);