From a6077e202d3e0746186a381144cbf204940c3b6f Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Wed, 9 Aug 2023 10:33:04 +0200 Subject: [PATCH] Add BTCheckTrigger --- bt/conditions/bt_check_trigger.cpp | 45 ++++++++++++++++++++++++++++++ bt/conditions/bt_check_trigger.h | 41 +++++++++++++++++++++++++++ config.py | 1 + register_types.cpp | 2 ++ 4 files changed, 89 insertions(+) create mode 100644 bt/conditions/bt_check_trigger.cpp create mode 100644 bt/conditions/bt_check_trigger.h diff --git a/bt/conditions/bt_check_trigger.cpp b/bt/conditions/bt_check_trigger.cpp new file mode 100644 index 0000000..517bd19 --- /dev/null +++ b/bt/conditions/bt_check_trigger.cpp @@ -0,0 +1,45 @@ +/** + * bt_check_trigger.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_check_trigger.h" + +#include "modules/limboai/util/limbo_utility.h" + +#include "core/variant/variant.h" + +void BTCheckTrigger::set_variable(String p_variable) { + variable = p_variable; + emit_changed(); +} + +String BTCheckTrigger::_generate_name() const { + if (variable.is_empty()) { + return "CheckTrigger ???"; + } + return "CheckTrigger " + LimboUtility::get_singleton()->decorate_var(variable); +} + +int BTCheckTrigger::_tick(double p_delta) { + ERR_FAIL_COND_V_MSG(variable.is_empty(), FAILURE, "BBCheckVar: `variable` is not set."); + Variant trigger_value = get_blackboard()->get_var(variable, false); + if (trigger_value == Variant(true)) { + get_blackboard()->set_var(variable, false); + return SUCCESS; + } + return FAILURE; +} + +void BTCheckTrigger::_bind_methods() { + ClassDB::bind_method(D_METHOD("set_variable", "p_variable"), &BTCheckTrigger::set_variable); + ClassDB::bind_method(D_METHOD("get_variable"), &BTCheckTrigger::get_variable); + + ADD_PROPERTY(PropertyInfo(Variant::STRING, "variable"), "set_variable", "get_variable"); +} diff --git a/bt/conditions/bt_check_trigger.h b/bt/conditions/bt_check_trigger.h new file mode 100644 index 0000000..aed4cc7 --- /dev/null +++ b/bt/conditions/bt_check_trigger.h @@ -0,0 +1,41 @@ +/** + * bt_check_trigger.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_check_trigger.h */ + +#ifndef BT_CHECK_TRIGGER_H +#define BT_CHECK_TRIGGER_H + +#include "bt_condition.h" + +#include "core/object/object.h" +#include "core/string/ustring.h" + +#include "core/object/class_db.h" + +class BTCheckTrigger : public BTCondition { + GDCLASS(BTCheckTrigger, BTCondition); + +private: + String variable; + +protected: + static void _bind_methods(); + + virtual String _generate_name() const override; + virtual int _tick(double p_delta) override; + +public: + void set_variable(String p_variable); + String get_variable() const { return variable; } +}; + +#endif // BT_CHECK_TRIGGER \ No newline at end of file diff --git a/config.py b/config.py index 7dda1ba..7a8a185 100644 --- a/config.py +++ b/config.py @@ -60,6 +60,7 @@ def get_doc_classes(): "BTAction", "BTAlwaysFail", "BTAlwaysSucceed", + "BTCheckTrigger", "BTCheckVar", "BTComposite", "BTCondition", diff --git a/register_types.cpp b/register_types.cpp index 5cec7d5..fd48a19 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -63,6 +63,7 @@ #include "bt/composites/bt_random_sequence.h" #include "bt/composites/bt_selector.h" #include "bt/composites/bt_sequence.h" +#include "bt/conditions/bt_check_trigger.h" #include "bt/conditions/bt_check_var.h" #include "bt/conditions/bt_condition.h" #include "bt/decorators/bt_always_fail.h" @@ -145,6 +146,7 @@ void initialize_limboai_module(ModuleInitializationLevel p_level) { GDREGISTER_CLASS(BTWaitTicks); GDREGISTER_CLASS(BTCondition); + GDREGISTER_CLASS(BTCheckTrigger); GDREGISTER_CLASS(BTCheckVar); GDREGISTER_ABSTRACT_CLASS(BBParam);