فهرست منبع

Differentiate between core and editor-only singletons

This makes sure that running scenes in debug mode
(from the editor) does not crash Godot.
In export mode it should already work correctly, because
editor-only singletons are never registered in the first place.
Yuri Sizov 1 سال پیش
والد
کامیت
4cdbbfd071
3فایلهای تغییر یافته به همراه36 افزوده شده و 4 حذف شده
  1. 28 2
      core/config/engine.cpp
  2. 5 1
      core/config/engine.h
  3. 3 1
      editor/register_editor_types.cpp

+ 28 - 2
core/config/engine.cpp

@@ -260,14 +260,21 @@ 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));
+	ERR_FAIL_COND_MSG(singleton_ptrs.has(p_singleton.name), vformat("Can't register singleton '%s' because it already exists.", p_singleton.name));
 	singletons.push_back(p_singleton);
 	singleton_ptrs[p_singleton.name] = p_singleton.ptr;
 }
 
 Object *Engine::get_singleton_object(const StringName &p_name) const {
 	HashMap<StringName, Object *>::ConstIterator E = singleton_ptrs.find(p_name);
-	ERR_FAIL_COND_V_MSG(!E, nullptr, "Failed to retrieve non-existent singleton '" + String(p_name) + "'.");
+	ERR_FAIL_COND_V_MSG(!E, nullptr, vformat("Failed to retrieve non-existent singleton '%s'.", p_name));
+
+#ifdef TOOLS_ENABLED
+	if (!is_editor_hint() && is_singleton_editor_only(p_name)) {
+		ERR_FAIL_V_MSG(nullptr, vformat("Can't retrieve singleton '%s' outside of editor.", p_name));
+	}
+#endif
+
 	return E->value;
 }
 
@@ -282,6 +289,19 @@ bool Engine::is_singleton_user_created(const StringName &p_name) const {
 
 	return false;
 }
+
+bool Engine::is_singleton_editor_only(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.editor_only) {
+			return true;
+		}
+	}
+
+	return false;
+}
+
 void Engine::remove_singleton(const StringName &p_name) {
 	ERR_FAIL_COND(!singleton_ptrs.has(p_name));
 
@@ -300,6 +320,12 @@ bool Engine::has_singleton(const StringName &p_name) const {
 
 void Engine::get_singletons(List<Singleton> *p_singletons) {
 	for (const Singleton &E : singletons) {
+#ifdef TOOLS_ENABLED
+		if (!is_editor_hint() && E.editor_only) {
+			continue;
+		}
+#endif
+
 		p_singletons->push_back(E);
 	}
 }

+ 5 - 1
core/config/engine.h

@@ -44,8 +44,11 @@ public:
 	struct Singleton {
 		StringName name;
 		Object *ptr = nullptr;
-		StringName class_name; //used for binding generation hinting
+		StringName class_name; // Used for binding generation hinting.
+		// Singleton scope flags.
 		bool user_created = false;
+		bool editor_only = false;
+
 		Singleton(const StringName &p_name = StringName(), Object *p_ptr = nullptr, const StringName &p_class_name = StringName());
 	};
 
@@ -129,6 +132,7 @@ public:
 	Object *get_singleton_object(const StringName &p_name) const;
 	void remove_singleton(const StringName &p_name);
 	bool is_singleton_user_created(const StringName &p_name) const;
+	bool is_singleton_editor_only(const StringName &p_name) const;
 
 #ifdef TOOLS_ENABLED
 	_FORCE_INLINE_ void set_editor_hint(bool p_enabled) { editor_hint = p_enabled; }

+ 3 - 1
editor/register_editor_types.cpp

@@ -275,7 +275,9 @@ void register_editor_types() {
 	GLOBAL_DEF("editor/version_control/autoload_on_startup", false);
 
 	EditorInterface::create();
-	Engine::get_singleton()->add_singleton(Engine::Singleton("EditorInterface", EditorInterface::get_singleton()));
+	Engine::Singleton ei_singleton = Engine::Singleton("EditorInterface", EditorInterface::get_singleton());
+	ei_singleton.editor_only = true;
+	Engine::get_singleton()->add_singleton(ei_singleton);
 
 	OS::get_singleton()->benchmark_end_measure("register_editor_types");
 }