LimboUtility as singleton

This commit is contained in:
Serhii Snitsaruk 2022-09-21 23:56:04 +02:00
parent 7be7ca276d
commit 5d39130595
3 changed files with 37 additions and 20 deletions

View File

@ -1,28 +1,31 @@
/* limbo_utility.cpp */ /* limbo_utility.cpp */
#include "limbo_utility.h" #include "limbo_utility.h"
#include "core/class_db.h"
#include "core/dictionary.h"
#include "core/print_string.h"
#include "core/project_settings.h"
#include "core/variant.h" #include "core/variant.h"
String LimboUtility::get_script_class(const Ref<Script> &p_script) { LimboUtility *LimboUtility::singleton = nullptr;
String a_name = "";
Array script_classes = GLOBAL_GET("_global_script_classes"); LimboUtility *LimboUtility::get_singleton() {
String task_filepath = p_script->get_path(); return singleton;
for (int i = 0; i < script_classes.size(); i++) {
Dictionary a_class = script_classes[i];
Variant class_path = a_class["path"];
// print_line(vformat("Type: %s Path: %s TaskPath: %s", class_path.get_type(), class_path, task_filepath));
if (a_class["path"] == task_filepath) {
a_name = a_class["class"];
break;
} }
String LimboUtility::decorate_var(String p_variable) {
String var = p_variable.trim_prefix("$").trim_prefix("\"").trim_suffix("\"");
if (var.find(" ") == -1 and not var.empty()) {
return vformat("$%s", var);
} else {
return vformat("$\"%s\"", var);
} }
return a_name;
} }
void LimboUtility::_bind_methods() { void LimboUtility::_bind_methods() {
// ClassDB::bind_method(D_METHOD("get_class_name", "p_script"), &LimboUtility::get_class_name); ClassDB::bind_method(D_METHOD("decorate_var", "p_variable"), &LimboUtility::decorate_var);
}
LimboUtility::LimboUtility() {
singleton = this;
}
LimboUtility::~LimboUtility() {
singleton = nullptr;
} }

View File

@ -4,16 +4,21 @@
#define LIMBO_UTILITY_H #define LIMBO_UTILITY_H
#include "core/object.h" #include "core/object.h"
#include "core/script_language.h"
class LimboUtility : public Object { class LimboUtility : public Object {
GDCLASS(LimboUtility, Object); GDCLASS(LimboUtility, Object);
protected: protected:
static LimboUtility *singleton;
static void _bind_methods(); static void _bind_methods();
public: public:
static String get_script_class(const Ref<Script> &p_script); static LimboUtility *get_singleton();
String decorate_var(String p_variable);
LimboUtility();
~LimboUtility();
}; };
#endif // LIMBO_UTILITY_H #endif // LIMBO_UTILITY_H

View File

@ -37,6 +37,7 @@
#include "bt/decorators/bt_run_limit.h" #include "bt/decorators/bt_run_limit.h"
#include "bt/decorators/bt_subtree.h" #include "bt/decorators/bt_subtree.h"
#include "bt/decorators/bt_time_limit.h" #include "bt/decorators/bt_time_limit.h"
#include "core/os/memory.h"
#include "limbo_string_names.h" #include "limbo_string_names.h"
#include "limbo_utility.h" #include "limbo_utility.h"
@ -44,6 +45,8 @@
#include "editor/limbo_ai_editor_plugin.h" #include "editor/limbo_ai_editor_plugin.h"
#endif #endif
static LimboUtility *_limbo_utility = nullptr;
void register_limboai_types() { void register_limboai_types() {
ClassDB::register_class<Blackboard>(); ClassDB::register_class<Blackboard>();
ClassDB::register_class<BTTask>(); ClassDB::register_class<BTTask>();
@ -84,6 +87,10 @@ void register_limboai_types() {
ClassDB::register_class<BTCondition>(); ClassDB::register_class<BTCondition>();
_limbo_utility = memnew(LimboUtility);
ClassDB::register_class<LimboUtility>();
Engine::get_singleton()->add_singleton(Engine::Singleton("LimboUtility", LimboUtility::get_singleton()));
#ifdef TOOLS_ENABLED #ifdef TOOLS_ENABLED
EditorPlugins::add_by_type<LimboAIEditorPlugin>(); EditorPlugins::add_by_type<LimboAIEditorPlugin>();
#endif #endif
@ -93,4 +100,6 @@ void register_limboai_types() {
void unregister_limboai_types() { void unregister_limboai_types() {
LimboStringNames::free(); LimboStringNames::free();
memdelete(_limbo_utility);
} }