summaryrefslogtreecommitdiffstats
path: root/core/config/engine.cpp
diff options
context:
space:
mode:
authorreduz <reduzio@gmail.com>2021-08-24 19:03:10 -0300
committerreduz <reduzio@gmail.com>2021-08-25 08:32:25 -0300
commite2f8df8c5b3b6a1d4c18a75ff1c8293cc5909bc0 (patch)
treea85c182dce309f35dcf3da83349a688c644fd5a4 /core/config/engine.cpp
parentca7f53dd25cd7ee52004a09d4b7ababb9fa0bca6 (diff)
downloadredot-engine-e2f8df8c5b3b6a1d4c18a75ff1c8293cc5909bc0.tar.gz
Add ability to register singletons from engine API
* Exposed functions in Engine to register and unregister singletons. * Added the concept of user singletons, which can be removed (the system ones can't).
Diffstat (limited to 'core/config/engine.cpp')
-rw-r--r--core/config/engine.cpp30
1 files changed, 27 insertions, 3 deletions
diff --git a/core/config/engine.cpp b/core/config/engine.cpp
index 495670bc88..d8fbb50a75 100644
--- a/core/config/engine.cpp
+++ b/core/config/engine.cpp
@@ -199,17 +199,41 @@ bool Engine::is_printing_error_messages() const {
}
void Engine::add_singleton(const Singleton &p_singleton) {
+ ERR_FAIL_COND_MSG(singleton_ptrs.has(p_singleton.name), "Can't register singleton that already exists: " + String(p_singleton.name));
singletons.push_back(p_singleton);
singleton_ptrs[p_singleton.name] = p_singleton.ptr;
}
-Object *Engine::get_singleton_object(const String &p_name) const {
+Object *Engine::get_singleton_object(const StringName &p_name) const {
const Map<StringName, Object *>::Element *E = singleton_ptrs.find(p_name);
- ERR_FAIL_COND_V_MSG(!E, nullptr, "Failed to retrieve non-existent singleton '" + p_name + "'.");
+ ERR_FAIL_COND_V_MSG(!E, nullptr, "Failed to retrieve non-existent singleton '" + String(p_name) + "'.");
return E->get();
}
-bool Engine::has_singleton(const String &p_name) const {
+bool Engine::is_singleton_user_created(const StringName &p_name) const {
+ ERR_FAIL_COND_V(!singleton_ptrs.has(p_name), false);
+
+ for (const Singleton &E : singletons) {
+ if (E.name == p_name && E.user_created) {
+ return true;
+ }
+ }
+
+ return false;
+}
+void Engine::remove_singleton(const StringName &p_name) {
+ ERR_FAIL_COND(!singleton_ptrs.has(p_name));
+
+ for (List<Singleton>::Element *E = singletons.front(); E; E = E->next()) {
+ if (E->get().name == p_name) {
+ singletons.erase(E);
+ singleton_ptrs.erase(p_name);
+ return;
+ }
+ }
+}
+
+bool Engine::has_singleton(const StringName &p_name) const {
return singleton_ptrs.has(p_name);
}