From a2309aa272f4b83f73f95905fb6322475822b159 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 11 May 2025 14:51:57 +0200 Subject: [PATCH] Now UserManagers have a register_as_global property. A manager will only try to set itself as the global manager if it's set to true (default). --- modules/users/managers/user_manager.cpp | 25 ++++++++++++++++++++++--- modules/users/managers/user_manager.h | 5 +++++ modules/users/singleton/user_db.cpp | 5 +++++ modules/users/singleton/user_db.h | 1 + 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/modules/users/managers/user_manager.cpp b/modules/users/managers/user_manager.cpp index 3a3b0b545..ba8256225 100644 --- a/modules/users/managers/user_manager.cpp +++ b/modules/users/managers/user_manager.cpp @@ -36,6 +36,13 @@ #include "../singleton/user_db.h" #include "../users/user.h" +bool UserManager::get_register_as_global() const { + return _register_as_global; +} +void UserManager::set_register_as_global(const bool p_global) { + _register_as_global = p_global; +} + Ref UserManager::get_user(const int id) { return call("_get_user", id); } @@ -106,20 +113,32 @@ Array UserManager::_get_all_users() { } UserManager::UserManager() { + _register_as_global = true; } UserManager::~UserManager() { } void UserManager::_notification(int p_what) { - if (p_what == NOTIFICATION_POST_ENTER_TREE) { - if (!Engine::get_singleton()->is_editor_hint()) { - UserDB::get_singleton()->set_user_manager(this); + switch (p_what) { + case NOTIFICATION_POST_ENTER_TREE: { + if (_register_as_global && !Engine::get_singleton()->is_editor_hint()) { + UserDB::get_singleton()->set_user_manager(this); + } + } + case NOTIFICATION_EXIT_TREE: { + if (_register_as_global && !Engine::get_singleton()->is_editor_hint()) { + UserDB::get_singleton()->unset_user_manager(this); + } } } } void UserManager::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_register_as_global"), &UserManager::get_register_as_global); + ClassDB::bind_method(D_METHOD("set_register_as_global", "global"), &UserManager::set_register_as_global); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "register_as_global"), "set_register_as_global", "get_register_as_global"); + BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::OBJECT, "ret", PROPERTY_HINT_RESOURCE_TYPE, "User"), "_get_user", PropertyInfo(Variant::INT, "id"))); BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::OBJECT, "ret", PROPERTY_HINT_RESOURCE_TYPE, "User"), "_get_user_name", PropertyInfo(Variant::STRING, "user_name"))); BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::OBJECT, "ret", PROPERTY_HINT_RESOURCE_TYPE, "User"), "_get_user_email", PropertyInfo(Variant::STRING, "user_email"))); diff --git a/modules/users/managers/user_manager.h b/modules/users/managers/user_manager.h index dfa551776..93f8b6e1f 100644 --- a/modules/users/managers/user_manager.h +++ b/modules/users/managers/user_manager.h @@ -44,6 +44,9 @@ class UserManager : public Node { GDCLASS(UserManager, Node); public: + bool get_register_as_global() const; + void set_register_as_global(const bool p_global); + Ref get_user(const int id); Ref get_user_name(const String &user_name); Ref get_user_email(const String &user_email); @@ -78,6 +81,8 @@ protected: void _notification(int p_what); static void _bind_methods(); + + bool _register_as_global; }; #endif diff --git a/modules/users/singleton/user_db.cpp b/modules/users/singleton/user_db.cpp index 1fb57521a..246eab9ed 100644 --- a/modules/users/singleton/user_db.cpp +++ b/modules/users/singleton/user_db.cpp @@ -84,6 +84,11 @@ UserManager *UserDB::get_user_manager() { void UserDB::set_user_manager(UserManager *um) { _user_manager = um; } +void UserDB::unset_user_manager(UserManager *um) { + if (_user_manager == um) { + _user_manager = NULL; + } +} Node *UserDB::get_user_manager_bind() { return _user_manager; } diff --git a/modules/users/singleton/user_db.h b/modules/users/singleton/user_db.h index 183a7172c..7662df77d 100644 --- a/modules/users/singleton/user_db.h +++ b/modules/users/singleton/user_db.h @@ -71,6 +71,7 @@ public: UserManager *get_user_manager(); void set_user_manager(UserManager *um); + void unset_user_manager(UserManager *um); static UserDB *get_singleton();