Run autoload first pass before MainLoop initializes

This commit is contained in:
TML 2024-10-07 20:04:56 +08:00
parent db66bd35af
commit 6b69dd0376

View file

@ -3790,6 +3790,21 @@ int Main::start() {
}
#endif
if (!project_manager && !editor) { // game
HashMap<StringName, ProjectSettings::AutoloadInfo> 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<StringName, ProjectSettings::AutoloadInfo> &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<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
//first pass, add the constants so they exist before any script is loaded
for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &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<Node *> to_add;
for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : autoloads) {