Repository: godotengine/godot
File: main/main.cpp (C++)

Code before:
				//autoload
				OrderedHashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();

				//first pass, add the constants so they exist before any script is loaded
				for (OrderedHashMap<StringName, ProjectSettings::AutoloadInfo>::Element E = autoloads.front(); E; E = E.next()) {
					const ProjectSettings::AutoloadInfo &info = E.get();

					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 (OrderedHashMap<StringName, ProjectSettings::AutoloadInfo>::Element E = autoloads.front(); E; E = E.next()) {
					const ProjectSettings::AutoloadInfo &info = E.get();

					RES res = ResourceLoader::load(info.path);
					ERR_CONTINUE_MSG(res.is_null(), "Can't autoload: " + info.path);
					Node *n = nullptr;
					if (res->is_class("PackedScene")) {
						Ref<PackedScene> ps = res;
						n = ps->instantiate();
					} else if (res->is_class("Script")) {
						Ref<Script> script_res = res;
						StringName ibt = script_res->get_instance_base_type();
						bool valid_type = ClassDB::is_parent_class(ibt, "Node");
						ERR_CONTINUE_MSG(!valid_type, "Script does not inherit a Node: " + info.path);

						Object *obj = ClassDB::instantiate(ibt);

						ERR_CONTINUE_MSG(obj == nullptr,
								"Cannot instance script for autoload, expected 'Node' inheritance, got: " +
										String(ibt));

						n = Object::cast_to<Node>(obj);
						n->set_script(script_res);
					}

					ERR_CONTINUE_MSG(!n, "Path in autoload not a node or script: " + info.path);
					n->set_name(info.name);

					//defer so references are all valid on _ready()
					to_add.push_back(n);

					if (info.is_singleton) {
						for (int i = 0; i < ScriptServer::get_language_count(); i++) {
							ScriptServer::get_language(i)->add_global_constant(info.name, n);
						}

Review comment (suggestion):
This exact code is present in at least three different locations to load autoloads. This should likely be moved to a dedicated method (maybe in `ResourceLoader` or similar?).

Code after:
				OrderedHashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();

				//first pass, add the constants so they exist before any script is loaded
				for (OrderedHashMap<StringName, ProjectSettings::AutoloadInfo>::Element E = autoloads.front(); E; E = E.next()) {
					const ProjectSettings::AutoloadInfo &info = E.get();

					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 (OrderedHashMap<StringName, ProjectSettings::AutoloadInfo>::Element E = autoloads.front(); E; E = E.next()) {
					const ProjectSettings::AutoloadInfo &info = E.get();

					RES res = ResourceLoader::load(info.path);
					ERR_CONTINUE_MSG(res.is_null(), "Can't autoload: " + info.path);
					Node *n = nullptr;
					Ref<PackedScene> scn = res;
					Ref<Script> script_res = res;
					if (scn.is_valid()) {
						n = scn->instantiate();
					} else if (script_res.is_valid()) {
						StringName ibt = script_res->get_instance_base_type();
						bool valid_type = ClassDB::is_parent_class(ibt, "Node");
						ERR_CONTINUE_MSG(!valid_type, "Script does not inherit a Node: " + info.path);

						Object *obj = ClassDB::instantiate(ibt);

						ERR_CONTINUE_MSG(!obj, "Cannot instance script for autoload, expected 'Node' inheritance, got: " + String(ibt) + ".");

						n = Object::cast_to<Node>(obj);
						n->set_script(script_res);
					}

					ERR_CONTINUE_MSG(!n, "Path in autoload not a node or script: " + info.path);
					n->set_name(info.name);

					//defer so references are all valid on _ready()
					to_add.push_back(n);

					if (info.is_singleton) {
						for (int i = 0; i < ScriptServer::get_language_count(); i++) {
							ScriptServer::get_language(i)->add_global_constant(info.name, n);
						}
					}
				}