From 6b69dd0376f1c2a96995de2ab4f74c04f9272937 Mon Sep 17 00:00:00 2001 From: TML Date: Mon, 7 Oct 2024 20:04:56 +0800 Subject: [PATCH] Run autoload first pass before MainLoop initializes --- main/main.cpp | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/main/main.cpp b/main/main.cpp index 5206e9b84c5..5f788440e2e 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -3790,6 +3790,21 @@ int Main::start() { } #endif + if (!project_manager && !editor) { // game + HashMap autoloads = ProjectSettings::get_singleton()->get_autoload_list(); + + //first pass, add the constants so they exist before any script is loaded (including MainLoop scripts) + for (const KeyValue &E : autoloads) { + const ProjectSettings::AutoloadInfo &info = E.value; + + if (info.is_singleton) { + for (int i = 0; i < ScriptServer::get_language_count(); i++) { + ScriptServer::get_language(i)->add_global_constant(info.name, Variant()); + } + } + } + } + MainLoop *main_loop = nullptr; if (editor) { main_loop = memnew(SceneTree); @@ -3912,17 +3927,6 @@ int Main::start() { OS::get_singleton()->benchmark_begin_measure("Startup", "Load Autoloads"); HashMap autoloads = ProjectSettings::get_singleton()->get_autoload_list(); - //first pass, add the constants so they exist before any script is loaded - for (const KeyValue &E : autoloads) { - const ProjectSettings::AutoloadInfo &info = E.value; - - if (info.is_singleton) { - for (int i = 0; i < ScriptServer::get_language_count(); i++) { - ScriptServer::get_language(i)->add_global_constant(info.name, Variant()); - } - } - } - //second pass, load into global constants List to_add; for (const KeyValue &E : autoloads) {