Repository: godotengine/godot
File: editor/settings/editor_autoload_settings.cpp (C++)

Code before:
	if (!autoload_add_path->get_text().is_empty() && _autoload_name_is_valid(p_name, nullptr)) {
		_autoload_add();
	}
}

void EditorAutoloadSettings::_autoload_path_text_changed(const String &p_path) {
	add_autoload->set_disabled(!_autoload_name_is_valid(autoload_add_name->get_text(), nullptr));
}

void EditorAutoloadSettings::_autoload_text_changed(const String &p_name) {
	String error_string;
	bool is_name_valid = _autoload_name_is_valid(p_name, &error_string);
	add_autoload->set_disabled(!is_name_valid);
	error_message->set_text(error_string);
	error_message->set_visible(!autoload_add_name->get_text().is_empty() && !is_name_valid);
}

Node *EditorAutoloadSettings::_create_autoload(const String &p_path) {
	Node *n = nullptr;
	if (ResourceLoader::get_resource_type(p_path) == "PackedScene") {
		// Cache the scene reference before loading it (for cyclic references)
		Ref<PackedScene> scn;
		scn.instantiate();
		scn->set_path(p_path);
		scn->reload_from_file();
		ERR_FAIL_COND_V_MSG(scn.is_null(), nullptr, vformat("Failed to create an autoload, can't load from uid or path: %s.", p_path));

		if (scn.is_valid()) {
			n = scn->instantiate();
		}
	} else {
		Ref<Resource> res = ResourceLoader::load(p_path);
		ERR_FAIL_COND_V_MSG(res.is_null(), nullptr, vformat("Failed to create an autoload, can't load from uid or path: %s.", p_path));

		Ref<Script> scr = res;
		if (scr.is_valid()) {
			ERR_FAIL_COND_V_MSG(!scr->is_valid(), nullptr, vformat("Failed to create an autoload, script '%s' is not compiling.", p_path));

			StringName ibt = scr->get_instance_base_type();
			bool valid_type = ClassDB::is_parent_class(ibt, "Node");
			ERR_FAIL_COND_V_MSG(!valid_type, nullptr, vformat("Failed to create an autoload, script '%s' does not inherit from 'Node'.", p_path));

			Object *obj = ClassDB::instantiate(ibt);
			ERR_FAIL_NULL_V_MSG(obj, nullptr, vformat("Failed to create an autoload, cannot instantiate '%s'.", ibt));

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

	ERR_FAIL_NULL_V_MSG(n, nullptr, vformat("Failed to create an autoload, path is not pointing to a scene or a script: %s.", p_path));

Review comment (bug):
```suggestion
		ERR_FAIL_COND_V_MSG(scn.is_null(), nullptr, vformat("Failed to create an autoload, can't load from UID or path: %s.", p_path));
```

Code after:
	if (!autoload_add_path->get_text().is_empty() && _autoload_name_is_valid(p_name, nullptr)) {
		_autoload_add();
	}
}

void EditorAutoloadSettings::_autoload_path_text_changed(const String &p_path) {
	add_autoload->set_disabled(!_autoload_name_is_valid(autoload_add_name->get_text(), nullptr));
}

void EditorAutoloadSettings::_autoload_text_changed(const String &p_name) {
	String error_string;
	bool is_name_valid = _autoload_name_is_valid(p_name, &error_string);
	add_autoload->set_disabled(!is_name_valid);
	error_message->set_text(error_string);
	error_message->set_visible(!autoload_add_name->get_text().is_empty() && !is_name_valid);
}

Node *EditorAutoloadSettings::_create_autoload(const String &p_path) {
	Node *n = nullptr;
	if (ResourceLoader::get_resource_type(p_path) == "PackedScene") {
		// Cache the scene reference before loading it (for cyclic references)
		Ref<PackedScene> scn;
		scn.instantiate();
		scn->set_path(p_path);
		scn->reload_from_file();
		ERR_FAIL_COND_V_MSG(scn.is_null(), nullptr, vformat("Failed to create an autoload, can't load from UID or path: %s.", p_path));

		if (scn.is_valid()) {
			n = scn->instantiate();
		}
	} else {
		Ref<Resource> res = ResourceLoader::load(p_path);
		ERR_FAIL_COND_V_MSG(res.is_null(), nullptr, vformat("Failed to create an autoload, can't load from UID or path: %s.", p_path));

		Ref<Script> scr = res;
		if (scr.is_valid()) {
			ERR_FAIL_COND_V_MSG(!scr->is_valid(), nullptr, vformat("Failed to create an autoload, script '%s' is not compiling.", p_path));

			StringName ibt = scr->get_instance_base_type();
			bool valid_type = ClassDB::is_parent_class(ibt, "Node");
			ERR_FAIL_COND_V_MSG(!valid_type, nullptr, vformat("Failed to create an autoload, script '%s' does not inherit from 'Node'.", p_path));

			Object *obj = ClassDB::instantiate(ibt);
			ERR_FAIL_NULL_V_MSG(obj, nullptr, vformat("Failed to create an autoload, cannot instantiate '%s'.", ibt));

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

	ERR_FAIL_NULL_V_MSG(n, nullptr, vformat("Failed to create an autoload, path is not pointing to a scene or a script: %s.", p_path));